Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Litter-Robot binary sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Generic
8 
9 from pylitterbot import LitterRobot, Robot
10 
12  BinarySensorDeviceClass,
13  BinarySensorEntity,
14  BinarySensorEntityDescription,
15 )
16 from homeassistant.const import EntityCategory
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import LitterRobotConfigEntry
21 from .entity import LitterRobotEntity, _RobotT
22 
23 
24 @dataclass(frozen=True)
25 class RequiredKeysMixin(Generic[_RobotT]):
26  """A class that describes robot binary sensor entity required keys."""
27 
28  is_on_fn: Callable[[_RobotT], bool]
29 
30 
31 @dataclass(frozen=True)
33  BinarySensorEntityDescription, RequiredKeysMixin[_RobotT]
34 ):
35  """A class that describes robot binary sensor entities."""
36 
37 
38 class LitterRobotBinarySensorEntity(LitterRobotEntity[_RobotT], BinarySensorEntity):
39  """Litter-Robot binary sensor entity."""
40 
41  entity_description: RobotBinarySensorEntityDescription[_RobotT]
42 
43  @property
44  def is_on(self) -> bool:
45  """Return the state."""
46  return self.entity_descriptionentity_description.is_on_fn(self.robotrobot)
47 
48 
49 BINARY_SENSOR_MAP: dict[type[Robot], tuple[RobotBinarySensorEntityDescription, ...]] = {
50  LitterRobot: ( # type: ignore[type-abstract] # only used for isinstance check
51  RobotBinarySensorEntityDescription[LitterRobot](
52  key="sleeping",
53  translation_key="sleeping",
54  entity_category=EntityCategory.DIAGNOSTIC,
55  entity_registry_enabled_default=False,
56  is_on_fn=lambda robot: robot.is_sleeping,
57  ),
58  RobotBinarySensorEntityDescription[LitterRobot](
59  key="sleep_mode",
60  translation_key="sleep_mode",
61  entity_category=EntityCategory.DIAGNOSTIC,
62  entity_registry_enabled_default=False,
63  is_on_fn=lambda robot: robot.sleep_mode_enabled,
64  ),
65  ),
66  Robot: ( # type: ignore[type-abstract] # only used for isinstance check
67  RobotBinarySensorEntityDescription[Robot](
68  key="power_status",
69  translation_key="power_status",
70  device_class=BinarySensorDeviceClass.PLUG,
71  entity_category=EntityCategory.DIAGNOSTIC,
72  entity_registry_enabled_default=False,
73  is_on_fn=lambda robot: robot.power_status == "AC",
74  ),
75  ),
76 }
77 
78 
80  hass: HomeAssistant,
81  entry: LitterRobotConfigEntry,
82  async_add_entities: AddEntitiesCallback,
83 ) -> None:
84  """Set up Litter-Robot binary sensors using config entry."""
85  hub = entry.runtime_data
87  LitterRobotBinarySensorEntity(robot=robot, hub=hub, description=description)
88  for robot in hub.account.robots
89  for robot_type, entity_descriptions in BINARY_SENSOR_MAP.items()
90  if isinstance(robot, robot_type)
91  for description in entity_descriptions
92  )
None async_setup_entry(HomeAssistant hass, LitterRobotConfigEntry entry, AddEntitiesCallback async_add_entities)