UniFi simplify update (#24304)

This commit is contained in:
Robert Svensson authored and GitHub committed 2019-06-15 17:38:22 +02:00
1 parent aa8ddeca34
commit e9b0f54a43
7 files changed
+90 -134

No files matched your search

+60 -12
View File
@@ -5,11 +5,14 @@ import async_timeout
from aiohttp import CookieJar
import aiounifi
from homeassistant.exceptions import ConfigEntryNotReady
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_POE_CONTROL, LOGGER
from .const import CONF_CONTROLLER, CONF_SITE_ID, CONTROLLER_ID, LOGGER
from .errors import AuthenticationRequired, CannotConnect
@@ -37,7 +40,57 @@ class UniFiController:
return client.mac
return None
async def async_setup(self, tries=0):
@property
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]))
async def request_update(self):
"""Request an update."""
if self.progress is not None:
return await self.progress
self.progress = self.hass.async_create_task(self.async_update())
await self.progress
self.progress = None
async def async_update(self):
"""Update UniFi controller information."""
failed = False
try:
with async_timeout.timeout(4):
await self.api.clients.update()
await self.api.devices.update()
except aiounifi.LoginRequired:
try:
with async_timeout.timeout(5):
await self.api.login()
except (asyncio.TimeoutError, aiounifi.AiounifiException):
failed = True
if self.available:
LOGGER.error('Unable to reach controller %s', self.host)
self.available = False
except (asyncio.TimeoutError, aiounifi.AiounifiException):
failed = True
if self.available:
LOGGER.error('Unable to reach controller %s', self.host)
self.available = False
if not failed and not self.available:
LOGGER.info('Reconnected to controller %s', self.host)
self.available = True
async_dispatcher_send(self.hass, self.event_update)
async def async_setup(self):
"""Set up a UniFi controller."""
hass = self.hass
@@ -54,10 +107,9 @@ class UniFiController:
'Unknown error connecting with UniFi controller.')
return False
if self.config_entry.data[CONF_POE_CONTROL]:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
self.config_entry, 'switch'))
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
self.config_entry, 'switch'))
return True
@@ -71,17 +123,13 @@ class UniFiController:
if self.api is None:
return True
if self.config_entry.data[CONF_POE_CONTROL]:
return await self.hass.config_entries.async_forward_entry_unload(
self.config_entry, 'switch')
return True
return await self.hass.config_entries.async_forward_entry_unload(
self.config_entry, 'switch')
async def get_controller(
hass, host, username, password, port, site, verify_ssl):
"""Create a controller object and verify authentication."""
import aiounifi
sslcontext = None
if verify_ssl: