Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Shark IQ Integration."""
2 
3 import asyncio
4 from contextlib import suppress
5 
6 from sharkiq import (
7  AylaApi,
8  SharkIqAuthError,
9  SharkIqAuthExpiringError,
10  SharkIqNotAuthedError,
11  get_ayla_api,
12 )
13 
14 from homeassistant import exceptions
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import CONF_PASSWORD, CONF_REGION, CONF_USERNAME
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.aiohttp_client import async_get_clientsession
19 
20 from .const import (
21  API_TIMEOUT,
22  DOMAIN,
23  LOGGER,
24  PLATFORMS,
25  SHARKIQ_REGION_DEFAULT,
26  SHARKIQ_REGION_EUROPE,
27 )
28 from .coordinator import SharkIqUpdateCoordinator
29 
30 
32  """Error to indicate we cannot connect."""
33 
34 
35 async def async_connect_or_timeout(ayla_api: AylaApi) -> bool:
36  """Connect to vacuum."""
37  try:
38  async with asyncio.timeout(API_TIMEOUT):
39  LOGGER.debug("Initialize connection to Ayla networks API")
40  await ayla_api.async_sign_in()
41  except SharkIqAuthError:
42  LOGGER.error("Authentication error connecting to Shark IQ api")
43  return False
44  except TimeoutError as exc:
45  LOGGER.error("Timeout expired")
46  raise CannotConnect from exc
47 
48  return True
49 
50 
51 async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
52  """Initialize the sharkiq platform via config entry."""
53  if CONF_REGION not in config_entry.data:
54  hass.config_entries.async_update_entry(
55  config_entry,
56  data={**config_entry.data, CONF_REGION: SHARKIQ_REGION_DEFAULT},
57  )
58 
59  ayla_api = get_ayla_api(
60  username=config_entry.data[CONF_USERNAME],
61  password=config_entry.data[CONF_PASSWORD],
62  websession=async_get_clientsession(hass),
63  europe=(config_entry.data[CONF_REGION] == SHARKIQ_REGION_EUROPE),
64  )
65 
66  try:
67  if not await async_connect_or_timeout(ayla_api):
68  return False
69  except CannotConnect as exc:
70  raise exceptions.ConfigEntryNotReady from exc
71 
72  shark_vacs = await ayla_api.async_get_devices(False)
73  device_names = ", ".join(d.name for d in shark_vacs)
74  LOGGER.debug("Found %d Shark IQ device(s): %s", len(shark_vacs), device_names)
75  coordinator = SharkIqUpdateCoordinator(hass, config_entry, ayla_api, shark_vacs)
76 
77  await coordinator.async_config_entry_first_refresh()
78 
79  hass.data.setdefault(DOMAIN, {})
80  hass.data[DOMAIN][config_entry.entry_id] = coordinator
81 
82  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
83 
84  return True
85 
86 
87 async def async_disconnect_or_timeout(coordinator: SharkIqUpdateCoordinator):
88  """Disconnect to vacuum."""
89  LOGGER.debug("Disconnecting from Ayla Api")
90  async with asyncio.timeout(5):
91  with suppress(
92  SharkIqAuthError, SharkIqAuthExpiringError, SharkIqNotAuthedError
93  ):
94  await coordinator.ayla_api.async_sign_out()
95 
96 
97 async def async_update_options(hass, config_entry):
98  """Update options."""
99  await hass.config_entries.async_reload(config_entry.entry_id)
100 
101 
102 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
103  """Unload a config entry."""
104  unload_ok = await hass.config_entries.async_unload_platforms(
105  config_entry, PLATFORMS
106  )
107  if unload_ok:
108  domain_data = hass.data[DOMAIN][config_entry.entry_id]
109  with suppress(SharkIqAuthError):
110  await async_disconnect_or_timeout(coordinator=domain_data)
111  hass.data[DOMAIN].pop(config_entry.entry_id)
112 
113  return unload_ok
def async_update_options(hass, config_entry)
Definition: __init__.py:97
bool async_connect_or_timeout(AylaApi ayla_api)
Definition: __init__.py:35
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:102
def async_disconnect_or_timeout(SharkIqUpdateCoordinator coordinator)
Definition: __init__.py:87
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_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)