diff --git a/homeassistant/components/binary_sensor/zha.py b/homeassistant/components/binary_sensor/zha.py index 0842ab7db6ea8fad61557b6a183e11fc6e925bc5..aa07a673c973d2b7e39ef0e2785a26b860899c06 100644 --- a/homeassistant/components/binary_sensor/zha.py +++ b/homeassistant/components/binary_sensor/zha.py @@ -132,7 +132,8 @@ class BinarySensor(zha.Entity, BinarySensorDevice): result = await zha.safe_read(self._endpoint.ias_zone, ['zone_status'], - allow_cache=False) + allow_cache=False, + only_cache=(not self._initialized)) state = result.get('zone_status', self._state) if isinstance(state, (int, uint16_t)): self._state = result.get('zone_status', self._state) & 3 diff --git a/homeassistant/components/fan/zha.py b/homeassistant/components/fan/zha.py index d86e0c3c99b5fd0cb6f214f8e555175cfb71f717..b5615f18d730eda37691974ae77162f079981965 100644 --- a/homeassistant/components/fan/zha.py +++ b/homeassistant/components/fan/zha.py @@ -101,7 +101,9 @@ class ZhaFan(zha.Entity, FanEntity): async def async_update(self): """Retrieve latest state.""" - result = await zha.safe_read(self._endpoint.fan, ['fan_mode']) + result = await zha.safe_read(self._endpoint.fan, ['fan_mode'], + allow_cache=False, + only_cache=(not self._initialized)) new_value = result.get('fan_mode', None) self._state = VALUE_TO_SPEED.get(new_value, None) diff --git a/homeassistant/components/light/zha.py b/homeassistant/components/light/zha.py index dc5c4977944d0dbe4ef865424533a673ff8106d1..dd675ee674deae3e9aeffea99197aa3cc7f94862 100644 --- a/homeassistant/components/light/zha.py +++ b/homeassistant/components/light/zha.py @@ -154,23 +154,31 @@ class Light(zha.Entity, light.Light): async def async_update(self): """Retrieve latest state.""" - result = await zha.safe_read(self._endpoint.on_off, ['on_off']) + result = await zha.safe_read(self._endpoint.on_off, ['on_off'], + allow_cache=False, + only_cache=(not self._initialized)) self._state = result.get('on_off', self._state) if self._supported_features & light.SUPPORT_BRIGHTNESS: result = await zha.safe_read(self._endpoint.level, - ['current_level']) + ['current_level'], + allow_cache=False, + only_cache=(not self._initialized)) self._brightness = result.get('current_level', self._brightness) if self._supported_features & light.SUPPORT_COLOR_TEMP: result = await zha.safe_read(self._endpoint.light_color, - ['color_temperature']) + ['color_temperature'], + allow_cache=False, + only_cache=(not self._initialized)) self._color_temp = result.get('color_temperature', self._color_temp) if self._supported_features & light.SUPPORT_COLOR: result = await zha.safe_read(self._endpoint.light_color, - ['current_x', 'current_y']) + ['current_x', 'current_y'], + allow_cache=False, + only_cache=(not self._initialized)) if 'current_x' in result and 'current_y' in result: xy_color = (round(result['current_x']/65535, 3), round(result['current_y']/65535, 3)) diff --git a/homeassistant/components/sensor/zha.py b/homeassistant/components/sensor/zha.py index 0a6710f8f6cec9c200d429502395fa4ad38b9677..9962270dfd76af616a04bbbbe597fc8b2ce0367a 100644 --- a/homeassistant/components/sensor/zha.py +++ b/homeassistant/components/sensor/zha.py @@ -95,7 +95,9 @@ class Sensor(zha.Entity): """Retrieve latest state.""" result = await zha.safe_read( list(self._in_clusters.values())[0], - [self.value_attribute] + [self.value_attribute], + allow_cache=False, + only_cache=(not self._initialized) ) self._state = result.get(self.value_attribute, self._state) @@ -224,7 +226,6 @@ class ElectricalMeasurementSensor(Sensor): _LOGGER.debug("%s async_update", self.entity_id) result = await zha.safe_read( - self._endpoint.electrical_measurement, - ['active_power'], - allow_cache=False) + self._endpoint.electrical_measurement, ['active_power'], + allow_cache=False, only_cache=(not self._initialized)) self._state = result.get('active_power', self._state) diff --git a/homeassistant/components/switch/zha.py b/homeassistant/components/switch/zha.py index 7ac93180cd770393253feedc4769b7a7977f93d2..4fde35653482cba23ec67bcec0f55cf9749b970d 100644 --- a/homeassistant/components/switch/zha.py +++ b/homeassistant/components/switch/zha.py @@ -86,5 +86,7 @@ class Switch(zha.Entity, SwitchDevice): async def async_update(self): """Retrieve latest state.""" result = await zha.safe_read(self._endpoint.on_off, - ['on_off']) + ['on_off'], + allow_cache=False, + only_cache=(not self._initialized)) self._state = result.get('on_off', self._state) diff --git a/homeassistant/components/zha/__init__.py b/homeassistant/components/zha/__init__.py index 4c1109f685da8035bb35b6c31c0ca5fe6ee2ab2f..f6b5868ff81989c676fb52e11bfc73134205b5dc 100644 --- a/homeassistant/components/zha/__init__.py +++ b/homeassistant/components/zha/__init__.py @@ -336,6 +336,7 @@ class Entity(entity.Entity): self._in_listeners = {} self._out_listeners = {} + self._initialized = False application_listener.register_entity(ieee, self) async def async_added_to_hass(self): @@ -348,6 +349,8 @@ class Entity(entity.Entity): for cluster_id, cluster in self._out_clusters.items(): cluster.add_listener(self._out_listeners.get(cluster_id, self)) + self._initialized = True + @property def unique_id(self) -> str: """Return a unique ID.""" @@ -384,7 +387,7 @@ def get_discovery_info(hass, discovery_info): return all_discovery_info.get(discovery_key, None) -async def safe_read(cluster, attributes, allow_cache=True): +async def safe_read(cluster, attributes, allow_cache=True, only_cache=False): """Swallow all exceptions from network read. If we throw during initialization, setup fails. Rather have an entity that @@ -395,6 +398,7 @@ async def safe_read(cluster, attributes, allow_cache=True): result, _ = await cluster.read_attributes( attributes, allow_cache=allow_cache, + only_cache=only_cache ) return result except Exception: # pylint: disable=broad-except