Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Anova integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import TYPE_CHECKING
7 
8 from anova_wifi import (
9  AnovaApi,
10  APCWifiDevice,
11  InvalidLogin,
12  NoDevicesFound,
13  WebsocketFailure,
14 )
15 
16 from homeassistant.const import CONF_DEVICES, CONF_PASSWORD, CONF_USERNAME, Platform
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import ConfigEntryNotReady
19 from homeassistant.helpers import aiohttp_client
20 
21 from .coordinator import AnovaCoordinator
22 from .models import AnovaConfigEntry, AnovaData
23 
24 PLATFORMS = [Platform.SENSOR]
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 
29 async def async_setup_entry(hass: HomeAssistant, entry: AnovaConfigEntry) -> bool:
30  """Set up Anova from a config entry."""
31  api = AnovaApi(
32  aiohttp_client.async_get_clientsession(hass),
33  entry.data[CONF_USERNAME],
34  entry.data[CONF_PASSWORD],
35  )
36  try:
37  await api.authenticate()
38  except InvalidLogin as err:
39  _LOGGER.error(
40  "Login was incorrect - please log back in through the config flow. %s", err
41  )
42  return False
43  assert api.jwt
44  try:
45  await api.create_websocket()
46  except NoDevicesFound as err:
47  # Can later setup successfully and spawn a repair.
48  raise ConfigEntryNotReady(
49  "No devices were found on the websocket, perhaps you don't have any devices on this account?"
50  ) from err
51  except WebsocketFailure as err:
52  raise ConfigEntryNotReady("Failed connecting to the websocket.") from err
53  # Create a coordinator per device, if the device is offline, no data will be on the
54  # websocket, and the coordinator should auto mark as unavailable. But as long as
55  # the websocket successfully connected, config entry should setup.
56  devices: list[APCWifiDevice] = []
57  if TYPE_CHECKING:
58  # api.websocket_handler can't be None after successfully creating the
59  # websocket client
60  assert api.websocket_handler is not None
61  devices = list(api.websocket_handler.devices.values())
62  coordinators = [AnovaCoordinator(hass, device) for device in devices]
63  entry.runtime_data = AnovaData(api_jwt=api.jwt, coordinators=coordinators, api=api)
64  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
65  return True
66 
67 
68 async def async_unload_entry(hass: HomeAssistant, entry: AnovaConfigEntry) -> bool:
69  """Unload a config entry."""
70  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
71  # Disconnect from WS
72  await entry.runtime_data.api.disconnect_websocket()
73  return unload_ok
74 
75 
76 async def async_migrate_entry(hass: HomeAssistant, entry: AnovaConfigEntry) -> bool:
77  """Migrate entry."""
78  _LOGGER.debug("Migrating from version %s:%s", entry.version, entry.minor_version)
79 
80  if entry.version > 1:
81  # This means the user has downgraded from a future version
82  return False
83 
84  if entry.version == 1 and entry.minor_version == 1:
85  new_data = {**entry.data}
86  if CONF_DEVICES in new_data:
87  new_data.pop(CONF_DEVICES)
88 
89  hass.config_entries.async_update_entry(entry, data=new_data, minor_version=2)
90 
91  _LOGGER.debug(
92  "Migration to version %s:%s successful", entry.version, entry.minor_version
93  )
94 
95  return True
bool async_setup_entry(HomeAssistant hass, AnovaConfigEntry entry)
Definition: __init__.py:29
bool async_unload_entry(HomeAssistant hass, AnovaConfigEntry entry)
Definition: __init__.py:68
bool async_migrate_entry(HomeAssistant hass, AnovaConfigEntry entry)
Definition: __init__.py:76