Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The laundrify integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from laundrify_aio import LaundrifyAPI
8 from laundrify_aio.exceptions import ApiConnectionException, UnauthorizedException
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_ACCESS_TOKEN, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import DEFAULT_POLL_INTERVAL, DOMAIN
17 from .coordinator import LaundrifyUpdateCoordinator
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
22 
23 
24 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
25  """Set up laundrify from a config entry."""
26 
27  session = async_get_clientsession(hass)
28  api_client = LaundrifyAPI(entry.data[CONF_ACCESS_TOKEN], session)
29 
30  try:
31  await api_client.validate_token()
32  except UnauthorizedException as err:
33  raise ConfigEntryAuthFailed("Invalid authentication") from err
34  except ApiConnectionException as err:
35  raise ConfigEntryNotReady("Cannot reach laundrify API") from err
36 
37  coordinator = LaundrifyUpdateCoordinator(hass, api_client, DEFAULT_POLL_INTERVAL)
38 
39  await coordinator.async_config_entry_first_refresh()
40 
41  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
42  "api": api_client,
43  "coordinator": coordinator,
44  }
45 
46  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
47 
48  return True
49 
50 
51 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
52  """Unload a config entry."""
53 
54  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
55  hass.data[DOMAIN].pop(entry.entry_id)
56 
57  return unload_ok
58 
59 
60 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
61  """Migrate entry."""
62 
63  _LOGGER.debug("Migrating from version %s", entry.version)
64 
65  if entry.version == 1:
66  # 1 -> 2: Unique ID from integer to string
67  if entry.minor_version == 1:
68  minor_version = 2
69  hass.config_entries.async_update_entry(
70  entry, unique_id=str(entry.unique_id), minor_version=minor_version
71  )
72 
73  _LOGGER.debug("Migration successful")
74 
75  return True
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:24
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:60
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:51
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)