1 """UniFi Network abstraction."""
3 from __future__
import annotations
5 from datetime
import datetime
6 from typing
import TYPE_CHECKING
19 from ..const
import ATTR_MANUFACTURER, CONF_SITE_ID, DOMAIN
as UNIFI_DOMAIN, PLATFORMS
20 from .config
import UnifiConfig
21 from .entity_helper
import UnifiEntityHelper
22 from .entity_loader
import UnifiEntityLoader
23 from .websocket
import UnifiWebsocket
26 from ..
import UnifiConfigEntry
30 """Manages a single UniFi Network instance."""
35 config_entry: UnifiConfigEntry,
36 api: aiounifi.Controller,
38 """Initialize the system."""
41 self.
configconfig = UnifiConfig.from_config_entry(config_entry)
46 self.
sitesite = config_entry.data[CONF_SITE_ID]
51 """Websocket connection state."""
56 """Event to signal new heartbeat missed."""
61 """Update device time in heartbeat monitor."""
66 """Update device time in heartbeat monitor."""
71 self, device_id: str, port_idx: int, poe_mode: str
73 """Queue commands to execute them together per device."""
78 """Integration specific event to signal a change in connection status."""
79 return f
"unifi-reachable-{self.config.entry.entry_id}"
83 """Event specific per UniFi entry to signal new options."""
84 return f
"unifi-options-{self.config.entry.entry_id}"
87 """Set up a UniFi Network instance."""
91 assert self.
configconfig.entry.unique_id
is not None
92 self.
is_adminis_admin = self.
apiapi.sites[self.
configconfig.entry.unique_id].role ==
"admin"
98 """UniFi Network device info."""
99 assert self.
configconfig.entry.unique_id
is not None
101 version: str |
None =
None
102 if sysinfo := next(iter(self.
apiapi.system_information.values()),
None):
103 version = sysinfo.version
106 entry_type=DeviceEntryType.SERVICE,
107 identifiers={(UNIFI_DOMAIN, self.
configconfig.entry.unique_id)},
108 manufacturer=ATTR_MANUFACTURER,
109 model=
"UniFi Network Application",
110 name=
"UniFi Network",
116 """Update device registry."""
117 device_registry = dr.async_get(self.
hasshass)
118 return device_registry.async_get_or_create(
124 hass: HomeAssistant, config_entry: UnifiConfigEntry
126 """Handle signals of config entry being updated.
128 If config entry is updated due to reauth flow
129 the entry might already have been reset and thus is not available.
131 hub = config_entry.runtime_data
132 hub.config = UnifiConfig.from_config_entry(config_entry)
137 """Wrap the call to unifi.close.
139 Used as an argument to EventBus.async_listen_once.
144 """Reset this hub to default state.
146 Will cancel any scheduled setup retry and will unload
149 await self.
websocketwebsocket.stop_and_wait()
151 unload_ok = await self.
hasshass.config_entries.async_unload_platforms(
152 self.
configconfig.entry, PLATFORMS
str signal_options_update(self)
str signal_reachable(self)
None update_heartbeat(self, str unique_id, datetime heartbeat_expire_time)
DeviceEntry async_update_device_registry(self)
str signal_heartbeat_missed(self)
None async_config_entry_updated(HomeAssistant hass, UnifiConfigEntry config_entry)
DeviceInfo device_info(self)
None shutdown(self, Event event)
None remove_heartbeat(self, str unique_id)
None __init__(self, HomeAssistant hass, UnifiConfigEntry config_entry, aiounifi.Controller api)
None queue_poe_port_command(self, str device_id, int port_idx, str poe_mode)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)