2021-11-26 21:44:49 +00:00
|
|
|
"""Integration to UniFi Network and its various features."""
|
2022-07-06 08:25:53 +00:00
|
|
|
from collections.abc import Mapping
|
|
|
|
from typing import Any
|
|
|
|
|
2021-12-27 22:42:24 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-03-12 10:56:50 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
2021-12-27 22:42:24 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-07-18 15:39:38 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2022-05-17 16:45:57 +00:00
|
|
|
from homeassistant.helpers.storage import Store
|
2021-12-27 22:42:24 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2022-09-25 18:08:56 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_CONTROLLER,
|
|
|
|
DOMAIN as UNIFI_DOMAIN,
|
|
|
|
PLATFORMS,
|
|
|
|
UNIFI_WIRELESS_CLIENTS,
|
|
|
|
)
|
|
|
|
from .controller import UniFiController, get_unifi_controller
|
2022-07-18 15:39:38 +00:00
|
|
|
from .errors import AuthenticationRequired, CannotConnect
|
2021-09-30 12:38:29 +00:00
|
|
|
from .services import async_setup_services, async_unload_services
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-10-02 19:43:14 +00:00
|
|
|
SAVE_DELAY = 10
|
|
|
|
STORAGE_KEY = "unifi_data"
|
|
|
|
STORAGE_VERSION = 1
|
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2021-12-27 22:42:24 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2021-11-26 21:44:49 +00:00
|
|
|
"""Integration doesn't support configuration through configuration.yaml."""
|
2019-10-02 19:43:14 +00:00
|
|
|
hass.data[UNIFI_WIRELESS_CLIENTS] = wireless_clients = UnifiWirelessClients(hass)
|
|
|
|
await wireless_clients.async_load()
|
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-12-27 22:42:24 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2021-11-26 21:44:49 +00:00
|
|
|
"""Set up the UniFi Network integration."""
|
2020-04-23 14:48:24 +00:00
|
|
|
hass.data.setdefault(UNIFI_DOMAIN, {})
|
2018-10-29 18:09:54 +00:00
|
|
|
|
2021-02-06 20:32:18 +00:00
|
|
|
# Flat configuration was introduced with 2021.3
|
|
|
|
await async_flatten_entry_data(hass, config_entry)
|
|
|
|
|
2022-07-18 15:39:38 +00:00
|
|
|
try:
|
|
|
|
api = await get_unifi_controller(hass, config_entry.data)
|
|
|
|
controller = UniFiController(hass, config_entry, api)
|
|
|
|
await controller.initialize()
|
|
|
|
|
|
|
|
except CannotConnect as err:
|
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
|
|
|
|
except AuthenticationRequired as err:
|
|
|
|
raise ConfigEntryAuthFailed from err
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2021-02-06 20:32:18 +00:00
|
|
|
# Unique ID was introduced with 2021.3
|
2021-02-05 18:38:08 +00:00
|
|
|
if config_entry.unique_id is None:
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
config_entry, unique_id=controller.site_id
|
|
|
|
)
|
|
|
|
|
2022-07-18 15:39:38 +00:00
|
|
|
hass.data[UNIFI_DOMAIN][config_entry.entry_id] = controller
|
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
|
|
|
await controller.async_update_device_registry()
|
|
|
|
|
|
|
|
if len(hass.data[UNIFI_DOMAIN]) == 1:
|
2021-10-01 13:59:29 +00:00
|
|
|
async_setup_services(hass)
|
|
|
|
|
2022-07-18 15:39:38 +00:00
|
|
|
api.start_websocket()
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2021-04-20 16:13:07 +00:00
|
|
|
config_entry.async_on_unload(
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, controller.shutdown)
|
|
|
|
)
|
2020-03-25 23:03:26 +00:00
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-12-27 22:42:24 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2018-10-16 08:35:35 +00:00
|
|
|
"""Unload a config entry."""
|
2020-04-23 14:48:24 +00:00
|
|
|
controller = hass.data[UNIFI_DOMAIN].pop(config_entry.entry_id)
|
2021-09-30 12:38:29 +00:00
|
|
|
|
|
|
|
if not hass.data[UNIFI_DOMAIN]:
|
2021-10-01 13:59:29 +00:00
|
|
|
async_unload_services(hass)
|
2021-09-30 12:38:29 +00:00
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
return await controller.async_reset()
|
2019-10-02 19:43:14 +00:00
|
|
|
|
|
|
|
|
2021-12-27 22:42:24 +00:00
|
|
|
async def async_flatten_entry_data(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry
|
|
|
|
) -> None:
|
2021-02-06 20:32:18 +00:00
|
|
|
"""Simpler configuration structure for entry data.
|
|
|
|
|
|
|
|
Keep controller key layer in case user rollbacks.
|
|
|
|
"""
|
|
|
|
|
2022-07-06 08:25:53 +00:00
|
|
|
data: Mapping[str, Any] = {
|
|
|
|
**config_entry.data,
|
|
|
|
**config_entry.data[CONF_CONTROLLER],
|
|
|
|
}
|
2021-02-06 20:32:18 +00:00
|
|
|
if config_entry.data != data:
|
|
|
|
hass.config_entries.async_update_entry(config_entry, data=data)
|
|
|
|
|
|
|
|
|
2019-10-02 19:43:14 +00:00
|
|
|
class UnifiWirelessClients:
|
|
|
|
"""Class to store clients known to be wireless.
|
|
|
|
|
|
|
|
This is needed since wireless devices going offline might get marked as wired by UniFi.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, hass):
|
|
|
|
"""Set up client storage."""
|
|
|
|
self.hass = hass
|
|
|
|
self.data = {}
|
2022-05-17 16:45:57 +00:00
|
|
|
self._store = Store(hass, STORAGE_VERSION, STORAGE_KEY)
|
2019-10-02 19:43:14 +00:00
|
|
|
|
|
|
|
async def async_load(self):
|
|
|
|
"""Load data from file."""
|
2021-10-30 14:32:41 +00:00
|
|
|
if (data := await self._store.async_load()) is not None:
|
2019-10-02 19:43:14 +00:00
|
|
|
self.data = data
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def get_data(self, config_entry):
|
|
|
|
"""Get data related to a specific controller."""
|
2021-02-01 16:55:16 +00:00
|
|
|
data = self.data.get(config_entry.entry_id, {"wireless_devices": []})
|
2019-10-02 19:43:14 +00:00
|
|
|
return set(data["wireless_devices"])
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_data(self, data, config_entry):
|
|
|
|
"""Update data and schedule to save to file."""
|
2020-04-23 14:48:24 +00:00
|
|
|
self.data[config_entry.entry_id] = {"wireless_devices": list(data)}
|
2019-10-02 19:43:14 +00:00
|
|
|
self._store.async_delay_save(self._data_to_save, SAVE_DELAY)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _data_to_save(self):
|
|
|
|
"""Return data of UniFi wireless clients to store in a file."""
|
|
|
|
return self.data
|