Limit calls in UniFi to write state (#85248)

Limit calls to write state to when relevant
This commit is contained in:
Robert Svensson 2023-01-05 21:38:24 +01:00 committed by GitHub
parent cc3c5772c5
commit 9c689d757c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 8 deletions

View File

@ -65,6 +65,7 @@ class UnifiEntity(Entity, Generic[HandlerT, DataT]):
self.entity_description = description
self._removed = False
self._write_state = False
self._attr_available = description.available_fn(controller, obj_id)
self._attr_device_info = description.device_info_fn(controller.api, obj_id)
@ -117,9 +118,14 @@ class UnifiEntity(Entity, Generic[HandlerT, DataT]):
self.hass.async_create_task(self.remove_item({self._obj_id}))
return
self._attr_available = description.available_fn(self.controller, self._obj_id)
if (
available := description.available_fn(self.controller, self._obj_id)
) != self.available:
self._attr_available = available
self._write_state = True
self.async_update_state(event, obj_id)
self.async_write_ha_state()
if self._write_state:
self.async_write_ha_state()
@callback
def async_signal_reachable_callback(self) -> None:

View File

@ -253,11 +253,19 @@ class UnifiSensorEntity(SensorEntity, Generic[_HandlerT, _DataT]):
self.hass.async_create_task(self.remove_item({self._obj_id}))
return
update_state = False
obj = description.object_fn(self.controller.api, self._obj_id)
if (value := description.value_fn(self.controller, obj)) != self.native_value:
self._attr_native_value = value
self._attr_available = description.available_fn(self.controller, self._obj_id)
self.async_write_ha_state()
update_state = True
if (
available := description.available_fn(self.controller, self._obj_id)
) != self.available:
self._attr_available = available
update_state = True
if update_state:
self.async_write_ha_state()
@callback
def async_signal_reachable_callback(self) -> None:

View File

@ -410,11 +410,20 @@ class UnifiSwitchEntity(SwitchEntity, Generic[_HandlerT, _DataT]):
self.hass.async_create_task(self.remove_item({self._obj_id}))
return
update_state = False
if not description.only_event_for_state_change:
obj = description.object_fn(self.controller.api, self._obj_id)
self._attr_is_on = description.is_on_fn(self.controller.api, obj)
self._attr_available = description.available_fn(self.controller, self._obj_id)
self.async_write_ha_state()
if (is_on := description.is_on_fn(self.controller.api, obj)) != self.is_on:
self._attr_is_on = is_on
update_state = True
if (
available := description.available_fn(self.controller, self._obj_id)
) != self.available:
self._attr_available = available
update_state = True
if update_state:
self.async_write_ha_state()
@callback
def async_signal_reachable_callback(self) -> None:

View File

@ -163,6 +163,12 @@ class UnifiDeviceUpdateEntity(UnifiEntity[HandlerT, DataT], UpdateEntity):
description = self.entity_description
obj = description.object_fn(self.controller.api, self._obj_id)
self._attr_in_progress = description.state_fn(self.controller.api, obj)
if (
in_progress := description.state_fn(self.controller.api, obj)
) != self.in_progress:
self._attr_in_progress = in_progress
self._write_state = True
self._attr_installed_version = obj.version
self._attr_latest_version = obj.upgrade_to_firmware or obj.version
if self.installed_version != self.latest_version:
self._write_state = True