2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Adafruit DHT temperature and humidity sensor."""
|
2021-03-23 13:36:43 +00:00
|
|
|
from contextlib import suppress
|
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
|
|
|
|
2021-04-28 07:52:57 +00:00
|
|
|
import adafruit_dht
|
|
|
|
import board
|
2016-09-03 08:56:41 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2020-02-28 19:46:48 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_MONITORED_CONDITIONS,
|
|
|
|
CONF_NAME,
|
2021-02-23 22:32:39 +00:00
|
|
|
CONF_PIN,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2020-02-28 19:46:48 +00:00
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
)
|
2016-09-03 08:56:41 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
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_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-09-05 19:09:14 +00:00
|
|
|
SENSOR_HUMIDITY: ["Humidity", PERCENTAGE],
|
2015-08-28 12:42:41 +00:00
|
|
|
}
|
2016-09-03 08:56:41 +00:00
|
|
|
|
2021-04-28 07:52:57 +00:00
|
|
|
|
|
|
|
def validate_pin_input(value):
|
|
|
|
"""Validate that the GPIO PIN is prefixed with a D."""
|
|
|
|
try:
|
|
|
|
int(value)
|
|
|
|
return f"D{value}"
|
|
|
|
except ValueError:
|
|
|
|
return value.upper()
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_SENSOR): cv.string,
|
2021-04-28 07:52:57 +00:00
|
|
|
vol.Required(CONF_PIN): vol.All(cv.string, validate_pin_input),
|
2019-07-31 19:25:30 +00:00
|
|
|
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."""
|
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 = {
|
2021-04-28 07:52:57 +00:00
|
|
|
"AM2302": adafruit_dht.DHT22,
|
|
|
|
"DHT11": adafruit_dht.DHT11,
|
|
|
|
"DHT22": adafruit_dht.DHT22,
|
2015-08-28 12:42:41 +00:00
|
|
|
}
|
2020-04-15 12:10:07 +00:00
|
|
|
sensor = available_sensors.get(config[CONF_SENSOR])
|
|
|
|
pin = config[CONF_PIN]
|
|
|
|
temperature_offset = config[CONF_TEMPERATURE_OFFSET]
|
|
|
|
humidity_offset = config[CONF_HUMIDITY_OFFSET]
|
2021-04-28 07:52:57 +00:00
|
|
|
name = config[CONF_NAME]
|
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
|
|
|
|
2021-04-28 07:52:57 +00:00
|
|
|
data = DHTClient(sensor, pin, name)
|
2015-08-28 12:42:41 +00:00
|
|
|
dev = []
|
2016-02-09 17:58:04 +00:00
|
|
|
|
2021-03-23 13:36:43 +00:00
|
|
|
with suppress(KeyError):
|
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
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(dev, True)
|
2015-08-28 12:42:41 +00:00
|
|
|
|
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
class DHTSensor(SensorEntity):
|
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."""
|
|
|
|
|
2021-04-28 07:52:57 +00:00
|
|
|
def __init__(self, sensor, pin, name):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2015-08-28 12:42:41 +00:00
|
|
|
self.sensor = sensor
|
2021-04-28 07:52:57 +00:00
|
|
|
self.pin = getattr(board, pin)
|
2020-04-04 20:31:56 +00:00
|
|
|
self.data = {}
|
2021-04-28 07:52:57 +00:00
|
|
|
self.name = name
|
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."""
|
2021-04-28 07:52:57 +00:00
|
|
|
dht = self.sensor(self.pin)
|
|
|
|
try:
|
|
|
|
temperature = dht.temperature
|
|
|
|
humidity = dht.humidity
|
|
|
|
except RuntimeError:
|
|
|
|
_LOGGER.debug("Unexpected value from DHT sensor: %s", self.name)
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Error updating DHT sensor: %s", self.name)
|
|
|
|
else:
|
|
|
|
if temperature:
|
|
|
|
self.data[SENSOR_TEMPERATURE] = temperature
|
|
|
|
if humidity:
|
|
|
|
self.data[SENSOR_HUMIDITY] = humidity
|
|
|
|
finally:
|
|
|
|
dht.exit()
|