mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Reduce overhead to update unifiprotect entity (#93725)
This commit is contained in:
parent
8053073a77
commit
17fadbcf4a
@ -1,7 +1,7 @@
|
|||||||
"""Shared Entity definition for UniFi Protect Integration."""
|
"""Shared Entity definition for UniFi Protect Integration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Sequence
|
from collections.abc import Callable, Sequence
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -191,6 +191,9 @@ class ProtectDeviceEntity(Entity):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.data: ProtectData = data
|
self.data: ProtectData = data
|
||||||
self.device = device
|
self.device = device
|
||||||
|
self._async_get_ufp_enabled: Callable[
|
||||||
|
[ProtectAdoptableDeviceModel], bool
|
||||||
|
] | None = None
|
||||||
|
|
||||||
if description is None:
|
if description is None:
|
||||||
self._attr_unique_id = f"{self.device.mac}"
|
self._attr_unique_id = f"{self.device.mac}"
|
||||||
@ -200,6 +203,8 @@ class ProtectDeviceEntity(Entity):
|
|||||||
self._attr_unique_id = f"{self.device.mac}_{description.key}"
|
self._attr_unique_id = f"{self.device.mac}_{description.key}"
|
||||||
name = description.name or ""
|
name = description.name or ""
|
||||||
self._attr_name = f"{self.device.display_name} {name.title()}"
|
self._attr_name = f"{self.device.display_name} {name.title()}"
|
||||||
|
if isinstance(description, ProtectRequiredKeysMixin):
|
||||||
|
self._async_get_ufp_enabled = description.get_ufp_enabled
|
||||||
|
|
||||||
self._attr_attribution = DEFAULT_ATTRIBUTION
|
self._attr_attribution = DEFAULT_ATTRIBUTION
|
||||||
self._async_set_device_info()
|
self._async_set_device_info()
|
||||||
@ -227,24 +232,20 @@ class ProtectDeviceEntity(Entity):
|
|||||||
@callback
|
@callback
|
||||||
def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None:
|
def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None:
|
||||||
"""Update Entity object from Protect device."""
|
"""Update Entity object from Protect device."""
|
||||||
if self.data.last_update_success:
|
assert isinstance(device, ProtectAdoptableDeviceModel)
|
||||||
assert isinstance(device, ProtectAdoptableDeviceModel)
|
|
||||||
|
if last_update_success := self.data.last_update_success:
|
||||||
self.device = device
|
self.device = device
|
||||||
|
|
||||||
is_connected = self.data.last_update_success and (
|
async_get_ufp_enabled = self._async_get_ufp_enabled
|
||||||
self.device.state == StateType.CONNECTED
|
self._attr_available = (
|
||||||
or (not self.device.is_adopted_by_us and self.device.can_adopt)
|
last_update_success
|
||||||
)
|
and (
|
||||||
if (
|
device.state == StateType.CONNECTED
|
||||||
hasattr(self, "entity_description")
|
or (not device.is_adopted_by_us and device.can_adopt)
|
||||||
and self.entity_description is not None
|
|
||||||
and hasattr(self.entity_description, "get_ufp_enabled")
|
|
||||||
):
|
|
||||||
assert isinstance(self.entity_description, ProtectRequiredKeysMixin)
|
|
||||||
is_connected = is_connected and self.entity_description.get_ufp_enabled(
|
|
||||||
self.device
|
|
||||||
)
|
)
|
||||||
self._attr_available = is_connected
|
and (not async_get_ufp_enabled or async_get_ufp_enabled(device))
|
||||||
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_updated_event(self, device: ProtectModelWithId) -> None:
|
def _async_updated_event(self, device: ProtectModelWithId) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user