ha_gehome/sharkiq/__init__.py

132 lines
4.0 KiB
Python
Raw Normal View History

2020-08-12 07:31:39 -06:00
"""Shark IQ Integration."""
2020-07-22 08:30:57 -06:00
import asyncio
2020-08-12 07:31:39 -06:00
import async_timeout
2020-07-22 08:30:57 -06:00
from sharkiqpy import (
AylaApi,
SharkIqAuthError,
2020-08-12 07:31:39 -06:00
SharkIqAuthExpiringError,
SharkIqNotAuthedError,
2020-07-22 08:30:57 -06:00
get_ayla_api,
)
2020-08-12 07:31:39 -06:00
import voluptuous as vol
2020-07-22 08:30:57 -06:00
2020-08-12 07:31:39 -06:00
from homeassistant import config_entries, exceptions
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from .const import API_TIMEOUT, COMPONENTS, DOMAIN, LOGGER
from .update_coordinator import SharkIqUpdateCoordinator
2020-07-22 08:30:57 -06:00
CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA)
class CannotConnect(exceptions.HomeAssistantError):
"""Error to indicate we cannot connect."""
async def async_setup(hass, config):
2020-08-12 07:31:39 -06:00
"""Set up the sharkiq environment."""
2020-07-22 08:30:57 -06:00
hass.data.setdefault(DOMAIN, {})
if DOMAIN not in config:
return True
for index, conf in enumerate(config[DOMAIN]):
2020-08-12 07:31:39 -06:00
LOGGER.debug(
"Importing Shark IQ #%d (Username: %s)", index, conf[CONF_USERNAME]
)
2020-07-22 08:30:57 -06:00
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=conf,
)
)
async def async_connect_or_timeout(ayla_api: AylaApi) -> AylaApi:
"""Connect to vacuum."""
try:
2020-08-12 07:31:39 -06:00
with async_timeout.timeout(API_TIMEOUT):
LOGGER.debug("Initialize connection to Ayla networks API")
2020-07-22 08:30:57 -06:00
await ayla_api.async_sign_in()
except SharkIqAuthError as exc:
2020-08-12 07:31:39 -06:00
LOGGER.error("Error to connect to Shark IQ api")
2020-07-22 08:30:57 -06:00
raise CannotConnect from exc
except asyncio.TimeoutError as exc:
2020-08-12 07:31:39 -06:00
LOGGER.error("Timeout expired")
2020-07-22 08:30:57 -06:00
raise CannotConnect from exc
return ayla_api
async def async_setup_entry(hass, config_entry):
2020-08-12 07:31:39 -06:00
"""Initialize the sharkiq platform via config entry."""
2020-07-22 08:30:57 -06:00
ayla_api = get_ayla_api(
username=config_entry.data[CONF_USERNAME],
password=config_entry.data[CONF_PASSWORD],
2020-08-12 07:31:39 -06:00
websession=hass.helpers.aiohttp_client.async_get_clientsession(),
2020-07-22 08:30:57 -06:00
)
try:
if not await async_connect_or_timeout(ayla_api):
return False
except CannotConnect as exc:
raise exceptions.ConfigEntryNotReady from exc
2020-08-12 07:31:39 -06:00
shark_vacs = await ayla_api.async_get_devices(False)
device_names = ", ".join([d.name for d in shark_vacs])
LOGGER.info("Found %d Shark IQ device(s): %s", len(device_names), device_names)
coordinator = SharkIqUpdateCoordinator(hass, config_entry, ayla_api, shark_vacs)
await coordinator.async_refresh()
if not coordinator.last_update_success:
raise exceptions.ConfigEntryNotReady
hass.data[DOMAIN][config_entry.entry_id] = coordinator
2020-07-22 08:30:57 -06:00
for component in COMPONENTS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, component)
)
if not config_entry.update_listeners:
config_entry.add_update_listener(async_update_options)
return True
2020-08-12 07:31:39 -06:00
async def async_disconnect_or_timeout(coordinator: SharkIqUpdateCoordinator):
2020-07-22 08:30:57 -06:00
"""Disconnect to vacuum."""
2020-08-12 07:31:39 -06:00
LOGGER.debug("Disconnecting from Ayla Api")
with async_timeout.timeout(5):
try:
await coordinator.ayla_api.async_sign_out()
except (SharkIqAuthError, SharkIqAuthExpiringError, SharkIqNotAuthedError):
pass
2020-07-22 08:30:57 -06:00
return True
async def async_update_options(hass, config_entry):
"""Update options."""
await hass.config_entries.async_reload(config_entry.entry_id)
async def async_unload_entry(hass, config_entry):
"""Unload a config entry."""
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(config_entry, component)
for component in COMPONENTS
]
)
)
if unload_ok:
domain_data = hass.data[DOMAIN][config_entry.entry_id]
2020-08-12 07:31:39 -06:00
try:
await async_disconnect_or_timeout(coordinator=domain_data)
except SharkIqAuthError:
pass
2020-07-22 08:30:57 -06:00
hass.data[DOMAIN].pop(config_entry.entry_id)
return unload_ok