Skip to content
Snippets Groups Projects
Commit 7a52bbdf authored by Alexei Chetroi's avatar Alexei Chetroi Committed by Pascal Vizeli
Browse files

Allow only_cache parameter in zha.safe_read() (#16553)

* Allow only_cache parameter in zha.safe_read()

* Use cache_only for binary_sensor.zha initial update.

* Use cache_only for fan.zha initial update.

* Use cache_only for sensor.zha initial update.

* Use cache_only for switch.zha initial update.

* Use cache_only for light.zha initial update.

* Refactor cached only read in zha platform.
parent f2203e52
No related branches found
No related tags found
No related merge requests found
...@@ -132,7 +132,8 @@ class BinarySensor(zha.Entity, BinarySensorDevice): ...@@ -132,7 +132,8 @@ class BinarySensor(zha.Entity, BinarySensorDevice):
result = await zha.safe_read(self._endpoint.ias_zone, result = await zha.safe_read(self._endpoint.ias_zone,
['zone_status'], ['zone_status'],
allow_cache=False) allow_cache=False,
only_cache=(not self._initialized))
state = result.get('zone_status', self._state) state = result.get('zone_status', self._state)
if isinstance(state, (int, uint16_t)): if isinstance(state, (int, uint16_t)):
self._state = result.get('zone_status', self._state) & 3 self._state = result.get('zone_status', self._state) & 3
......
...@@ -101,7 +101,9 @@ class ZhaFan(zha.Entity, FanEntity): ...@@ -101,7 +101,9 @@ class ZhaFan(zha.Entity, FanEntity):
async def async_update(self): async def async_update(self):
"""Retrieve latest state.""" """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) new_value = result.get('fan_mode', None)
self._state = VALUE_TO_SPEED.get(new_value, None) self._state = VALUE_TO_SPEED.get(new_value, None)
......
...@@ -154,23 +154,31 @@ class Light(zha.Entity, light.Light): ...@@ -154,23 +154,31 @@ class Light(zha.Entity, light.Light):
async def async_update(self): async def async_update(self):
"""Retrieve latest state.""" """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) self._state = result.get('on_off', self._state)
if self._supported_features & light.SUPPORT_BRIGHTNESS: if self._supported_features & light.SUPPORT_BRIGHTNESS:
result = await zha.safe_read(self._endpoint.level, 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) self._brightness = result.get('current_level', self._brightness)
if self._supported_features & light.SUPPORT_COLOR_TEMP: if self._supported_features & light.SUPPORT_COLOR_TEMP:
result = await zha.safe_read(self._endpoint.light_color, 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 = result.get('color_temperature',
self._color_temp) self._color_temp)
if self._supported_features & light.SUPPORT_COLOR: if self._supported_features & light.SUPPORT_COLOR:
result = await zha.safe_read(self._endpoint.light_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: if 'current_x' in result and 'current_y' in result:
xy_color = (round(result['current_x']/65535, 3), xy_color = (round(result['current_x']/65535, 3),
round(result['current_y']/65535, 3)) round(result['current_y']/65535, 3))
......
...@@ -95,7 +95,9 @@ class Sensor(zha.Entity): ...@@ -95,7 +95,9 @@ class Sensor(zha.Entity):
"""Retrieve latest state.""" """Retrieve latest state."""
result = await zha.safe_read( result = await zha.safe_read(
list(self._in_clusters.values())[0], 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) self._state = result.get(self.value_attribute, self._state)
...@@ -224,7 +226,6 @@ class ElectricalMeasurementSensor(Sensor): ...@@ -224,7 +226,6 @@ class ElectricalMeasurementSensor(Sensor):
_LOGGER.debug("%s async_update", self.entity_id) _LOGGER.debug("%s async_update", self.entity_id)
result = await zha.safe_read( result = await zha.safe_read(
self._endpoint.electrical_measurement, self._endpoint.electrical_measurement, ['active_power'],
['active_power'], allow_cache=False, only_cache=(not self._initialized))
allow_cache=False)
self._state = result.get('active_power', self._state) self._state = result.get('active_power', self._state)
...@@ -86,5 +86,7 @@ class Switch(zha.Entity, SwitchDevice): ...@@ -86,5 +86,7 @@ class Switch(zha.Entity, SwitchDevice):
async def async_update(self): async def async_update(self):
"""Retrieve latest state.""" """Retrieve latest state."""
result = await zha.safe_read(self._endpoint.on_off, 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) self._state = result.get('on_off', self._state)
...@@ -336,6 +336,7 @@ class Entity(entity.Entity): ...@@ -336,6 +336,7 @@ class Entity(entity.Entity):
self._in_listeners = {} self._in_listeners = {}
self._out_listeners = {} self._out_listeners = {}
self._initialized = False
application_listener.register_entity(ieee, self) application_listener.register_entity(ieee, self)
async def async_added_to_hass(self): async def async_added_to_hass(self):
...@@ -348,6 +349,8 @@ class Entity(entity.Entity): ...@@ -348,6 +349,8 @@ class Entity(entity.Entity):
for cluster_id, cluster in self._out_clusters.items(): for cluster_id, cluster in self._out_clusters.items():
cluster.add_listener(self._out_listeners.get(cluster_id, self)) cluster.add_listener(self._out_listeners.get(cluster_id, self))
self._initialized = True
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
"""Return a unique ID.""" """Return a unique ID."""
...@@ -384,7 +387,7 @@ def get_discovery_info(hass, discovery_info): ...@@ -384,7 +387,7 @@ def get_discovery_info(hass, discovery_info):
return all_discovery_info.get(discovery_key, None) 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. """Swallow all exceptions from network read.
If we throw during initialization, setup fails. Rather have an entity that 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): ...@@ -395,6 +398,7 @@ async def safe_read(cluster, attributes, allow_cache=True):
result, _ = await cluster.read_attributes( result, _ = await cluster.read_attributes(
attributes, attributes,
allow_cache=allow_cache, allow_cache=allow_cache,
only_cache=only_cache
) )
return result return result
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment