Home Assistant Unofficial Reference 2024.12.1
hub.py
Go to the documentation of this file.
1 """A wrapper 'hub' for the Litter-Robot API."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Generator, Mapping
6 from datetime import timedelta
7 import logging
8 from typing import Any
9 
10 from pylitterbot import Account, FeederRobot, LitterRobot
11 from pylitterbot.exceptions import LitterRobotException, LitterRobotLoginException
12 
13 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
14 from homeassistant.core import HomeAssistant
15 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
16 from homeassistant.helpers.aiohttp_client import async_get_clientsession
17 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
18 
19 from .const import DOMAIN
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 UPDATE_INTERVAL_SECONDS = 60 * 5
24 
25 
27  """A Litter-Robot hub wrapper class."""
28 
29  def __init__(self, hass: HomeAssistant, data: Mapping[str, Any]) -> None:
30  """Initialize the Litter-Robot hub."""
31  self._data_data = data
32  self.accountaccount = Account(websession=async_get_clientsession(hass))
33 
34  async def _async_update_data() -> bool:
35  """Update all device states from the Litter-Robot API."""
36  await self.accountaccount.refresh_robots()
37  return True
38 
40  hass,
41  _LOGGER,
42  name=DOMAIN,
43  update_method=_async_update_data,
44  update_interval=timedelta(seconds=UPDATE_INTERVAL_SECONDS),
45  )
46 
47  async def login(
48  self, load_robots: bool = False, subscribe_for_updates: bool = False
49  ) -> None:
50  """Login to Litter-Robot."""
51  try:
52  await self.accountaccount.connect(
53  username=self._data_data[CONF_USERNAME],
54  password=self._data_data[CONF_PASSWORD],
55  load_robots=load_robots,
56  subscribe_for_updates=subscribe_for_updates,
57  )
58  except LitterRobotLoginException as ex:
59  raise ConfigEntryAuthFailed("Invalid credentials") from ex
60  except LitterRobotException as ex:
61  raise ConfigEntryNotReady("Unable to connect to Litter-Robot API") from ex
62 
63  def litter_robots(self) -> Generator[LitterRobot]:
64  """Get Litter-Robots from the account."""
65  return (
66  robot for robot in self.accountaccount.robots if isinstance(robot, LitterRobot)
67  )
68 
69  def feeder_robots(self) -> Generator[FeederRobot]:
70  """Get Feeder-Robots from the account."""
71  return (
72  robot for robot in self.accountaccount.robots if isinstance(robot, FeederRobot)
73  )
None __init__(self, HomeAssistant hass, Mapping[str, Any] data)
Definition: hub.py:29
Generator[LitterRobot] litter_robots(self)
Definition: hub.py:63
None login(self, bool load_robots=False, bool subscribe_for_updates=False)
Definition: hub.py:49
Generator[FeederRobot] feeder_robots(self)
Definition: hub.py:69
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)