Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Neato botvac connected vacuum cleaners."""
2 
3 import logging
4 
5 import aiohttp
6 from pybotvac import Account
7 from pybotvac.exceptions import NeatoException
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_TOKEN, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
13 from homeassistant.helpers import config_entry_oauth2_flow
14 
15 from . import api
16 from .const import NEATO_DOMAIN, NEATO_LOGIN
17 from .hub import NeatoHub
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 PLATFORMS = [
22  Platform.BUTTON,
23  Platform.CAMERA,
24  Platform.SENSOR,
25  Platform.SWITCH,
26  Platform.VACUUM,
27 ]
28 
29 
30 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
31  """Set up config entry."""
32  hass.data.setdefault(NEATO_DOMAIN, {})
33  if CONF_TOKEN not in entry.data:
34  raise ConfigEntryAuthFailed
35 
36  implementation = (
37  await config_entry_oauth2_flow.async_get_config_entry_implementation(
38  hass, entry
39  )
40  )
41 
42  session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
43  try:
44  await session.async_ensure_token_valid()
45  except aiohttp.ClientResponseError as ex:
46  _LOGGER.debug("API error: %s (%s)", ex.code, ex.message)
47  if ex.code in (401, 403):
48  raise ConfigEntryAuthFailed("Token not valid, trigger renewal") from ex
49  raise ConfigEntryNotReady from ex
50 
51  neato_session = api.ConfigEntryAuth(hass, entry, implementation)
52  hass.data[NEATO_DOMAIN][entry.entry_id] = neato_session
53  hub = NeatoHub(hass, Account(neato_session))
54 
55  await hub.async_update_entry_unique_id(entry)
56 
57  try:
58  await hass.async_add_executor_job(hub.update_robots)
59  except NeatoException as ex:
60  _LOGGER.debug("Failed to connect to Neato API")
61  raise ConfigEntryNotReady from ex
62 
63  hass.data[NEATO_LOGIN] = hub
64 
65  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
66 
67  return True
68 
69 
70 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
71  """Unload config entry."""
72  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
73  if unload_ok:
74  hass.data[NEATO_DOMAIN].pop(entry.entry_id)
75 
76  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:70
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:30