DHT support for humidity and temperature offset (#8238)

* Added support for temperature_offset and humidity_offset

Some DHT sensors require some offsets to work.

* Support for temperature and humidity offset

* Changed lines with 79 characters

* Moved const to dht.py from const.py

* Changed temperature_offset range

* Removed the const

const.py is at original state.

* Fixed continuation line under-indented

* Removed first round and added debug info
pull/8435/merge
gitmopp 2017-07-11 23:04:00 +02:00 committed by Fabian Affolter
parent ef94b5c77a
commit 471501d386
1 changed files with 24 additions and 6 deletions

View File

@ -26,6 +26,8 @@ _LOGGER = logging.getLogger(__name__)
CONF_PIN = 'pin'
CONF_SENSOR = 'sensor'
CONF_HUMIDITY_OFFSET = 'humidity_offset'
CONF_TEMPERATURE_OFFSET = 'temperature_offset'
DEFAULT_NAME = 'DHT Sensor'
@ -45,6 +47,10 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
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))
})
@ -61,6 +67,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
}
sensor = available_sensors.get(config.get(CONF_SENSOR))
pin = config.get(CONF_PIN)
temperature_offset = config.get(CONF_TEMPERATURE_OFFSET)
humidity_offset = config.get(CONF_HUMIDITY_OFFSET)
if not sensor:
_LOGGER.error("DHT sensor type is not supported")
@ -73,7 +81,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
try:
for variable in config[CONF_MONITORED_CONDITIONS]:
dev.append(DHTSensor(
data, variable, SENSOR_TYPES[variable][1], name))
data, variable, SENSOR_TYPES[variable][1], name,
temperature_offset, humidity_offset))
except KeyError:
pass
@ -83,13 +92,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class DHTSensor(Entity):
"""Implementation of the DHT sensor."""
def __init__(self, dht_client, sensor_type, temp_unit, name):
def __init__(self, dht_client, sensor_type, temp_unit, name,
temperature_offset, humidity_offset):
"""Initialize the sensor."""
self.client_name = name
self._name = SENSOR_TYPES[sensor_type][0]
self.dht_client = dht_client
self.temp_unit = temp_unit
self.type = sensor_type
self.temperature_offset = temperature_offset
self.humidity_offset = humidity_offset
self._state = None
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self.update()
@ -112,18 +124,24 @@ class DHTSensor(Entity):
def update(self):
"""Get the latest data from the DHT and updates the states."""
self.dht_client.update()
temperature_offset = self.temperature_offset
humidity_offset = self.humidity_offset
data = self.dht_client.data
if self.type == SENSOR_TEMPERATURE:
temperature = round(data[SENSOR_TEMPERATURE], 1)
temperature = data[SENSOR_TEMPERATURE]
_LOGGER.debug("Temperature %.1f \u00b0C + offset %.1f",
temperature, temperature_offset)
if (temperature >= -20) and (temperature < 80):
self._state = temperature
self._state = round(temperature + temperature_offset, 1)
if self.temp_unit == TEMP_FAHRENHEIT:
self._state = round(celsius_to_fahrenheit(temperature), 1)
elif self.type == SENSOR_HUMIDITY:
humidity = round(data[SENSOR_HUMIDITY], 1)
humidity = data[SENSOR_HUMIDITY]
_LOGGER.debug("Humidity %.1f%% + offset %.1f",
humidity, humidity_offset)
if (humidity >= 0) and (humidity <= 100):
self._state = humidity
self._state = round(humidity + humidity_offset, 1)
class DHTClient(object):