hass-core/homeassistant/components/laundrify/__init__.py
xLarry abf9aab18f
Add laundrify integration (#65090)
* First version of laundrify integration

* Code cleanup

* Code cleanup after review #2

* Move coordinator to its own file

* Save devices as dict and implement available prop as fn

* Validate token on init, abort if already configured

* Some more cleanup after review

* Add strict type hints

* Minor changes after code review

* Remove OptionsFlow (use default poll interval instead)

* Fix CODEOWNERS to pass hassfest job

* Fix formatting to pass prettier job

* Fix mypy typing error

* Update internal device property after fetching data

* Call parental update handler and remove obsolete code

* Add coordinator tests and fix some config flow tests

* Refactor tests

* Refactor fixtures

* Device unavailable if polling fails
2022-05-21 15:18:01 -04:00

52 lines
1.7 KiB
Python

"""The laundrify integration."""
from __future__ import annotations
from laundrify_aio import LaundrifyAPI
from laundrify_aio.exceptions import ApiConnectionException, UnauthorizedException
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DEFAULT_POLL_INTERVAL, DOMAIN
from .coordinator import LaundrifyUpdateCoordinator
PLATFORMS = [Platform.BINARY_SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up laundrify from a config entry."""
session = async_get_clientsession(hass)
api_client = LaundrifyAPI(entry.data[CONF_ACCESS_TOKEN], session)
try:
await api_client.validate_token()
except UnauthorizedException as err:
raise ConfigEntryAuthFailed("Invalid authentication") from err
except ApiConnectionException as err:
raise ConfigEntryNotReady("Cannot reach laundrify API") from err
coordinator = LaundrifyUpdateCoordinator(hass, api_client, DEFAULT_POLL_INTERVAL)
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
"api": api_client,
"coordinator": coordinator,
}
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok