1 """Support for Roborock time."""
4 from collections.abc
import Callable, Coroutine
5 from dataclasses
import dataclass
7 from datetime
import time
11 from roborock.command_cache
import CacheableAttribute
12 from roborock.exceptions
import RoborockException
13 from roborock.version_1_apis.roborock_client_v1
import AttributeCache
21 from .
import DOMAIN, RoborockConfigEntry
22 from .coordinator
import RoborockDataUpdateCoordinator
23 from .entity
import RoborockEntityV1
25 _LOGGER = logging.getLogger(__name__)
28 @dataclass(frozen=True, kw_only=True)
30 """Class to describe a Roborock time entity."""
33 cache_key: CacheableAttribute
35 update_value: Callable[[AttributeCache, datetime.time], Coroutine[Any, Any,
None]]
37 get_value: Callable[[AttributeCache], datetime.time]
40 TIME_DESCRIPTIONS: list[RoborockTimeDescription] = [
43 translation_key=
"dnd_start_time",
44 cache_key=CacheableAttribute.dnd_timer,
45 update_value=
lambda cache, desired_time: cache.update_value(
49 cache.value.get(
"end_hour"),
50 cache.value.get(
"end_minute"),
53 get_value=
lambda cache: datetime.time(
54 hour=cache.value.get(
"start_hour"), minute=cache.value.get(
"start_minute")
56 entity_category=EntityCategory.CONFIG,
60 translation_key=
"dnd_end_time",
61 cache_key=CacheableAttribute.dnd_timer,
62 update_value=
lambda cache, desired_time: cache.update_value(
64 cache.value.get(
"start_hour"),
65 cache.value.get(
"start_minute"),
70 get_value=
lambda cache: datetime.time(
71 hour=cache.value.get(
"end_hour"), minute=cache.value.get(
"end_minute")
73 entity_category=EntityCategory.CONFIG,
77 translation_key=
"off_peak_start",
78 cache_key=CacheableAttribute.valley_electricity_timer,
79 update_value=
lambda cache, desired_time: cache.update_value(
83 cache.value.get(
"end_hour"),
84 cache.value.get(
"end_minute"),
87 get_value=
lambda cache: datetime.time(
88 hour=cache.value.get(
"start_hour"), minute=cache.value.get(
"start_minute")
90 entity_category=EntityCategory.CONFIG,
91 entity_registry_enabled_default=
False,
95 translation_key=
"off_peak_end",
96 cache_key=CacheableAttribute.valley_electricity_timer,
97 update_value=
lambda cache, desired_time: cache.update_value(
99 cache.value.get(
"start_hour"),
100 cache.value.get(
"start_minute"),
105 get_value=
lambda cache: datetime.time(
106 hour=cache.value.get(
"end_hour"), minute=cache.value.get(
"end_minute")
108 entity_category=EntityCategory.CONFIG,
109 entity_registry_enabled_default=
False,
116 config_entry: RoborockConfigEntry,
117 async_add_entities: AddEntitiesCallback,
119 """Set up Roborock time platform."""
120 possible_entities: list[
121 tuple[RoborockDataUpdateCoordinator, RoborockTimeDescription]
123 (coordinator, description)
124 for coordinator
in config_entry.runtime_data.v1
125 for description
in TIME_DESCRIPTIONS
128 results = await asyncio.gather(
130 coordinator.api.get_from_cache(description.cache_key)
131 for coordinator, description
in possible_entities
133 return_exceptions=
True,
135 valid_entities: list[RoborockTimeEntity] = []
136 for (coordinator, description), result
in zip(
137 possible_entities, results, strict=
False
139 if result
is None or isinstance(result, RoborockException):
140 _LOGGER.debug(
"Not adding entity because of %s", result)
142 valid_entities.append(
144 f
"{description.key}_{coordinator.duid_slug}",
153 """A class to let you set options on a Roborock vacuum where the potential options are fixed."""
155 entity_description: RoborockTimeDescription
160 coordinator: RoborockDataUpdateCoordinator,
161 entity_description: RoborockTimeDescription,
163 """Create a time entity."""
165 super().
__init__(unique_id, coordinator.device_info, coordinator.api)
169 """Return the value reported by the time."""
180 except RoborockException
as err:
182 translation_domain=DOMAIN,
183 translation_key=
"update_options_failed",
AttributeCache get_cache(self, CacheableAttribute attribute)
time|None native_value(self)
None async_set_value(self, time value)
None __init__(self, str unique_id, RoborockDataUpdateCoordinator coordinator, RoborockTimeDescription entity_description)
float|int|str|None get_value(Sensor sensor, str field)
None async_setup_entry(HomeAssistant hass, RoborockConfigEntry config_entry, AddEntitiesCallback async_add_entities)