1 """Support for Litter-Robot sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import datetime
8 from typing
import Any, Generic, cast
10 from pylitterbot
import FeederRobot, LitterRobot, LitterRobot4, Robot
15 SensorEntityDescription,
22 from .
import LitterRobotConfigEntry
23 from .entity
import LitterRobotEntity, _RobotT
27 """Return a gauge icon valid identifier."""
28 if gauge_level
is None or gauge_level <= 0 + offset:
29 return "mdi:gauge-empty"
30 if gauge_level > 70 + offset:
31 return "mdi:gauge-full"
32 if gauge_level > 30 + offset:
34 return "mdi:gauge-low"
37 @dataclass(frozen=True)
39 """A class that describes robot sensor entities."""
41 icon_fn: Callable[[Any], str |
None] =
lambda _:
None
42 should_report: Callable[[_RobotT], bool] =
lambda _:
True
46 """Litter-Robot sensor entity."""
48 entity_description: RobotSensorEntityDescription[_RobotT]
52 """Return the state."""
56 return cast(float | datetime |
None, val)
60 def icon(self) -> str | None:
61 """Return the icon to use in the frontend, if any."""
67 ROBOT_SENSOR_MAP: dict[type[Robot], list[RobotSensorEntityDescription]] = {
69 RobotSensorEntityDescription[LitterRobot](
70 key=
"waste_drawer_level",
71 translation_key=
"waste_drawer",
72 native_unit_of_measurement=PERCENTAGE,
74 state_class=SensorStateClass.MEASUREMENT,
76 RobotSensorEntityDescription[LitterRobot](
77 key=
"sleep_mode_start_time",
78 translation_key=
"sleep_mode_start_time",
79 device_class=SensorDeviceClass.TIMESTAMP,
80 should_report=
lambda robot: robot.sleep_mode_enabled,
82 RobotSensorEntityDescription[LitterRobot](
83 key=
"sleep_mode_end_time",
84 translation_key=
"sleep_mode_end_time",
85 device_class=SensorDeviceClass.TIMESTAMP,
86 should_report=
lambda robot: robot.sleep_mode_enabled,
88 RobotSensorEntityDescription[LitterRobot](
90 translation_key=
"last_seen",
91 device_class=SensorDeviceClass.TIMESTAMP,
92 entity_category=EntityCategory.DIAGNOSTIC,
94 RobotSensorEntityDescription[LitterRobot](
96 translation_key=
"status_code",
97 entity_category=EntityCategory.DIAGNOSTIC,
98 device_class=SensorDeviceClass.ENUM,
129 RobotSensorEntityDescription[LitterRobot4](
131 translation_key=
"litter_level",
132 native_unit_of_measurement=PERCENTAGE,
134 state_class=SensorStateClass.MEASUREMENT,
136 RobotSensorEntityDescription[LitterRobot4](
138 translation_key=
"pet_weight",
139 native_unit_of_measurement=UnitOfMass.POUNDS,
140 device_class=SensorDeviceClass.WEIGHT,
141 state_class=SensorStateClass.MEASUREMENT,
145 RobotSensorEntityDescription[FeederRobot](
147 translation_key=
"food_level",
148 native_unit_of_measurement=PERCENTAGE,
150 state_class=SensorStateClass.MEASUREMENT,
158 entry: LitterRobotConfigEntry,
159 async_add_entities: AddEntitiesCallback,
161 """Set up Litter-Robot sensors using config entry."""
162 hub = entry.runtime_data
165 for robot
in hub.account.robots
166 for robot_type, entity_descriptions
in ROBOT_SENSOR_MAP.items()
167 if isinstance(robot, robot_type)
168 for description
in entity_descriptions
float|datetime|str|None native_value(self)
str icon_for_gauge_level(int|None gauge_level=None, int offset=0)
None async_setup_entry(HomeAssistant hass, LitterRobotConfigEntry entry, AddEntitiesCallback async_add_entities)