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
|
|
|
|
|
2019-05-13 08:16:55 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
2018-10-16 08:35:35 +00:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
|
|
|
|
2019-07-14 19:57:09 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
from .const import (
|
2019-07-25 14:56:56 +00:00
|
|
|
CONF_BLOCK_CLIENT, CONF_CONTROLLER, CONF_DETECTION_TIME, CONF_SITE_ID,
|
|
|
|
CONF_SSID_FILTER, CONTROLLER_ID, DOMAIN, UNIFI_CONFIG)
|
2019-05-13 08:16:55 +00:00
|
|
|
from .controller import UniFiController
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-07-14 19:57:09 +00:00
|
|
|
CONF_CONTROLLERS = 'controllers'
|
|
|
|
|
|
|
|
CONTROLLER_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_SITE_ID): cv.string,
|
2019-07-25 14:56:56 +00:00
|
|
|
vol.Optional(CONF_BLOCK_CLIENT, default=[]): vol.All(
|
|
|
|
cv.ensure_list, [cv.string]),
|
2019-07-14 19:57:09 +00:00
|
|
|
vol.Optional(CONF_DETECTION_TIME): vol.All(
|
|
|
|
cv.time_period, cv.positive_timedelta),
|
2019-07-25 14:56:56 +00:00
|
|
|
vol.Optional(CONF_SSID_FILTER): vol.All(cv.ensure_list, [cv.string]),
|
2019-07-14 19:57:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Required(CONF_CONTROLLERS):
|
|
|
|
vol.All(cv.ensure_list, [CONTROLLER_SCHEMA]),
|
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Component doesn't support configuration through configuration.yaml."""
|
2019-07-14 19:57:09 +00:00
|
|
|
hass.data[UNIFI_CONFIG] = []
|
|
|
|
|
|
|
|
if DOMAIN in config:
|
|
|
|
hass.data[UNIFI_CONFIG] = config[DOMAIN][CONF_CONTROLLERS]
|
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry):
|
|
|
|
"""Set up the UniFi component."""
|
|
|
|
if DOMAIN not in hass.data:
|
|
|
|
hass.data[DOMAIN] = {}
|
2018-10-29 18:09:54 +00:00
|
|
|
|
|
|
|
controller = UniFiController(hass, config_entry)
|
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
controller_id = CONTROLLER_ID.format(
|
|
|
|
host=config_entry.data[CONF_CONTROLLER][CONF_HOST],
|
|
|
|
site=config_entry.data[CONF_CONTROLLER][CONF_SITE_ID]
|
|
|
|
)
|
|
|
|
|
2018-10-29 18:09:54 +00:00
|
|
|
hass.data[DOMAIN][controller_id] = controller
|
|
|
|
|
2018-10-16 08:35:35 +00:00
|
|
|
if not await controller.async_setup():
|
|
|
|
return False
|
|
|
|
|
|
|
|
if controller.mac is None:
|
|
|
|
return True
|
|
|
|
|
|
|
|
device_registry = await \
|
|
|
|
hass.helpers.device_registry.async_get_registry()
|
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=config_entry.entry_id,
|
|
|
|
connections={(CONNECTION_NETWORK_MAC, controller.mac)},
|
|
|
|
manufacturer='Ubiquiti',
|
|
|
|
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."""
|
|
|
|
controller_id = CONTROLLER_ID.format(
|
|
|
|
host=config_entry.data[CONF_CONTROLLER][CONF_HOST],
|
|
|
|
site=config_entry.data[CONF_CONTROLLER][CONF_SITE_ID]
|
|
|
|
)
|
|
|
|
controller = hass.data[DOMAIN].pop(controller_id)
|
|
|
|
return await controller.async_reset()
|