1 """UniFi Network entity loader.
3 Central point to load entities for the different platforms.
4 Make sure expected clients are available for platforms.
7 from __future__
import annotations
10 from collections.abc
import Callable, Coroutine, Sequence
11 from datetime
import timedelta
12 from functools
import partial
13 from typing
import TYPE_CHECKING, Any
15 from aiounifi.interfaces.api_handlers
import ItemEvent
24 from ..const
import LOGGER, UNIFI_WIRELESS_CLIENTS
25 from ..entity
import UnifiEntity, UnifiEntityDescription
28 from .hub
import UnifiHub
35 """UniFi Network integration handling platforms for entity registration."""
38 """Initialize the UniFi entity loader."""
41 hub.api.clients.update,
42 hub.api.clients_all.update,
43 hub.api.devices.update,
44 hub.api.dpi_apps.update,
45 hub.api.dpi_groups.update,
46 hub.api.port_forwarding.update,
48 hub.api.system_information.update,
49 hub.api.traffic_rules.update,
58 name=
"Unifi entity poller",
60 update_interval=POLL_INTERVAL,
64 update_callback=
lambda:
None
71 tuple[UnifiEntityDescription, ...],
76 self.known_objects: set[tuple[str, str]] = set()
77 """Tuples of entity description key and object ID of loaded entities."""
80 """Initialize API data and extra client support."""
86 self, updaters: Sequence[Callable[[], Coroutine[Any, Any,
None]]]
88 results = await asyncio.gather(
89 *[
update()
for update
in updaters],
90 return_exceptions=
True,
92 for result
in results:
93 if result
is not None:
94 LOGGER.warning(
"Exception on update %s", result)
97 """Refresh API data for pollable updaters."""
101 """Refresh API data from network application."""
106 """Restore inactive clients.
108 Provide inactive clients to device tracker and switch platform.
110 config = self.
hubhub.config
111 entity_registry = er.async_get(self.
hubhub.hass)
113 entry.unique_id.split(
"-", 1)[1]
114 for entry
in er.async_entries_for_config_entry(
115 entity_registry, config.entry.entry_id
117 if entry.domain == Platform.DEVICE_TRACKER
and "-" in entry.unique_id
119 api = self.
hubhub.api
120 for mac
in config.option_supported_clients + config.option_block_clients + macs:
121 if mac
not in api.clients
and mac
in api.clients_all:
122 api.clients.process_raw([
dict(api.clients_all[mac].raw)])
127 async_add_entities: AddEntitiesCallback,
128 entity_class: type[UnifiEntity],
129 descriptions: tuple[UnifiEntityDescription, ...],
130 requires_admin: bool =
False,
132 """Register UniFi entity platforms."""
133 self.platforms.append(
134 (async_add_entities, entity_class, descriptions, requires_admin)
139 """Load entities into the registered UniFi platforms."""
146 if requires_admin
and not self.
hubhub.is_admin:
148 self.
_load_entities_load_entities(entity_class, descriptions, async_add_entities)
152 self, description: UnifiEntityDescription, obj_id: str
154 """Validate if entity is allowed and supported before creating it."""
156 (description.key, obj_id)
not in self.known_objects
157 and description.allowed_fn(self.
hubhub, obj_id)
158 and description.supported_fn(self.
hubhub, obj_id)
164 unifi_platform_entity: type[UnifiEntity],
165 descriptions: tuple[UnifiEntityDescription, ...],
166 async_add_entities: AddEntitiesCallback,
168 """Load entities and subscribe for future entities."""
171 def add_unifi_entities() -> None:
172 """Add currently known UniFi entities."""
174 unifi_platform_entity(obj_id, self.
hubhub, description)
175 for description
in descriptions
176 for obj_id
in description.api_handler_fn(self.
hubhub.api)
182 self.
hubhub.config.entry.async_on_unload(
185 self.
hubhub.signal_options_update,
193 def create_unifi_entity(
194 description: UnifiEntityDescription, event: ItemEvent, obj_id: str
196 """Create new UniFi entity on event."""
199 [unifi_platform_entity(obj_id, self.
hubhub, description)]
202 for description
in descriptions:
204 partial(create_unifi_entity, description), ItemEvent.ADDED
None _refresh_data(self, Sequence[Callable[[], Coroutine[Any, Any, None]]] updaters)
None _refresh_api_data(self)
None _update_pollable_api_data(self)
None _load_entities(self, type[UnifiEntity] unifi_platform_entity, tuple[UnifiEntityDescription,...] descriptions, AddEntitiesCallback async_add_entities)
bool _should_add_entity(self, UnifiEntityDescription description, str obj_id)
None __init__(self, UnifiHub hub)
None _restore_inactive_clients(self)
None register_platform(self, AddEntitiesCallback async_add_entities, type[UnifiEntity] entity_class, tuple[UnifiEntityDescription,...] descriptions, bool requires_admin=False)
IssData update(pyiss.ISS iss)
Callable[[], None] subscribe(HomeAssistant hass, str topic, MessageCallbackType msg_callback, int qos=DEFAULT_QOS, str encoding="utf-8")
None async_add_listener(HomeAssistant hass, Callable[[], None] listener)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)