Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce overhead to update unifiprotect entity #93725

Merged
merged 3 commits into from
May 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 17 additions & 16 deletions homeassistant/components/unifiprotect/entity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Shared Entity definition for UniFi Protect Integration."""
from __future__ import annotations

from collections.abc import Sequence
from collections.abc import Callable, Sequence
import logging
from typing import Any

Expand Down Expand Up @@ -191,6 +191,9 @@ def __init__(
super().__init__()
self.data: ProtectData = data
self.device = device
self._async_get_ufp_enabled: Callable[
[ProtectAdoptableDeviceModel], bool
] | None = None

if description is None:
self._attr_unique_id = f"{self.device.mac}"
Expand All @@ -200,6 +203,8 @@ def __init__(
self._attr_unique_id = f"{self.device.mac}_{description.key}"
name = description.name or ""
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._async_set_device_info()
Expand Down Expand Up @@ -227,24 +232,20 @@ def _async_set_device_info(self) -> None:
@callback
def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None:
"""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

is_connected = self.data.last_update_success and (
self.device.state == StateType.CONNECTED
or (not self.device.is_adopted_by_us and self.device.can_adopt)
)
if (
hasattr(self, "entity_description")
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
async_get_ufp_enabled = self._async_get_ufp_enabled
self._attr_available = (
last_update_success
and (
device.state == StateType.CONNECTED
or (not device.is_adopted_by_us and device.can_adopt)
)
self._attr_available = is_connected
and (not async_get_ufp_enabled or async_get_ufp_enabled(device))
)

@callback
def _async_updated_event(self, device: ProtectModelWithId) -> None:
Expand Down