Merge UniFi device tracker to config entry (#24367)
* Move device tracker to use config entry * Remove monitored conditions attributes based on ADR0003 * Add support for import of device tracker config to be backwards compatible * Remove unnecessary configuration options from device tracker * Add component configuration support
This commit is contained in:
1 parent
3480e6229a
commit
01b890f426
11 files changed
+493
-449
No files matched your search
@@ -12,7 +12,8 @@ from homeassistant.const import CONF_HOST
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
|
||||
from .const import CONF_CONTROLLER, CONF_SITE_ID, CONTROLLER_ID, LOGGER
|
||||
from .const import (
|
||||
CONF_CONTROLLER, CONF_SITE_ID, CONTROLLER_ID, LOGGER, UNIFI_CONFIG)
|
||||
from .errors import AuthenticationRequired, CannotConnect
|
||||
|
||||
|
||||
@@ -27,11 +28,30 @@ class UniFiController:
|
||||
self.api = None
|
||||
self.progress = None
|
||||
|
||||
self._site_name = None
|
||||
self._site_role = None
|
||||
self.unifi_config = {}
|
||||
|
||||
@property
|
||||
def host(self):
|
||||
"""Return the host of this controller."""
|
||||
return self.config_entry.data[CONF_CONTROLLER][CONF_HOST]
|
||||
|
||||
@property
|
||||
def site(self):
|
||||
"""Return the site of this config entry."""
|
||||
return self.config_entry.data[CONF_CONTROLLER][CONF_SITE_ID]
|
||||
|
||||
@property
|
||||
def site_name(self):
|
||||
"""Return the nice name of site."""
|
||||
return self._site_name
|
||||
|
||||
@property
|
||||
def site_role(self):
|
||||
"""Return the site user role of this controller."""
|
||||
return self._site_role
|
||||
|
||||
@property
|
||||
def mac(self):
|
||||
"""Return the mac address of this controller."""
|
||||
@@ -44,9 +64,7 @@ class UniFiController:
|
||||
def event_update(self):
|
||||
"""Event specific per UniFi entry to signal new data."""
|
||||
return 'unifi-update-{}'.format(
|
||||
CONTROLLER_ID.format(
|
||||
host=self.host,
|
||||
site=self.config_entry.data[CONF_CONTROLLER][CONF_SITE_ID]))
|
||||
CONTROLLER_ID.format(host=self.host, site=self.site))
|
||||
|
||||
async def request_update(self):
|
||||
"""Request an update."""
|
||||
@@ -63,7 +81,7 @@ class UniFiController:
|
||||
failed = False
|
||||
|
||||
try:
|
||||
with async_timeout.timeout(4):
|
||||
with async_timeout.timeout(10):
|
||||
await self.api.clients.update()
|
||||
await self.api.devices.update()
|
||||
|
||||
@@ -99,6 +117,14 @@ class UniFiController:
|
||||
self.hass, **self.config_entry.data[CONF_CONTROLLER])
|
||||
await self.api.initialize()
|
||||
|
||||
sites = await self.api.sites()
|
||||
|
||||
for site in sites.values():
|
||||
if self.site == site['name']:
|
||||
self._site_name = site['desc']
|
||||
self._site_role = site['role']
|
||||
break
|
||||
|
||||
except CannotConnect:
|
||||
raise ConfigEntryNotReady
|
||||
|
||||
@@ -107,9 +133,16 @@ class UniFiController:
|
||||
'Unknown error connecting with UniFi controller.')
|
||||
return False
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(
|
||||
self.config_entry, 'switch'))
|
||||
for unifi_config in hass.data[UNIFI_CONFIG]:
|
||||
if self.host == unifi_config[CONF_HOST] and \
|
||||
self.site == unifi_config[CONF_SITE_ID]:
|
||||
self.unifi_config = unifi_config
|
||||
break
|
||||
|
||||
for platform in ['device_tracker', 'switch']:
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(
|
||||
self.config_entry, platform))
|
||||
|
||||
return True
|
||||
|
||||
@@ -123,8 +156,11 @@ class UniFiController:
|
||||
if self.api is None:
|
||||
return True
|
||||
|
||||
return await self.hass.config_entries.async_forward_entry_unload(
|
||||
self.config_entry, 'switch')
|
||||
for platform in ['device_tracker', 'switch']:
|
||||
await self.hass.config_entries.async_forward_entry_unload(
|
||||
self.config_entry, platform)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def get_controller(
|
||||
|
||||
Reference in new issue
Block a user