1 """Support for Roborock switch."""
3 from __future__
import annotations
6 from collections.abc
import Callable, Coroutine
7 from dataclasses
import dataclass
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 switch entity."""
33 cache_key: CacheableAttribute
35 update_value: Callable[[AttributeCache, bool], Coroutine[Any, Any,
None]]
40 SWITCH_DESCRIPTIONS: list[RoborockSwitchDescription] = [
42 cache_key=CacheableAttribute.child_lock_status,
43 update_value=
lambda cache, value: cache.update_value(
44 {
"lock_status": 1
if value
else 0}
46 attribute=
"lock_status",
48 translation_key=
"child_lock",
49 entity_category=EntityCategory.CONFIG,
52 cache_key=CacheableAttribute.flow_led_status,
53 update_value=
lambda cache, value: cache.update_value(
54 {
"status": 1
if value
else 0}
57 key=
"status_indicator",
58 translation_key=
"status_indicator",
59 entity_category=EntityCategory.CONFIG,
62 cache_key=CacheableAttribute.dnd_timer,
63 update_value=
lambda cache, value: cache.update_value(
65 cache.value.get(
"start_hour"),
66 cache.value.get(
"start_minute"),
67 cache.value.get(
"end_hour"),
68 cache.value.get(
"end_minute"),
72 else cache.close_value(),
75 translation_key=
"dnd_switch",
76 entity_category=EntityCategory.CONFIG,
79 cache_key=CacheableAttribute.valley_electricity_timer,
80 update_value=
lambda cache, value: cache.update_value(
82 cache.value.get(
"start_hour"),
83 cache.value.get(
"start_minute"),
84 cache.value.get(
"end_hour"),
85 cache.value.get(
"end_minute"),
89 else cache.close_value(),
91 key=
"off_peak_switch",
92 translation_key=
"off_peak_switch",
93 entity_category=EntityCategory.CONFIG,
94 entity_registry_enabled_default=
False,
101 config_entry: RoborockConfigEntry,
102 async_add_entities: AddEntitiesCallback,
104 """Set up Roborock switch platform."""
105 possible_entities: list[
106 tuple[RoborockDataUpdateCoordinator, RoborockSwitchDescription]
108 (coordinator, description)
109 for coordinator
in config_entry.runtime_data.v1
110 for description
in SWITCH_DESCRIPTIONS
113 results = await asyncio.gather(
115 coordinator.api.get_from_cache(description.cache_key)
116 for coordinator, description
in possible_entities
118 return_exceptions=
True,
120 valid_entities: list[RoborockSwitch] = []
121 for (coordinator, description), result
in zip(
122 possible_entities, results, strict=
False
124 if result
is None or isinstance(result, Exception):
125 _LOGGER.debug(
"Not adding entity because of %s", result)
127 valid_entities.append(
129 f
"{description.key}_{coordinator.duid_slug}",
138 """A class to let you turn functionality on Roborock devices on and off that does need a coordinator."""
140 entity_description: RoborockSwitchDescription
145 coordinator: RoborockDataUpdateCoordinator,
146 entity_description: RoborockSwitchDescription,
148 """Initialize the entity."""
150 super().
__init__(unique_id, coordinator.device_info, coordinator.api)
153 """Turn off the switch."""
158 except RoborockException
as err:
160 translation_domain=DOMAIN,
161 translation_key=
"update_options_failed",
165 """Turn on the switch."""
170 except RoborockException
as err:
172 translation_domain=DOMAIN,
173 translation_key=
"update_options_failed",
178 """Return True if entity is on."""
AttributeCache get_cache(self, CacheableAttribute attribute)
None __init__(self, str unique_id, RoborockDataUpdateCoordinator coordinator, RoborockSwitchDescription entity_description)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, RoborockConfigEntry config_entry, AddEntitiesCallback async_add_entities)