Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Litter-Robot integration."""
2 
3 from __future__ import annotations
4 
5 from pylitterbot import FeederRobot, LitterRobot, LitterRobot3, LitterRobot4, Robot
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.device_registry import DeviceEntry
11 
12 from .const import DOMAIN
13 from .hub import LitterRobotHub
14 
15 type LitterRobotConfigEntry = ConfigEntry[LitterRobotHub]
16 
17 PLATFORMS_BY_TYPE = {
18  Robot: (
19  Platform.BINARY_SENSOR,
20  Platform.SELECT,
21  Platform.SENSOR,
22  Platform.SWITCH,
23  ),
24  LitterRobot: (Platform.VACUUM,),
25  LitterRobot3: (Platform.BUTTON, Platform.TIME),
26  LitterRobot4: (Platform.UPDATE,),
27  FeederRobot: (Platform.BUTTON,),
28 }
29 
30 
31 def get_platforms_for_robots(robots: list[Robot]) -> set[Platform]:
32  """Get platforms for robots."""
33  return {
34  platform
35  for robot in robots
36  for robot_type, platforms in PLATFORMS_BY_TYPE.items()
37  if isinstance(robot, robot_type)
38  for platform in platforms
39  }
40 
41 
42 async def async_setup_entry(hass: HomeAssistant, entry: LitterRobotConfigEntry) -> bool:
43  """Set up Litter-Robot from a config entry."""
44  hub = LitterRobotHub(hass, entry.data)
45  await hub.login(load_robots=True, subscribe_for_updates=True)
46  entry.runtime_data = hub
47 
48  if platforms := get_platforms_for_robots(hub.account.robots):
49  await hass.config_entries.async_forward_entry_setups(entry, platforms)
50  return True
51 
52 
54  hass: HomeAssistant, entry: LitterRobotConfigEntry
55 ) -> bool:
56  """Unload a config entry."""
57  await entry.runtime_data.account.disconnect()
58 
59  platforms = get_platforms_for_robots(entry.runtime_data.account.robots)
60  return await hass.config_entries.async_unload_platforms(entry, platforms)
61 
62 
64  hass: HomeAssistant, entry: LitterRobotConfigEntry, device_entry: DeviceEntry
65 ) -> bool:
66  """Remove a config entry from a device."""
67  return not any(
68  identifier
69  for identifier in device_entry.identifiers
70  if identifier[0] == DOMAIN
71  for robot in entry.runtime_data.account.robots
72  if robot.serial == identifier[1]
73  )
set[Platform] get_platforms_for_robots(list[Robot] robots)
Definition: __init__.py:31
bool async_unload_entry(HomeAssistant hass, LitterRobotConfigEntry entry)
Definition: __init__.py:55
bool async_remove_config_entry_device(HomeAssistant hass, LitterRobotConfigEntry entry, DeviceEntry device_entry)
Definition: __init__.py:65
bool async_setup_entry(HomeAssistant hass, LitterRobotConfigEntry entry)
Definition: __init__.py:42