1 """Integration to UniFi Network and its various features."""
3 from aiounifi.models.client
import Client
14 from .const
import DOMAIN
as UNIFI_DOMAIN, PLATFORMS, UNIFI_WIRELESS_CLIENTS
15 from .errors
import AuthenticationRequired, CannotConnect
16 from .hub
import UnifiHub, get_unifi_api
17 from .services
import async_setup_services
19 type UnifiConfigEntry = ConfigEntry[UnifiHub]
22 STORAGE_KEY =
"unifi_data"
25 CONFIG_SCHEMA = cv.config_entry_only_config_schema(UNIFI_DOMAIN)
28 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
29 """Integration doesn't support configuration through configuration.yaml."""
33 await wireless_clients.async_load()
39 hass: HomeAssistant, config_entry: UnifiConfigEntry
41 """Set up the UniFi Network integration."""
45 except CannotConnect
as err:
46 raise ConfigEntryNotReady
from err
48 except AuthenticationRequired
as err:
49 raise ConfigEntryAuthFailed
from err
51 hub = config_entry.runtime_data = UnifiHub(hass, config_entry, api)
52 await hub.initialize()
54 await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
55 hub.async_update_device_registry()
56 hub.entity_loader.load_entities()
60 config_entry.async_on_unload(
61 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, hub.shutdown)
67 hass: HomeAssistant, config_entry: UnifiConfigEntry
69 """Unload a config entry."""
70 return await config_entry.runtime_data.async_reset()
74 hass: HomeAssistant, config_entry: UnifiConfigEntry, device_entry: DeviceEntry
76 """Remove config entry from a device."""
77 hub = config_entry.runtime_data
80 for _, identifier
in device_entry.connections
81 if identifier
in hub.api.clients
or identifier
in hub.api.devices
86 """Class to store clients known to be wireless.
88 This is needed since wireless devices going offline
89 might get marked as wired by UniFi.
92 def __init__(self, hass: HomeAssistant) ->
None:
93 """Set up client storage."""
95 self.
datadata: dict[str, dict[str, list[str]] | list[str]] = {}
96 self.wireless_clients: set[str] = set()
97 self._store: Store =
Store(hass, STORAGE_VERSION, STORAGE_KEY)
100 """Load data from file."""
101 if (data := await self._store.
async_load())
is not None:
103 if "wireless_clients" not in data:
104 data[
"wireless_clients"] = [
106 for config_entry
in data
107 for obj_id
in data[config_entry][
"wireless_devices"]
109 self.wireless_clients.
update(data[
"wireless_clients"])
113 """Is client known to be wireless.
115 Store if client is wireless and not known.
117 if not client.is_wired
and client.mac
not in self.wireless_clients:
118 self.wireless_clients.
add(client.mac)
121 return client.mac
in self.wireless_clients
125 """Update data and schedule to save to file."""
126 self.wireless_clients.
update(
127 {client.mac
for client
in clients
if not client.is_wired}
133 """Return data of UniFi wireless clients to store in a file."""
134 self.
datadata[
"wireless_clients"] =
list(self.wireless_clients)
138 """Validate membership of item ID."""
139 return obj_id
in self.wireless_clients
bool is_wireless(self, Client client)
None update_clients(self, set[Client] clients)
dict[str, dict[str, list[str]]|list[str]] _data_to_save(self)
None __init__(self, HomeAssistant hass)
bool __contains__(self, int|str obj_id)
bool add(self, _T matcher)
None async_setup_services(HomeAssistant hass)
IssData update(pyiss.ISS iss)
aiounifi.Controller get_unifi_api(HomeAssistant hass, MappingProxyType[str, Any] config)
bool async_setup_entry(HomeAssistant hass, UnifiConfigEntry config_entry)
bool async_remove_config_entry_device(HomeAssistant hass, UnifiConfigEntry config_entry, DeviceEntry device_entry)
bool async_unload_entry(HomeAssistant hass, UnifiConfigEntry config_entry)
bool async_setup(HomeAssistant hass, ConfigType config)
None async_delay_save(self, Callable[[], _T] data_func, float delay=0)