Home Assistant Unofficial Reference 2024.12.1
time.py
Go to the documentation of this file.
1 """Support for Litter-Robot time."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from dataclasses import dataclass
7 from datetime import datetime, time
8 from typing import Any, Generic
9 
10 from pylitterbot import LitterRobot3
11 
12 from homeassistant.components.time import TimeEntity, TimeEntityDescription
13 from homeassistant.const import EntityCategory
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 import homeassistant.util.dt as dt_util
17 
18 from . import LitterRobotConfigEntry
19 from .entity import LitterRobotEntity, _RobotT
20 
21 
22 @dataclass(frozen=True)
23 class RequiredKeysMixin(Generic[_RobotT]):
24  """A class that describes robot time entity required keys."""
25 
26  value_fn: Callable[[_RobotT], time | None]
27  set_fn: Callable[[_RobotT, time], Coroutine[Any, Any, bool]]
28 
29 
30 @dataclass(frozen=True)
32  """A class that describes robot time entities."""
33 
34 
35 def _as_local_time(start: datetime | None) -> time | None:
36  """Return a datetime as local time."""
37  return dt_util.as_local(start).time() if start else None
38 
39 
40 LITTER_ROBOT_3_SLEEP_START = RobotTimeEntityDescription[LitterRobot3](
41  key="sleep_mode_start_time",
42  translation_key="sleep_mode_start_time",
43  entity_category=EntityCategory.CONFIG,
44  value_fn=lambda robot: _as_local_time(robot.sleep_mode_start_time),
45  set_fn=lambda robot, value: robot.set_sleep_mode(
46  robot.sleep_mode_enabled, value.replace(tzinfo=dt_util.get_default_time_zone())
47  ),
48 )
49 
50 
52  hass: HomeAssistant,
53  entry: LitterRobotConfigEntry,
54  async_add_entities: AddEntitiesCallback,
55 ) -> None:
56  """Set up Litter-Robot cleaner using config entry."""
57  hub = entry.runtime_data
59  [
61  robot=robot, hub=hub, description=LITTER_ROBOT_3_SLEEP_START
62  )
63  for robot in hub.litter_robots()
64  if isinstance(robot, LitterRobot3)
65  ]
66  )
67 
68 
69 class LitterRobotTimeEntity(LitterRobotEntity[_RobotT], TimeEntity):
70  """Litter-Robot time entity."""
71 
72  entity_description: RobotTimeEntityDescription[_RobotT]
73 
74  @property
75  def native_value(self) -> time | None:
76  """Return the value reported by the time."""
77  return self.entity_descriptionentity_description.value_fn(self.robotrobot)
78 
79  async def async_set_value(self, value: time) -> None:
80  """Update the current value."""
81  await self.entity_descriptionentity_description.set_fn(self.robotrobot, value)
None async_setup_entry(HomeAssistant hass, LitterRobotConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: time.py:55
time|None _as_local_time(datetime|None start)
Definition: time.py:35
bool time(HomeAssistant hass, dt_time|str|None before=None, dt_time|str|None after=None, str|Container[str]|None weekday=None)
Definition: condition.py:802