Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DHT support for humidity and temperature offset #8238

Merged
merged 8 commits into from
Jul 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions homeassistant/components/sensor/dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

CONF_PIN = 'pin'
CONF_SENSOR = 'sensor'
CONF_HUMIDITY_OFFSET = 'humidity_offset'
CONF_TEMPERATURE_OFFSET = 'temperature_offset'

DEFAULT_NAME = 'DHT Sensor'

Expand All @@ -45,6 +47,10 @@
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))
})


Expand All @@ -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")
Expand All @@ -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

Expand All @@ -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()
Expand All @@ -112,18 +124,24 @@ def unit_of_measurement(self):
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):
Expand Down