2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Adafruit DHT temperature and humidity sensor."""
|
2015-08-28 12:42:41 +00:00
|
|
|
from datetime import timedelta
|
2019-12-09 16:42:00 +00:00
|
|
|
import logging
|
2015-08-28 12:42:41 +00:00
|
|
|
|
2019-10-12 20:03:56 +00:00
|
|
|
import Adafruit_DHT # pylint: disable=import-error
|
2016-09-03 08:56:41 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2020-02-28 19:46:48 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_MONITORED_CONDITIONS,
|
|
|
|
CONF_NAME,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
UNIT_PERCENTAGE,
|
|
|
|
)
|
2016-09-03 08:56:41 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-08-28 12:42:41 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.util import Throttle
|
2016-07-31 20:24:49 +00:00
|
|
|
from homeassistant.util.temperature import celsius_to_fahrenheit
|
2015-08-28 12:42:41 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-09-03 08:56:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PIN = "pin"
|
|
|
|
CONF_SENSOR = "sensor"
|
|
|
|
CONF_HUMIDITY_OFFSET = "humidity_offset"
|
|
|
|
CONF_TEMPERATURE_OFFSET = "temperature_offset"
|
2016-09-03 08:56:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "DHT Sensor"
|
2016-09-03 08:56:41 +00:00
|
|
|
|
|
|
|
# DHT11 is able to deliver data once per second, DHT22 once every two
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_TEMPERATURE = "temperature"
|
|
|
|
SENSOR_HUMIDITY = "humidity"
|
2015-08-28 12:42:41 +00:00
|
|
|
SENSOR_TYPES = {
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_TEMPERATURE: ["Temperature", None],
|
2020-02-28 19:46:48 +00:00
|
|
|
SENSOR_HUMIDITY: ["Humidity", UNIT_PERCENTAGE],
|
2015-08-28 12:42:41 +00:00
|
|
|
}
|
2016-09-03 08:56:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_SENSOR): cv.string,
|
|
|
|
vol.Required(CONF_PIN): cv.string,
|
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_TEMPERATURE_OFFSET, default=0): vol.All(
|
|
|
|
vol.Coerce(float), vol.Range(min=-100, max=100)
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_HUMIDITY_OFFSET, default=0): vol.All(
|
|
|
|
vol.Coerce(float), vol.Range(min=-100, max=100)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2015-08-28 12:42:41 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the DHT sensor."""
|
2015-08-28 12:42:41 +00:00
|
|
|
|
2016-08-26 02:09:48 +00:00
|
|
|
SENSOR_TYPES[SENSOR_TEMPERATURE][1] = hass.config.units.temperature_unit
|
2015-08-28 12:42:41 +00:00
|
|
|
available_sensors = {
|
2018-11-28 12:38:26 +00:00
|
|
|
"AM2302": Adafruit_DHT.AM2302,
|
2015-08-28 12:42:41 +00:00
|
|
|
"DHT11": Adafruit_DHT.DHT11,
|
|
|
|
"DHT22": Adafruit_DHT.DHT22,
|
|
|
|
}
|
2016-08-26 02:09:48 +00:00
|
|
|
sensor = available_sensors.get(config.get(CONF_SENSOR))
|
|
|
|
pin = config.get(CONF_PIN)
|
2017-07-11 21:04:00 +00:00
|
|
|
temperature_offset = config.get(CONF_TEMPERATURE_OFFSET)
|
|
|
|
humidity_offset = config.get(CONF_HUMIDITY_OFFSET)
|
2015-08-28 12:42:41 +00:00
|
|
|
|
2016-09-03 08:56:41 +00:00
|
|
|
if not sensor:
|
|
|
|
_LOGGER.error("DHT sensor type is not supported")
|
|
|
|
return False
|
2015-08-28 12:42:41 +00:00
|
|
|
|
|
|
|
data = DHTClient(Adafruit_DHT, sensor, pin)
|
|
|
|
dev = []
|
2016-09-03 08:56:41 +00:00
|
|
|
name = config.get(CONF_NAME)
|
2016-02-09 17:58:04 +00:00
|
|
|
|
2015-08-28 12:42:41 +00:00
|
|
|
try:
|
2016-09-03 08:56:41 +00:00
|
|
|
for variable in config[CONF_MONITORED_CONDITIONS]:
|
2019-07-31 19:25:30 +00:00
|
|
|
dev.append(
|
|
|
|
DHTSensor(
|
|
|
|
data,
|
|
|
|
variable,
|
|
|
|
SENSOR_TYPES[variable][1],
|
|
|
|
name,
|
|
|
|
temperature_offset,
|
|
|
|
humidity_offset,
|
|
|
|
)
|
|
|
|
)
|
2015-08-28 12:42:41 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(dev, True)
|
2015-08-28 12:42:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DHTSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Implementation of the DHT sensor."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
dht_client,
|
|
|
|
sensor_type,
|
|
|
|
temp_unit,
|
|
|
|
name,
|
|
|
|
temperature_offset,
|
|
|
|
humidity_offset,
|
|
|
|
):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2016-02-09 17:58:04 +00:00
|
|
|
self.client_name = name
|
2015-08-28 12:42:41 +00:00
|
|
|
self._name = SENSOR_TYPES[sensor_type][0]
|
|
|
|
self.dht_client = dht_client
|
|
|
|
self.temp_unit = temp_unit
|
|
|
|
self.type = sensor_type
|
2017-07-11 21:04:00 +00:00
|
|
|
self.temperature_offset = temperature_offset
|
|
|
|
self.humidity_offset = humidity_offset
|
2015-08-28 12:42:41 +00:00
|
|
|
self._state = None
|
|
|
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2019-09-03 15:09:59 +00:00
|
|
|
return f"{self.client_name} {self._name}"
|
2015-08-28 12:42:41 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2015-08-28 12:42:41 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2015-08-28 12:42:41 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data from the DHT and updates the states."""
|
2015-08-28 12:42:41 +00:00
|
|
|
self.dht_client.update()
|
2017-07-11 21:04:00 +00:00
|
|
|
temperature_offset = self.temperature_offset
|
|
|
|
humidity_offset = self.humidity_offset
|
2015-08-28 12:42:41 +00:00
|
|
|
data = self.dht_client.data
|
|
|
|
|
2017-08-28 19:46:31 +00:00
|
|
|
if self.type == SENSOR_TEMPERATURE and SENSOR_TEMPERATURE in data:
|
2017-07-11 21:04:00 +00:00
|
|
|
temperature = data[SENSOR_TEMPERATURE]
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"Temperature %.1f \u00b0C + offset %.1f",
|
|
|
|
temperature,
|
|
|
|
temperature_offset,
|
|
|
|
)
|
2018-07-18 09:54:27 +00:00
|
|
|
if -20 <= temperature < 80:
|
2017-07-11 21:04:00 +00:00
|
|
|
self._state = round(temperature + temperature_offset, 1)
|
2016-08-14 08:02:26 +00:00
|
|
|
if self.temp_unit == TEMP_FAHRENHEIT:
|
2016-09-03 08:56:41 +00:00
|
|
|
self._state = round(celsius_to_fahrenheit(temperature), 1)
|
2017-08-28 19:46:31 +00:00
|
|
|
elif self.type == SENSOR_HUMIDITY and SENSOR_HUMIDITY in data:
|
2017-07-11 21:04:00 +00:00
|
|
|
humidity = data[SENSOR_HUMIDITY]
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("Humidity %.1f%% + offset %.1f", humidity, humidity_offset)
|
2018-07-18 09:54:27 +00:00
|
|
|
if 0 <= humidity <= 100:
|
2017-07-11 21:04:00 +00:00
|
|
|
self._state = round(humidity + humidity_offset, 1)
|
2015-08-28 12:42:41 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class DHTClient:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data from the DHT sensor."""
|
|
|
|
|
2015-08-28 12:42:41 +00:00
|
|
|
def __init__(self, adafruit_dht, sensor, pin):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2015-08-28 12:42:41 +00:00
|
|
|
self.adafruit_dht = adafruit_dht
|
|
|
|
self.sensor = sensor
|
|
|
|
self.pin = pin
|
2020-04-04 20:31:56 +00:00
|
|
|
self.data = {}
|
2015-08-28 12:42:41 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data the DHT sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
humidity, temperature = self.adafruit_dht.read_retry(self.sensor, self.pin)
|
2015-08-28 12:42:41 +00:00
|
|
|
if temperature:
|
2016-08-26 02:09:48 +00:00
|
|
|
self.data[SENSOR_TEMPERATURE] = temperature
|
2015-08-28 12:42:41 +00:00
|
|
|
if humidity:
|
2016-08-26 02:09:48 +00:00
|
|
|
self.data[SENSOR_HUMIDITY] = humidity
|