core/homeassistant/components/myq/__init__.py

66 lines
2.2 KiB
Python

"""The MyQ integration."""
from datetime import timedelta
import logging
import pymyq
from pymyq.errors import InvalidCredentialsError, MyQError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN, MYQ_COORDINATOR, MYQ_GATEWAY, PLATFORMS, UPDATE_INTERVAL
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up MyQ from a config entry."""
hass.data.setdefault(DOMAIN, {})
websession = aiohttp_client.async_get_clientsession(hass)
conf = entry.data
try:
myq = await pymyq.login(conf[CONF_USERNAME], conf[CONF_PASSWORD], websession)
except InvalidCredentialsError as err:
raise ConfigEntryAuthFailed from err
except MyQError as err:
raise ConfigEntryNotReady from err
# Called by DataUpdateCoordinator, allows to capture any MyQError exceptions and to throw an HASS UpdateFailed
# exception instead, preventing traceback in HASS logs.
async def async_update_data():
try:
return await myq.update_device_info()
except InvalidCredentialsError as err:
raise ConfigEntryAuthFailed from err
except MyQError as err:
raise UpdateFailed(str(err)) from err
coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
name="myq devices",
update_method=async_update_data,
update_interval=timedelta(seconds=UPDATE_INTERVAL),
)
hass.data[DOMAIN][entry.entry_id] = {MYQ_GATEWAY: myq, MYQ_COORDINATOR: coordinator}
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok