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

Conversation

Projects
None yet
5 participants
@gitmopp
Copy link
Contributor

commented Jun 28, 2017

Description:

DHT support for humidity and temperature offset

**Pull request in home-assistant/home-assistant.io#2899

Example entry for configuration.yaml (if applicable):

sensor:
   - platform: dht
     sensor: DHT22
     pin: 4
     temperature_offset: 2.1
     humidity_offset: -3.2
     monitored_conditions:
       - temperature
       - humidity
 

gitmopp added some commits Jun 28, 2017

Added support for temperature_offset and humidity_offset
Some DHT sensors require some offsets to work.
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)
if (humidity >= 0) and (humidity <= 100):
self._state = humidity
self._state = round(humidity + humidity_offset, 1)
_LOGGER.debug("Sensor reported humidity %s%% + humidity_offset %s", humidity, humidity_offset)

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jun 28, 2017

line too long (110 > 79 characters)

data = self.dht_client.data

if self.type == SENSOR_TEMPERATURE:
temperature = round(data[SENSOR_TEMPERATURE], 1)
if (temperature >= -20) and (temperature < 80):
self._state = temperature
self._state = round(temperature + temperature_offset, 1)
_LOGGER.debug("Sensor reported temperature %s \u00b0C + temperature_offset %s", temperature, temperature_offset)

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jun 28, 2017

line too long (128 > 79 characters)

@@ -83,13 +88,15 @@ 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):

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jun 28, 2017

line too long (102 > 79 characters)

@@ -73,7 +78,7 @@ 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))

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jun 28, 2017

line too long (102 > 79 characters)

@@ -45,6 +46,8 @@
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=-500, max=1000)),
vol.Optional(CONF_HUMIDITY_OFFSET, default=0): vol.All(vol.Coerce(float), vol.Range(min=-100, max=100))

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jun 28, 2017

line too long (107 > 79 characters)

@@ -45,6 +46,8 @@
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=-500, max=1000)),

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jun 28, 2017

line too long (112 > 79 characters)

@@ -83,13 +91,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):

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jun 28, 2017

continuation line under-indented for visual indent

@@ -73,7 +80,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))

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jun 28, 2017

continuation line unaligned for hanging indent

@@ -45,6 +46,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=-500, max=1000)),

This comment has been minimized.

Copy link
@fabaff

fabaff Jun 29, 2017

Member

Remove the range. It doesn't make much sense to allow -500 °C while the absolute zero point is -273.15 °C. As far as I remember is the measurement range of a DHT11 something like 0-50 ℃

This comment has been minimized.

Copy link
@gitmopp

gitmopp Jul 9, 2017

Author Contributor

It can measure between -20 and 80°C. What are good offsets? -100/+100?

This comment has been minimized.

Copy link
@fabaff

fabaff Jul 11, 2017

Member

Or remove the ranges.

vol.Optional(CONF_TEMPERATURE_OFFSET, default=0):
vol.All(vol.Coerce(float), vol.Range(min=-500, max=1000)),
vol.Optional(CONF_HUMIDITY_OFFSET, default=0):
vol.All(vol.Coerce(float), vol.Range(min=-100, max=100))

This comment has been minimized.

Copy link
@fabaff

fabaff Jun 29, 2017

Member

Here too. If you need to do wired calculation then a template sensor is the better place.

data = self.dht_client.data

if self.type == SENSOR_TEMPERATURE:
temperature = round(data[SENSOR_TEMPERATURE], 1)
if (temperature >= -20) and (temperature < 80):
self._state = temperature
self._state = round(temperature + temperature_offset, 1)

This comment has been minimized.

Copy link
@fabaff

fabaff Jun 29, 2017

Member

temperature is already rounded here. One rounding operation would be enough.

This comment has been minimized.

Copy link
@gitmopp

gitmopp Jul 9, 2017

Author Contributor

Without this round, in some situations adding the temperature_offset leads to a long floating number. So the temperature has more than one decimal numbers after the comma.

This comment has been minimized.

Copy link
@fabaff

fabaff Jul 11, 2017

Member

Still, then the rounding of temperature in the first place can be avoided.

This comment has been minimized.

Copy link
@gitmopp

gitmopp Jul 11, 2017

Author Contributor

This time I got your point. I removed the first round for temperature and humidity and added a _LOGGER.debug it might help in case of problems.

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)
if (humidity >= 0) and (humidity <= 100):
self._state = humidity
self._state = round(humidity + humidity_offset, 1)

This comment has been minimized.

Copy link
@fabaff

This comment has been minimized.

Copy link
@gitmopp

gitmopp Jul 9, 2017

Author Contributor

The same reason as above.

@@ -147,6 +148,7 @@
CONF_STATE = 'state'
CONF_STRUCTURE = 'structure'
CONF_SWITCHES = 'switches'
CONF_TEMPERATURE_OFFSET = 'temperature_offset'

This comment has been minimized.

Copy link
@fabaff

fabaff Jun 29, 2017

Member

Both constants are platform-specific. Please move them to the dht platform.

This comment has been minimized.

Copy link
@gitmopp

gitmopp Jun 29, 2017

Author Contributor

Thank you for the hints, I will fix it in a couple of days.

gitmopp added some commits Jul 9, 2017

Removed the const
const.py is at original state.
@gitmopp
Copy link
Contributor Author

left a comment

Applied the requested changes.

@fabaff

fabaff approved these changes Jul 11, 2017

@fabaff fabaff merged commit 471501d into home-assistant:dev Jul 11, 2017

4 checks passed

cla-bot Everyone involved has signed the CLA
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage decreased (-0.08%) to 93.589%
Details
hound No violations found. Woof!

@balloob balloob referenced this pull request Jul 13, 2017

Merged

0.49 #8468

dethpickle added a commit to dethpickle/home-assistant that referenced this pull request Aug 18, 2017

DHT support for humidity and temperature offset (home-assistant#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

@home-assistant home-assistant locked and limited conversation to collaborators Oct 20, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.