Home Assistant Unofficial Reference 2024.12.1
time.py
Go to the documentation of this file.
1 """Support for Roborock time."""
2 
3 import asyncio
4 from collections.abc import Callable, Coroutine
5 from dataclasses import dataclass
6 import datetime
7 from datetime import time
8 import logging
9 from typing import Any
10 
11 from roborock.command_cache import CacheableAttribute
12 from roborock.exceptions import RoborockException
13 from roborock.version_1_apis.roborock_client_v1 import AttributeCache
14 
15 from homeassistant.components.time import TimeEntity, TimeEntityDescription
16 from homeassistant.const import EntityCategory
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import HomeAssistantError
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import DOMAIN, RoborockConfigEntry
22 from .coordinator import RoborockDataUpdateCoordinator
23 from .entity import RoborockEntityV1
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
28 @dataclass(frozen=True, kw_only=True)
30  """Class to describe a Roborock time entity."""
31 
32  # Gets the status of the switch
33  cache_key: CacheableAttribute
34  # Sets the status of the switch
35  update_value: Callable[[AttributeCache, datetime.time], Coroutine[Any, Any, None]]
36  # Attribute from cache
37  get_value: Callable[[AttributeCache], datetime.time]
38 
39 
40 TIME_DESCRIPTIONS: list[RoborockTimeDescription] = [
42  key="dnd_start_time",
43  translation_key="dnd_start_time",
44  cache_key=CacheableAttribute.dnd_timer,
45  update_value=lambda cache, desired_time: cache.update_value(
46  [
47  desired_time.hour,
48  desired_time.minute,
49  cache.value.get("end_hour"),
50  cache.value.get("end_minute"),
51  ]
52  ),
53  get_value=lambda cache: datetime.time(
54  hour=cache.value.get("start_hour"), minute=cache.value.get("start_minute")
55  ),
56  entity_category=EntityCategory.CONFIG,
57  ),
59  key="dnd_end_time",
60  translation_key="dnd_end_time",
61  cache_key=CacheableAttribute.dnd_timer,
62  update_value=lambda cache, desired_time: cache.update_value(
63  [
64  cache.value.get("start_hour"),
65  cache.value.get("start_minute"),
66  desired_time.hour,
67  desired_time.minute,
68  ]
69  ),
70  get_value=lambda cache: datetime.time(
71  hour=cache.value.get("end_hour"), minute=cache.value.get("end_minute")
72  ),
73  entity_category=EntityCategory.CONFIG,
74  ),
76  key="off_peak_start",
77  translation_key="off_peak_start",
78  cache_key=CacheableAttribute.valley_electricity_timer,
79  update_value=lambda cache, desired_time: cache.update_value(
80  [
81  desired_time.hour,
82  desired_time.minute,
83  cache.value.get("end_hour"),
84  cache.value.get("end_minute"),
85  ]
86  ),
87  get_value=lambda cache: datetime.time(
88  hour=cache.value.get("start_hour"), minute=cache.value.get("start_minute")
89  ),
90  entity_category=EntityCategory.CONFIG,
91  entity_registry_enabled_default=False,
92  ),
94  key="off_peak_end",
95  translation_key="off_peak_end",
96  cache_key=CacheableAttribute.valley_electricity_timer,
97  update_value=lambda cache, desired_time: cache.update_value(
98  [
99  cache.value.get("start_hour"),
100  cache.value.get("start_minute"),
101  desired_time.hour,
102  desired_time.minute,
103  ]
104  ),
105  get_value=lambda cache: datetime.time(
106  hour=cache.value.get("end_hour"), minute=cache.value.get("end_minute")
107  ),
108  entity_category=EntityCategory.CONFIG,
109  entity_registry_enabled_default=False,
110  ),
111 ]
112 
113 
115  hass: HomeAssistant,
116  config_entry: RoborockConfigEntry,
117  async_add_entities: AddEntitiesCallback,
118 ) -> None:
119  """Set up Roborock time platform."""
120  possible_entities: list[
121  tuple[RoborockDataUpdateCoordinator, RoborockTimeDescription]
122  ] = [
123  (coordinator, description)
124  for coordinator in config_entry.runtime_data.v1
125  for description in TIME_DESCRIPTIONS
126  ]
127  # We need to check if this function is supported by the device.
128  results = await asyncio.gather(
129  *(
130  coordinator.api.get_from_cache(description.cache_key)
131  for coordinator, description in possible_entities
132  ),
133  return_exceptions=True,
134  )
135  valid_entities: list[RoborockTimeEntity] = []
136  for (coordinator, description), result in zip(
137  possible_entities, results, strict=False
138  ):
139  if result is None or isinstance(result, RoborockException):
140  _LOGGER.debug("Not adding entity because of %s", result)
141  else:
142  valid_entities.append(
144  f"{description.key}_{coordinator.duid_slug}",
145  coordinator,
146  description,
147  )
148  )
149  async_add_entities(valid_entities)
150 
151 
153  """A class to let you set options on a Roborock vacuum where the potential options are fixed."""
154 
155  entity_description: RoborockTimeDescription
156 
157  def __init__(
158  self,
159  unique_id: str,
160  coordinator: RoborockDataUpdateCoordinator,
161  entity_description: RoborockTimeDescription,
162  ) -> None:
163  """Create a time entity."""
164  self.entity_descriptionentity_description = entity_description
165  super().__init__(unique_id, coordinator.device_info, coordinator.api)
166 
167  @property
168  def native_value(self) -> time | None:
169  """Return the value reported by the time."""
170  return self.entity_descriptionentity_description.get_value(
171  self.get_cacheget_cache(self.entity_descriptionentity_description.cache_key)
172  )
173 
174  async def async_set_value(self, value: time) -> None:
175  """Set the time."""
176  try:
177  await self.entity_descriptionentity_description.update_value(
178  self.get_cacheget_cache(self.entity_descriptionentity_description.cache_key), value
179  )
180  except RoborockException as err:
181  raise HomeAssistantError(
182  translation_domain=DOMAIN,
183  translation_key="update_options_failed",
184  ) from err
AttributeCache get_cache(self, CacheableAttribute attribute)
Definition: entity.py:52
None __init__(self, str unique_id, RoborockDataUpdateCoordinator coordinator, RoborockTimeDescription entity_description)
Definition: time.py:162
float|int|str|None get_value(Sensor sensor, str field)
Definition: sensor.py:46
None async_setup_entry(HomeAssistant hass, RoborockConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: time.py:118