2021-12-26 06:12:57 +00:00
|
|
|
"""Shared Entity definition for UniFi Protect Integration."""
|
2024-03-08 15:35:23 +00:00
|
|
|
|
2021-12-26 06:12:57 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-05-29 18:39:34 +00:00
|
|
|
from collections.abc import Callable, Sequence
|
2024-06-14 19:29:18 +00:00
|
|
|
from functools import partial
|
2022-01-01 21:23:10 +00:00
|
|
|
import logging
|
2024-06-14 19:29:18 +00:00
|
|
|
from operator import attrgetter
|
2024-06-16 02:02:03 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2022-01-01 21:23:10 +00:00
|
|
|
|
2024-06-09 23:25:39 +00:00
|
|
|
from uiprotect.data import (
|
2022-05-20 01:34:58 +00:00
|
|
|
NVR,
|
2022-01-08 23:51:49 +00:00
|
|
|
Event,
|
2022-01-01 21:23:10 +00:00
|
|
|
ModelType,
|
|
|
|
ProtectAdoptableDeviceModel,
|
2022-06-21 03:52:41 +00:00
|
|
|
ProtectModelWithId,
|
2022-01-01 21:23:10 +00:00
|
|
|
StateType,
|
|
|
|
)
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
from homeassistant.core import callback
|
|
|
|
import homeassistant.helpers.device_registry as dr
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity import Entity, EntityDescription
|
2021-12-26 06:12:57 +00:00
|
|
|
|
2022-11-28 19:07:53 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_EVENT_ID,
|
|
|
|
ATTR_EVENT_SCORE,
|
|
|
|
DEFAULT_ATTRIBUTION,
|
|
|
|
DEFAULT_BRAND,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2021-12-26 06:12:57 +00:00
|
|
|
from .data import ProtectData
|
2024-06-16 02:02:03 +00:00
|
|
|
from .models import PermRequired, ProtectEntityDescription, ProtectEventMixin
|
2022-01-01 21:23:10 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_device_entities(
|
|
|
|
data: ProtectData,
|
2024-06-12 20:23:18 +00:00
|
|
|
klass: type[BaseProtectEntity],
|
2022-01-01 21:23:10 +00:00
|
|
|
model_type: ModelType,
|
2024-06-16 02:02:03 +00:00
|
|
|
descs: Sequence[ProtectEntityDescription],
|
|
|
|
unadopted_descs: Sequence[ProtectEntityDescription] | None = None,
|
2022-06-27 21:03:25 +00:00
|
|
|
ufp_device: ProtectAdoptableDeviceModel | None = None,
|
2024-06-12 20:23:18 +00:00
|
|
|
) -> list[BaseProtectEntity]:
|
2022-08-26 03:05:18 +00:00
|
|
|
if not descs and not unadopted_descs:
|
2022-01-01 21:23:10 +00:00
|
|
|
return []
|
|
|
|
|
2024-06-12 20:23:18 +00:00
|
|
|
entities: list[BaseProtectEntity] = []
|
2022-06-27 21:03:25 +00:00
|
|
|
devices = (
|
2022-08-26 11:46:11 +00:00
|
|
|
[ufp_device]
|
|
|
|
if ufp_device is not None
|
|
|
|
else data.get_by_types({model_type}, ignore_unadopted=False)
|
2022-06-27 21:03:25 +00:00
|
|
|
)
|
2024-06-12 16:11:59 +00:00
|
|
|
auth_user = data.api.bootstrap.auth_user
|
2022-06-27 21:03:25 +00:00
|
|
|
for device in devices:
|
2023-07-16 22:11:35 +00:00
|
|
|
if TYPE_CHECKING:
|
2024-06-12 16:11:59 +00:00
|
|
|
assert isinstance(device, ProtectAdoptableDeviceModel)
|
2022-06-22 20:57:21 +00:00
|
|
|
if not device.is_adopted_by_us:
|
2024-06-12 16:11:59 +00:00
|
|
|
if unadopted_descs:
|
|
|
|
for description in unadopted_descs:
|
|
|
|
entities.append(
|
|
|
|
klass(
|
|
|
|
data,
|
|
|
|
device=device,
|
|
|
|
description=description,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Adding %s entity %s for %s",
|
|
|
|
klass.__name__,
|
|
|
|
description.name,
|
|
|
|
device.display_name,
|
2022-08-25 23:54:52 +00:00
|
|
|
)
|
2022-06-22 20:57:21 +00:00
|
|
|
continue
|
|
|
|
|
2024-06-12 16:11:59 +00:00
|
|
|
can_write = device.can_write(auth_user)
|
2022-01-01 21:23:10 +00:00
|
|
|
for description in descs:
|
2024-06-12 16:11:59 +00:00
|
|
|
if (perms := description.ufp_perm) is not None:
|
|
|
|
if perms is PermRequired.WRITE and not can_write:
|
2022-06-21 16:17:29 +00:00
|
|
|
continue
|
2024-06-12 16:11:59 +00:00
|
|
|
if perms is PermRequired.NO_WRITE and can_write:
|
2022-06-21 17:01:06 +00:00
|
|
|
continue
|
2024-06-12 16:11:59 +00:00
|
|
|
if perms is PermRequired.DELETE and not device.can_delete(auth_user):
|
2022-08-25 23:54:52 +00:00
|
|
|
continue
|
2022-06-21 16:17:29 +00:00
|
|
|
|
2022-11-28 19:07:53 +00:00
|
|
|
if not description.has_required(device):
|
|
|
|
continue
|
2022-01-01 21:23:10 +00:00
|
|
|
|
|
|
|
entities.append(
|
|
|
|
klass(
|
|
|
|
data,
|
|
|
|
device=device,
|
|
|
|
description=description,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Adding %s entity %s for %s",
|
|
|
|
klass.__name__,
|
|
|
|
description.name,
|
2022-06-22 20:57:21 +00:00
|
|
|
device.display_name,
|
2022-01-01 21:23:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return entities
|
|
|
|
|
|
|
|
|
2024-06-12 16:11:59 +00:00
|
|
|
_ALL_MODEL_TYPES = (
|
|
|
|
ModelType.CAMERA,
|
|
|
|
ModelType.LIGHT,
|
|
|
|
ModelType.SENSOR,
|
|
|
|
ModelType.VIEWPORT,
|
|
|
|
ModelType.DOORLOCK,
|
|
|
|
ModelType.CHIME,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _combine_model_descs(
|
|
|
|
model_type: ModelType,
|
2024-06-16 02:02:03 +00:00
|
|
|
model_descriptions: dict[ModelType, Sequence[ProtectEntityDescription]] | None,
|
|
|
|
all_descs: Sequence[ProtectEntityDescription] | None,
|
|
|
|
) -> list[ProtectEntityDescription]:
|
2024-06-12 16:11:59 +00:00
|
|
|
"""Combine all the descriptions with descriptions a model type."""
|
2024-06-16 02:02:03 +00:00
|
|
|
descs: list[ProtectEntityDescription] = list(all_descs) if all_descs else []
|
2024-06-12 16:11:59 +00:00
|
|
|
if model_descriptions and (model_descs := model_descriptions.get(model_type)):
|
|
|
|
descs.extend(model_descs)
|
|
|
|
return descs
|
|
|
|
|
|
|
|
|
2022-01-01 21:23:10 +00:00
|
|
|
@callback
|
|
|
|
def async_all_device_entities(
|
|
|
|
data: ProtectData,
|
2024-06-12 20:23:18 +00:00
|
|
|
klass: type[BaseProtectEntity],
|
2024-06-16 02:02:03 +00:00
|
|
|
model_descriptions: dict[ModelType, Sequence[ProtectEntityDescription]]
|
2024-06-12 16:11:59 +00:00
|
|
|
| None = None,
|
2024-06-16 02:02:03 +00:00
|
|
|
all_descs: Sequence[ProtectEntityDescription] | None = None,
|
|
|
|
unadopted_descs: list[ProtectEntityDescription] | None = None,
|
2022-06-27 21:03:25 +00:00
|
|
|
ufp_device: ProtectAdoptableDeviceModel | None = None,
|
2024-06-12 20:23:18 +00:00
|
|
|
) -> list[BaseProtectEntity]:
|
2022-01-01 21:23:10 +00:00
|
|
|
"""Generate a list of all the device entities."""
|
2022-06-27 21:03:25 +00:00
|
|
|
if ufp_device is None:
|
2024-06-12 20:23:18 +00:00
|
|
|
entities: list[BaseProtectEntity] = []
|
2024-06-12 16:11:59 +00:00
|
|
|
for model_type in _ALL_MODEL_TYPES:
|
|
|
|
descs = _combine_model_descs(model_type, model_descriptions, all_descs)
|
|
|
|
entities.extend(
|
|
|
|
_async_device_entities(data, klass, model_type, descs, unadopted_descs)
|
2022-08-25 23:54:52 +00:00
|
|
|
)
|
2024-06-12 16:11:59 +00:00
|
|
|
return entities
|
|
|
|
|
|
|
|
device_model_type = ufp_device.model
|
|
|
|
assert device_model_type is not None
|
|
|
|
descs = _combine_model_descs(device_model_type, model_descriptions, all_descs)
|
2022-08-25 23:54:52 +00:00
|
|
|
return _async_device_entities(
|
2024-06-12 16:11:59 +00:00
|
|
|
data, klass, device_model_type, descs, unadopted_descs, ufp_device
|
2022-08-25 23:54:52 +00:00
|
|
|
)
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
|
2024-06-12 20:23:18 +00:00
|
|
|
class BaseProtectEntity(Entity):
|
2021-12-26 06:12:57 +00:00
|
|
|
"""Base class for UniFi protect entities."""
|
|
|
|
|
2024-06-12 20:23:18 +00:00
|
|
|
device: ProtectAdoptableDeviceModel | NVR
|
2022-01-10 04:37:24 +00:00
|
|
|
|
2021-12-26 06:12:57 +00:00
|
|
|
_attr_should_poll = False
|
2024-06-16 02:02:03 +00:00
|
|
|
_attr_attribution = DEFAULT_ATTRIBUTION
|
2024-06-14 19:29:18 +00:00
|
|
|
_state_attrs: tuple[str, ...] = ("_attr_available",)
|
2024-06-16 14:00:14 +00:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
_async_get_ufp_enabled: Callable[[ProtectAdoptableDeviceModel], bool] | None = None
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
data: ProtectData,
|
2024-06-12 20:23:18 +00:00
|
|
|
device: ProtectAdoptableDeviceModel | NVR,
|
2021-12-26 06:12:57 +00:00
|
|
|
description: EntityDescription | None = None,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the entity."""
|
|
|
|
super().__init__()
|
2024-06-16 14:00:14 +00:00
|
|
|
self.data = data
|
2022-01-10 04:37:24 +00:00
|
|
|
self.device = device
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
if description is None:
|
2024-06-16 14:00:14 +00:00
|
|
|
self._attr_unique_id = self.device.mac
|
|
|
|
self._attr_name = None
|
2021-12-26 06:12:57 +00:00
|
|
|
else:
|
2022-01-10 04:37:24 +00:00
|
|
|
self.entity_description = description
|
2022-06-19 14:22:33 +00:00
|
|
|
self._attr_unique_id = f"{self.device.mac}_{description.key}"
|
2024-06-16 02:02:03 +00:00
|
|
|
if isinstance(description, ProtectEntityDescription):
|
2023-05-29 18:39:34 +00:00
|
|
|
self._async_get_ufp_enabled = description.get_ufp_enabled
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
self._async_set_device_info()
|
2022-06-21 03:52:41 +00:00
|
|
|
self._async_update_device_from_protect(device)
|
2024-06-14 19:29:18 +00:00
|
|
|
self._state_getters = tuple(
|
|
|
|
partial(attrgetter(attr), self) for attr in self._state_attrs
|
|
|
|
)
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
async def async_update(self) -> None:
|
|
|
|
"""Update the entity.
|
|
|
|
|
|
|
|
Only used by the generic entity update service.
|
|
|
|
"""
|
|
|
|
await self.data.async_refresh()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_set_device_info(self) -> None:
|
|
|
|
self._attr_device_info = DeviceInfo(
|
2022-06-22 20:57:21 +00:00
|
|
|
name=self.device.display_name,
|
2021-12-26 06:12:57 +00:00
|
|
|
manufacturer=DEFAULT_BRAND,
|
|
|
|
model=self.device.type,
|
|
|
|
via_device=(DOMAIN, self.data.api.bootstrap.nvr.mac),
|
|
|
|
sw_version=self.device.firmware_version,
|
|
|
|
connections={(dr.CONNECTION_NETWORK_MAC, self.device.mac)},
|
|
|
|
configuration_url=self.device.protect_url,
|
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
2022-06-21 03:52:41 +00:00
|
|
|
def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None:
|
2021-12-26 06:12:57 +00:00
|
|
|
"""Update Entity object from Protect device."""
|
2023-07-16 22:11:35 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
assert isinstance(device, ProtectAdoptableDeviceModel)
|
2023-05-29 18:39:34 +00:00
|
|
|
|
|
|
|
if last_update_success := self.data.last_update_success:
|
2022-06-19 14:22:33 +00:00
|
|
|
self.device = device
|
2021-12-26 06:12:57 +00:00
|
|
|
|
2023-05-29 18:39:34 +00:00
|
|
|
async_get_ufp_enabled = self._async_get_ufp_enabled
|
|
|
|
self._attr_available = (
|
|
|
|
last_update_success
|
|
|
|
and (
|
2024-01-05 12:27:10 +00:00
|
|
|
device.state is StateType.CONNECTED
|
2023-05-29 18:39:34 +00:00
|
|
|
or (not device.is_adopted_by_us and device.can_adopt)
|
2022-01-13 03:54:22 +00:00
|
|
|
)
|
2023-05-29 18:39:34 +00:00
|
|
|
and (not async_get_ufp_enabled or async_get_ufp_enabled(device))
|
|
|
|
)
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
@callback
|
2024-04-30 18:13:56 +00:00
|
|
|
def _async_updated_event(self, device: ProtectAdoptableDeviceModel | NVR) -> None:
|
2024-01-11 04:06:45 +00:00
|
|
|
"""When device is updated from Protect."""
|
2024-06-14 19:29:18 +00:00
|
|
|
previous_attrs = [getter() for getter in self._state_getters]
|
2022-06-21 03:52:41 +00:00
|
|
|
self._async_update_device_from_protect(device)
|
2024-06-14 19:29:18 +00:00
|
|
|
changed = False
|
|
|
|
for idx, getter in enumerate(self._state_getters):
|
|
|
|
if previous_attrs[idx] != getter():
|
|
|
|
changed = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if changed:
|
2024-01-11 04:06:45 +00:00
|
|
|
if _LOGGER.isEnabledFor(logging.DEBUG):
|
2024-04-30 18:13:56 +00:00
|
|
|
device_name = device.name or ""
|
2024-01-11 04:06:45 +00:00
|
|
|
if hasattr(self, "entity_description") and self.entity_description.name:
|
|
|
|
device_name += f" {self.entity_description.name}"
|
|
|
|
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Updating state [%s (%s)] %s -> %s",
|
|
|
|
device_name,
|
|
|
|
device.mac,
|
|
|
|
previous_attrs,
|
2024-06-14 19:29:18 +00:00
|
|
|
tuple((getattr(self, attr)) for attr in self._state_attrs),
|
2024-01-11 04:06:45 +00:00
|
|
|
)
|
|
|
|
self.async_write_ha_state()
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""When entity is added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self.async_on_remove(
|
|
|
|
self.data.async_subscribe_device_id(
|
2022-06-29 03:00:26 +00:00
|
|
|
self.device.mac, self._async_updated_event
|
2021-12-26 06:12:57 +00:00
|
|
|
)
|
|
|
|
)
|
2022-01-05 21:59:21 +00:00
|
|
|
|
|
|
|
|
2024-06-12 20:23:18 +00:00
|
|
|
class ProtectDeviceEntity(BaseProtectEntity):
|
|
|
|
"""Base class for UniFi protect entities."""
|
2022-01-05 21:59:21 +00:00
|
|
|
|
2024-06-12 20:23:18 +00:00
|
|
|
device: ProtectAdoptableDeviceModel
|
2022-01-10 04:37:24 +00:00
|
|
|
|
2024-06-12 20:23:18 +00:00
|
|
|
|
|
|
|
class ProtectNVREntity(BaseProtectEntity):
|
|
|
|
"""Base class for unifi protect entities."""
|
|
|
|
|
|
|
|
device: NVR
|
2022-01-05 21:59:21 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_set_device_info(self) -> None:
|
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
connections={(dr.CONNECTION_NETWORK_MAC, self.device.mac)},
|
|
|
|
identifiers={(DOMAIN, self.device.mac)},
|
|
|
|
manufacturer=DEFAULT_BRAND,
|
2022-06-22 20:57:21 +00:00
|
|
|
name=self.device.display_name,
|
2022-01-05 21:59:21 +00:00
|
|
|
model=self.device.type,
|
|
|
|
sw_version=str(self.device.version),
|
|
|
|
configuration_url=self.device.api.base_url,
|
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
2022-06-21 03:52:41 +00:00
|
|
|
def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None:
|
2023-07-16 16:24:27 +00:00
|
|
|
data = self.data
|
2024-06-16 02:02:03 +00:00
|
|
|
if last_update_success := data.last_update_success:
|
2023-07-16 16:24:27 +00:00
|
|
|
self.device = data.api.bootstrap.nvr
|
2022-01-05 21:59:21 +00:00
|
|
|
|
2023-07-16 16:24:27 +00:00
|
|
|
self._attr_available = last_update_success
|
2022-01-08 23:51:49 +00:00
|
|
|
|
|
|
|
|
2022-11-28 19:07:53 +00:00
|
|
|
class EventEntityMixin(ProtectDeviceEntity):
|
2022-01-08 23:51:49 +00:00
|
|
|
"""Adds motion event attributes to sensor."""
|
|
|
|
|
2022-11-28 19:07:53 +00:00
|
|
|
entity_description: ProtectEventMixin
|
2024-06-16 02:02:03 +00:00
|
|
|
_unrecorded_attributes = frozenset({ATTR_EVENT_ID, ATTR_EVENT_SCORE})
|
|
|
|
_event: Event | None = None
|
2022-01-08 23:51:49 +00:00
|
|
|
|
|
|
|
@callback
|
2022-06-21 03:52:41 +00:00
|
|
|
def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None:
|
2024-06-16 02:02:03 +00:00
|
|
|
if (event := self.entity_description.get_event_obj(device)) is None:
|
|
|
|
self._attr_extra_state_attributes = {}
|
|
|
|
else:
|
2023-07-16 16:24:27 +00:00
|
|
|
self._attr_extra_state_attributes = {
|
|
|
|
ATTR_EVENT_ID: event.id,
|
|
|
|
ATTR_EVENT_SCORE: event.score,
|
|
|
|
}
|
|
|
|
self._event = event
|
2022-06-21 03:52:41 +00:00
|
|
|
super()._async_update_device_from_protect(device)
|