From be8aa161708bbda46c89be9aa71262d8df4ddef4 Mon Sep 17 00:00:00 2001 From: "David F. Mulcahey" Date: Mon, 7 Sep 2020 13:52:00 -0400 Subject: [PATCH] Don't poll entities for unavailable ZHA devices (#39756) * Don't poll entities for unavailable ZHA devices * Update homeassistant/components/zha/entity.py Co-authored-by: Bas Nijholt * cleanup after accepting suggestion Co-authored-by: Bas Nijholt --- homeassistant/components/zha/entity.py | 10 +++++++--- homeassistant/components/zha/light.py | 7 ++----- homeassistant/components/zha/sensor.py | 6 ++++++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/zha/entity.py b/homeassistant/components/zha/entity.py index 309691dd3df..96f005ba288 100644 --- a/homeassistant/components/zha/entity.py +++ b/homeassistant/components/zha/entity.py @@ -202,9 +202,13 @@ class ZhaEntity(BaseZhaEntity, RestoreEntity): async def async_update(self) -> None: """Retrieve latest state.""" - for channel in self.cluster_channels.values(): - if hasattr(channel, "async_update"): - await channel.async_update() + tasks = [ + channel.async_update() + for channel in self.cluster_channels.values() + if hasattr(channel, "async_update") + ] + if tasks: + await asyncio.gather(*tasks) class ZhaGroupEntity(BaseZhaEntity): diff --git a/homeassistant/components/zha/light.py b/homeassistant/components/zha/light.py index ff080562190..ba05d63df12 100644 --- a/homeassistant/components/zha/light.py +++ b/homeassistant/components/zha/light.py @@ -410,13 +410,10 @@ class Light(BaseLight, ZhaEntity): if "effect" in last_state.attributes: self._effect = last_state.attributes["effect"] - async def async_update(self): - """Attempt to retrieve on off state from the light.""" - await super().async_update() - await self.async_get_state() - async def async_get_state(self, from_cache=True): """Attempt to retrieve on off state from the light.""" + if not from_cache and not self.available: + return self.debug("polling current state - from cache: %s", from_cache) if self._on_off_channel: state = await self._on_off_channel.get_attribute_value( diff --git a/homeassistant/components/zha/sensor.py b/homeassistant/components/zha/sensor.py index 9f507275836..6e2878f371b 100644 --- a/homeassistant/components/zha/sensor.py +++ b/homeassistant/components/zha/sensor.py @@ -210,6 +210,12 @@ class ElectricalMeasurement(Sensor): return round(value, self._decimals) return round(value) + async def async_update(self) -> None: + """Retrieve latest state.""" + if not self.available: + return + await super().async_update() + @STRICT_MATCH(generic_ids=CHANNEL_ST_HUMIDITY_CLUSTER) @STRICT_MATCH(channel_names=CHANNEL_HUMIDITY)