Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for Litter-Robot button."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from dataclasses import dataclass
7 import itertools
8 from typing import Any, Generic
9 
10 from pylitterbot import FeederRobot, LitterRobot3
11 
12 from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
13 from homeassistant.const import EntityCategory
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import LitterRobotConfigEntry
18 from .entity import LitterRobotEntity, _RobotT
19 
20 
22  hass: HomeAssistant,
23  entry: LitterRobotConfigEntry,
24  async_add_entities: AddEntitiesCallback,
25 ) -> None:
26  """Set up Litter-Robot cleaner using config entry."""
27  hub = entry.runtime_data
28  entities: list[LitterRobotButtonEntity] = list(
29  itertools.chain(
30  (
32  robot=robot, hub=hub, description=LITTER_ROBOT_BUTTON
33  )
34  for robot in hub.litter_robots()
35  if isinstance(robot, LitterRobot3)
36  ),
37  (
39  robot=robot, hub=hub, description=FEEDER_ROBOT_BUTTON
40  )
41  for robot in hub.feeder_robots()
42  ),
43  )
44  )
45  async_add_entities(entities)
46 
47 
48 @dataclass(frozen=True)
49 class RequiredKeysMixin(Generic[_RobotT]):
50  """A class that describes robot button entity required keys."""
51 
52  press_fn: Callable[[_RobotT], Coroutine[Any, Any, bool]]
53 
54 
55 @dataclass(frozen=True)
57  """A class that describes robot button entities."""
58 
59 
60 LITTER_ROBOT_BUTTON = RobotButtonEntityDescription[LitterRobot3](
61  key="reset_waste_drawer",
62  translation_key="reset_waste_drawer",
63  entity_category=EntityCategory.CONFIG,
64  press_fn=lambda robot: robot.reset_waste_drawer(),
65 )
66 FEEDER_ROBOT_BUTTON = RobotButtonEntityDescription[FeederRobot](
67  key="give_snack",
68  translation_key="give_snack",
69  press_fn=lambda robot: robot.give_snack(),
70 )
71 
72 
73 class LitterRobotButtonEntity(LitterRobotEntity[_RobotT], ButtonEntity):
74  """Litter-Robot button entity."""
75 
76  entity_description: RobotButtonEntityDescription[_RobotT]
77 
78  async def async_press(self) -> None:
79  """Press the button."""
80  await self.entity_descriptionentity_description.press_fn(self.robotrobot)
81  self.coordinator.async_set_updated_data(True)
None async_setup_entry(HomeAssistant hass, LitterRobotConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:25