2022-01-01 21:23:10 +00:00
|
|
|
"""UniFi Protect Integration utils."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-06-02 15:26:08 +00:00
|
|
|
from collections.abc import Generator, Iterable
|
2022-01-31 23:27:26 +00:00
|
|
|
import contextlib
|
2022-01-01 21:23:10 +00:00
|
|
|
from enum import Enum
|
2022-01-31 23:27:26 +00:00
|
|
|
import socket
|
2022-01-01 21:23:10 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2022-06-19 14:22:33 +00:00
|
|
|
from pyunifiprotect.data import (
|
|
|
|
Bootstrap,
|
2022-06-21 17:01:06 +00:00
|
|
|
Camera,
|
|
|
|
Light,
|
|
|
|
LightModeEnableType,
|
|
|
|
LightModeType,
|
2022-06-19 14:22:33 +00:00
|
|
|
ProtectAdoptableDeviceModel,
|
|
|
|
ProtectDeviceModel,
|
2022-06-21 17:01:06 +00:00
|
|
|
VideoMode,
|
2022-06-19 14:22:33 +00:00
|
|
|
)
|
2022-06-02 15:26:08 +00:00
|
|
|
|
2022-01-31 23:27:26 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-01-18 22:38:16 +00:00
|
|
|
|
2022-06-19 14:22:33 +00:00
|
|
|
from .const import DEVICES_THAT_ADOPT, ModelType
|
2022-06-02 15:26:08 +00:00
|
|
|
|
2022-01-01 21:23:10 +00:00
|
|
|
|
|
|
|
def get_nested_attr(obj: Any, attr: str) -> Any:
|
|
|
|
"""Fetch a nested attribute."""
|
|
|
|
attrs = attr.split(".")
|
|
|
|
|
|
|
|
value = obj
|
|
|
|
for key in attrs:
|
|
|
|
if not hasattr(value, key):
|
|
|
|
return None
|
|
|
|
value = getattr(value, key)
|
|
|
|
|
|
|
|
if isinstance(value, Enum):
|
|
|
|
value = value.value
|
|
|
|
|
|
|
|
return value
|
2022-01-18 22:38:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_unifi_mac_from_hass(mac: str) -> str:
|
|
|
|
# MAC addresses in UFP are always caps
|
|
|
|
return mac.replace(":", "").upper()
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_short_mac(mac: str) -> str:
|
|
|
|
"""Get the short mac address from the full mac."""
|
|
|
|
return _async_unifi_mac_from_hass(mac)[-6:]
|
2022-01-31 23:27:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def _async_resolve(hass: HomeAssistant, host: str) -> str | None:
|
|
|
|
"""Resolve a hostname to an ip."""
|
|
|
|
with contextlib.suppress(OSError):
|
|
|
|
return next(
|
|
|
|
iter(
|
|
|
|
raw[0]
|
|
|
|
for family, _, _, _, raw in await hass.loop.getaddrinfo(
|
|
|
|
host, None, type=socket.SOCK_STREAM, proto=socket.IPPROTO_TCP
|
|
|
|
)
|
|
|
|
if family == socket.AF_INET
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
return None
|
2022-06-02 15:26:08 +00:00
|
|
|
|
|
|
|
|
2022-06-19 14:22:33 +00:00
|
|
|
@callback
|
2022-06-02 15:26:08 +00:00
|
|
|
def async_get_devices_by_type(
|
2022-06-19 14:22:33 +00:00
|
|
|
bootstrap: Bootstrap, device_type: ModelType
|
|
|
|
) -> dict[str, ProtectAdoptableDeviceModel]:
|
2022-06-02 15:26:08 +00:00
|
|
|
"""Get devices by type."""
|
|
|
|
|
|
|
|
devices: dict[str, ProtectAdoptableDeviceModel] = getattr(
|
2022-06-19 14:22:33 +00:00
|
|
|
bootstrap, f"{device_type.value}s"
|
2022-06-02 15:26:08 +00:00
|
|
|
)
|
|
|
|
return devices
|
|
|
|
|
|
|
|
|
2022-06-19 14:22:33 +00:00
|
|
|
@callback
|
|
|
|
def async_device_by_id(
|
|
|
|
bootstrap: Bootstrap,
|
|
|
|
device_id: str,
|
|
|
|
device_type: ModelType | None = None,
|
|
|
|
) -> ProtectAdoptableDeviceModel | None:
|
|
|
|
"""Get devices by type."""
|
|
|
|
|
|
|
|
device_types = DEVICES_THAT_ADOPT
|
|
|
|
if device_type is not None:
|
|
|
|
device_types = {device_type}
|
|
|
|
|
|
|
|
device = None
|
|
|
|
for model in device_types:
|
|
|
|
device = async_get_devices_by_type(bootstrap, model).get(device_id)
|
|
|
|
if device is not None:
|
|
|
|
break
|
|
|
|
return device
|
|
|
|
|
|
|
|
|
2022-06-02 15:26:08 +00:00
|
|
|
@callback
|
|
|
|
def async_get_devices(
|
2022-06-19 14:22:33 +00:00
|
|
|
bootstrap: Bootstrap, model_type: Iterable[ModelType]
|
2022-06-02 15:26:08 +00:00
|
|
|
) -> Generator[ProtectDeviceModel, None, None]:
|
|
|
|
"""Return all device by type."""
|
|
|
|
return (
|
|
|
|
device
|
|
|
|
for device_type in model_type
|
2022-06-19 14:22:33 +00:00
|
|
|
for device in async_get_devices_by_type(bootstrap, device_type).values()
|
2022-06-02 15:26:08 +00:00
|
|
|
)
|
2022-06-21 17:01:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_get_is_highfps(obj: Camera) -> bool:
|
|
|
|
"""Return if camera has High FPS mode enabled."""
|
|
|
|
|
|
|
|
return bool(obj.video_mode == VideoMode.HIGH_FPS)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_get_light_motion_current(obj: Light) -> str:
|
|
|
|
"""Get light motion mode for Flood Light."""
|
|
|
|
|
|
|
|
if (
|
|
|
|
obj.light_mode_settings.mode == LightModeType.MOTION
|
|
|
|
and obj.light_mode_settings.enable_at == LightModeEnableType.DARK
|
|
|
|
):
|
|
|
|
return f"{LightModeType.MOTION.value}Dark"
|
|
|
|
return obj.light_mode_settings.mode.value
|