2019-02-13 20:21:14 +00:00
|
|
|
"""Support for devices connected to UniFi POE."""
|
2019-07-14 19:57:09 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-03-12 10:56:50 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
2019-10-02 19:43:14 +00:00
|
|
|
from homeassistant.core import callback
|
2019-07-14 19:57:09 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-12-09 11:19:34 +00:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2019-10-02 19:43:14 +00:00
|
|
|
from .config_flow import get_controller_id_from_config_entry
|
2020-04-23 14:48:24 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_MANUFACTURER,
|
|
|
|
DOMAIN as UNIFI_DOMAIN,
|
|
|
|
LOGGER,
|
|
|
|
UNIFI_WIRELESS_CLIENTS,
|
|
|
|
)
|
2019-05-13 08:16:55 +00:00
|
|
|
from .controller import UniFiController
|
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
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
2020-04-23 14:48:24 +00:00
|
|
|
cv.deprecated(UNIFI_DOMAIN, invalidation_version="0.109"),
|
|
|
|
{UNIFI_DOMAIN: cv.match_all},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Component 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
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry):
|
|
|
|
"""Set up the UniFi component."""
|
2020-04-23 14:48:24 +00:00
|
|
|
hass.data.setdefault(UNIFI_DOMAIN, {})
|
2018-10-29 18:09:54 +00:00
|
|
|
|
|
|
|
controller = UniFiController(hass, config_entry)
|
2018-10-16 08:35:35 +00:00
|
|
|
if not await controller.async_setup():
|
|
|
|
return False
|
|
|
|
|
2020-04-23 14:48:24 +00:00
|
|
|
hass.data[UNIFI_DOMAIN][config_entry.entry_id] = controller
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2020-03-25 23:03:26 +00:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, controller.shutdown)
|
|
|
|
|
2020-04-17 06:39:01 +00:00
|
|
|
LOGGER.debug("UniFi config options %s", config_entry.options)
|
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
if controller.mac is None:
|
|
|
|
return True
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
device_registry = await hass.helpers.device_registry.async_get_registry()
|
2018-10-16 08:35:35 +00:00
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=config_entry.entry_id,
|
|
|
|
connections={(CONNECTION_NETWORK_MAC, controller.mac)},
|
2019-07-30 08:05:51 +00:00
|
|
|
manufacturer=ATTR_MANUFACTURER,
|
2018-10-16 08:35:35 +00:00
|
|
|
model="UniFi Controller",
|
|
|
|
name="UniFi Controller",
|
|
|
|
# sw_version=config.raw['swversion'],
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, config_entry):
|
|
|
|
"""Unload a config entry."""
|
2020-04-23 14:48:24 +00:00
|
|
|
controller = hass.data[UNIFI_DOMAIN].pop(config_entry.entry_id)
|
2018-10-16 08:35:35 +00:00
|
|
|
return await controller.async_reset()
|
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 = {}
|
|
|
|
self._store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY)
|
|
|
|
|
|
|
|
async def async_load(self):
|
|
|
|
"""Load data from file."""
|
|
|
|
data = await self._store.async_load()
|
|
|
|
|
|
|
|
if data is not None:
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def get_data(self, config_entry):
|
|
|
|
"""Get data related to a specific controller."""
|
|
|
|
controller_id = get_controller_id_from_config_entry(config_entry)
|
2020-04-23 14:48:24 +00:00
|
|
|
key = config_entry.entry_id
|
|
|
|
if controller_id in self.data:
|
|
|
|
key = controller_id
|
|
|
|
|
|
|
|
data = self.data.get(key, {"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."""
|
|
|
|
controller_id = get_controller_id_from_config_entry(config_entry)
|
2020-04-23 14:48:24 +00:00
|
|
|
if controller_id in self.data:
|
|
|
|
self.data.pop(controller_id)
|
2019-10-02 19:43:14 +00:00
|
|
|
|
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
|