From da3f2e6c769df3280ed276f3d8a103a8a814eff7 Mon Sep 17 00:00:00 2001 From: gitmopp Date: Tue, 22 Aug 2017 23:12:52 +0200 Subject: [PATCH] DHT Catch KeyError During the start of homeassistant the DHT sensor sometimes does not return data. Error message: File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/sensor/dht.py", line 139, in update humidity = data[SENSOR_HUMIDITY] KeyError: 'humidity' This patch will log the error and exit the function. During the next run the sensor is ready to deliver the data. --- homeassistant/components/sensor/dht.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sensor/dht.py b/homeassistant/components/sensor/dht.py index 8fa34d50137f80..13407ae7cd7791 100644 --- a/homeassistant/components/sensor/dht.py +++ b/homeassistant/components/sensor/dht.py @@ -128,7 +128,11 @@ def update(self): data = self.dht_client.data if self.type == SENSOR_TEMPERATURE: - temperature = data[SENSOR_TEMPERATURE] + try: + temperature = data[SENSOR_TEMPERATURE] + except KeyError: + _LOGGER.error("Error reading temperature data") + return _LOGGER.debug("Temperature %.1f \u00b0C + offset %.1f", temperature, temperature_offset) if (temperature >= -20) and (temperature < 80): @@ -136,7 +140,11 @@ def update(self): if self.temp_unit == TEMP_FAHRENHEIT: self._state = round(celsius_to_fahrenheit(temperature), 1) elif self.type == SENSOR_HUMIDITY: - humidity = data[SENSOR_HUMIDITY] + try: + humidity = data[SENSOR_HUMIDITY] + except KeyError: + _LOGGER.error("Error reading humidity data") + return _LOGGER.debug("Humidity %.1f%% + offset %.1f", humidity, humidity_offset) if (humidity >= 0) and (humidity <= 100):