1 """UniFi Protect Integration utils."""
3 from __future__
import annotations
5 from collections.abc
import Generator, Iterable
7 from pathlib
import Path
9 from typing
import TYPE_CHECKING
11 from aiohttp
import CookieJar
12 from uiprotect
import ProtectApiClient
13 from uiprotect.data
import (
19 ProtectAdoptableDeviceModel,
36 DEVICES_FOR_SUBSCRIBE,
41 from .data
import UFPConfigEntry
47 return mac.replace(
":",
"").upper()
52 """Get the short mac address from the full mac."""
57 """Resolve a hostname to an ip."""
58 with contextlib.suppress(OSError):
62 for family, _, _, _, raw
in await hass.loop.getaddrinfo(
63 host,
None, type=socket.SOCK_STREAM, proto=socket.IPPROTO_TCP
65 if family == socket.AF_INET
74 bootstrap: Bootstrap, device_type: ModelType
75 ) -> dict[str, ProtectAdoptableDeviceModel]:
76 """Get devices by type."""
77 devices: dict[str, ProtectAdoptableDeviceModel]
78 devices = getattr(bootstrap, device_type.devices_key)
84 bootstrap: Bootstrap, model_type: Iterable[ModelType]
85 ) -> Generator[ProtectAdoptableDeviceModel]:
86 """Return all device by type."""
89 for device_type
in model_type
96 """Get light motion mode for Flood Light."""
99 obj.light_mode_settings.mode
is LightModeType.MOTION
100 and obj.light_mode_settings.enable_at
is LightModeEnableType.DARK
102 return f
"{LightModeType.MOTION.value}Dark"
103 return obj.light_mode_settings.mode.value
108 hass: HomeAssistant, entry: UFPConfigEntry
109 ) -> ProtectApiClient:
110 """Create ProtectApiClient from config entry."""
113 return ProtectApiClient(
114 host=entry.data[CONF_HOST],
115 port=entry.data[CONF_PORT],
116 username=entry.data[CONF_USERNAME],
117 password=entry.data[CONF_PASSWORD],
118 verify_ssl=entry.data[CONF_VERIFY_SSL],
120 subscribed_models=DEVICES_FOR_SUBSCRIBE,
121 override_connection_host=entry.options.get(CONF_OVERRIDE_CHOST,
False),
122 ignore_stats=
not entry.options.get(CONF_ALL_UPDATES,
False),
123 ignore_unadopted=
False,
124 cache_dir=Path(hass.config.path(STORAGE_DIR,
"unifiprotect")),
125 config_dir=Path(hass.config.path(STORAGE_DIR,
"unifiprotect")),
131 """Get base name for cameras channel."""
133 camera_name = channel.name
134 if channel.name !=
"Package Camera":
135 camera_name = f
"{channel.name} resolution channel"
aiohttp.ClientSession async_create_clientsession()
str _async_unifi_mac_from_hass(str mac)
str _async_short_mac(str mac)
str|None _async_resolve(HomeAssistant hass, str host)
dict[str, ProtectAdoptableDeviceModel] async_get_devices_by_type(Bootstrap bootstrap, ModelType device_type)
str async_get_light_motion_current(Light obj)
ProtectApiClient async_create_api_client(HomeAssistant hass, UFPConfigEntry entry)
Generator[ProtectAdoptableDeviceModel] async_get_devices(Bootstrap bootstrap, Iterable[ModelType] model_type)
str get_camera_base_name(CameraChannel channel)