* Risco integration * Fix lint errors * Raise ConfigEntryNotReady if can't connect * Gracefully handle shutdown * pass session to pyrisco * minor change to init * Fix retries * Add exception log * Remove retries * Address code review comments * Remove log
90 lines
2.6 KiB
Python
90 lines
2.6 KiB
Python
"""The Risco integration."""
|
|
import asyncio
|
|
from datetime import timedelta
|
|
import logging
|
|
|
|
from pyrisco import CannotConnectError, OperationError, RiscoAPI, UnauthorizedError
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_PASSWORD, CONF_PIN, CONF_USERNAME
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
from .const import DATA_COORDINATOR, DOMAIN
|
|
|
|
PLATFORMS = ["alarm_control_panel"]
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
"""Set up the Risco component."""
|
|
hass.data.setdefault(DOMAIN, {})
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
"""Set up Risco from a config entry."""
|
|
data = entry.data
|
|
risco = RiscoAPI(data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_PIN])
|
|
try:
|
|
await risco.login(async_get_clientsession(hass))
|
|
except CannotConnectError as error:
|
|
raise ConfigEntryNotReady() from error
|
|
except UnauthorizedError:
|
|
_LOGGER.exception("Failed to login to Risco cloud")
|
|
return False
|
|
|
|
coordinator = RiscoDataUpdateCoordinator(hass, risco)
|
|
await coordinator.async_refresh()
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
DATA_COORDINATOR: coordinator,
|
|
}
|
|
|
|
for component in PLATFORMS:
|
|
hass.async_create_task(
|
|
hass.config_entries.async_forward_entry_setup(entry, component)
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
"""Unload a config entry."""
|
|
unload_ok = all(
|
|
await asyncio.gather(
|
|
*[
|
|
hass.config_entries.async_forward_entry_unload(entry, component)
|
|
for component in PLATFORMS
|
|
]
|
|
)
|
|
)
|
|
|
|
if unload_ok:
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
return unload_ok
|
|
|
|
|
|
class RiscoDataUpdateCoordinator(DataUpdateCoordinator):
|
|
"""Class to manage fetching risco data."""
|
|
|
|
def __init__(self, hass, risco):
|
|
"""Initialize global risco data updater."""
|
|
self.risco = risco
|
|
interval = timedelta(seconds=30)
|
|
super().__init__(
|
|
hass, _LOGGER, name=DOMAIN, update_interval=interval,
|
|
)
|
|
|
|
async def _async_update_data(self):
|
|
"""Fetch data from risco."""
|
|
try:
|
|
return await self.risco.get_state()
|
|
except (CannotConnectError, UnauthorizedError, OperationError) as error:
|
|
raise UpdateFailed from error
|