From 50ec73df86422127b52dfdae8775581e2f63896b Mon Sep 17 00:00:00 2001 From: corneyl Date: Thu, 15 Mar 2018 21:54:08 +0100 Subject: [PATCH] Force update continuous sensors when new measurement available. --- homeassistant/components/sensor/buienradar.py | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/sensor/buienradar.py b/homeassistant/components/sensor/buienradar.py index 5d74f038eaa..62d4c2898b9 100644 --- a/homeassistant/components/sensor/buienradar.py +++ b/homeassistant/components/sensor/buienradar.py @@ -174,7 +174,7 @@ class BrSensor(Entity): def __init__(self, sensor_type, client_name): """Initialize the sensor.""" - from buienradar.buienradar import (PRECIPITATION_FORECAST) + from buienradar.buienradar import (PRECIPITATION_FORECAST, CONDITION) self.client_name = client_name self._name = SENSOR_TYPES[sensor_type][0] @@ -186,6 +186,10 @@ class BrSensor(Entity): self._measured = None self._stationname = None + # All continuous sensors should be forced to be updated + self._force_update = self.type != SYMBOL and \ + not self.type.startswith(CONDITION) + if self.type.startswith(PRECIPITATION_FORECAST): self._timeframe = None @@ -198,6 +202,11 @@ class BrSensor(Entity): PRECIPITATION_FORECAST, STATIONNAME, TIMEFRAME) + # Check if we have a new measurement, + # otherwise we do not have to update the sensor + if self._measured == data.get(MEASURED): + return False + self._attribution = data.get(ATTRIBUTION) self._stationname = data.get(STATIONNAME) self._measured = data.get(MEASURED) @@ -246,18 +255,12 @@ class BrSensor(Entity): return False else: try: - new_state = data.get(FORECAST)[fcday].get(self.type[:-3]) + self._state = data.get(FORECAST)[fcday].get(self.type[:-3]) + return True except IndexError: _LOGGER.warning("No forecast for fcday=%s...", fcday) return False - if new_state != self._state: - self._state = new_state - return True - return False - - return False - if self.type == SYMBOL or self.type.startswith(CONDITION): # update weather symbol & status text condition = data.get(CONDITION, None) @@ -286,21 +289,15 @@ class BrSensor(Entity): if self.type.startswith(PRECIPITATION_FORECAST): # update nested precipitation forecast sensors nested = data.get(PRECIPITATION_FORECAST) - new_state = nested.get(self.type[len(PRECIPITATION_FORECAST)+1:]) self._timeframe = nested.get(TIMEFRAME) # pylint: disable=protected-access - if new_state != self._state: - self._state = new_state - return True - return False + self._state = nested.get(self.type[len(PRECIPITATION_FORECAST)+1:]) + return True # update all other sensors - new_state = data.get(self.type) # pylint: disable=protected-access - if new_state != self._state: - self._state = new_state - return True - return False + self._state = data.get(self.type) + return True @property def attribution(self): @@ -360,6 +357,10 @@ class BrSensor(Entity): """Return possible sensor specific icon.""" return SENSOR_TYPES[self.type][2] + @property + def force_update(self): + """Return true for continuous sensors, false for discrete sensors""" + return self._force_update class BrData(object): """Get the latest data and updates the states."""