Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for Roborock select."""
2 
3 import asyncio
4 from collections.abc import Callable
5 from dataclasses import dataclass
6 
7 from roborock.containers import Status
8 from roborock.roborock_message import RoborockDataProtocol
9 from roborock.roborock_typing import RoborockCommand
10 
11 from homeassistant.components.select import SelectEntity, SelectEntityDescription
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import RoborockConfigEntry
17 from .const import MAP_SLEEP
18 from .coordinator import RoborockDataUpdateCoordinator
19 from .entity import RoborockCoordinatedEntityV1
20 
21 
22 @dataclass(frozen=True, kw_only=True)
24  """Class to describe a Roborock select entity."""
25 
26  # The command that the select entity will send to the api.
27  api_command: RoborockCommand
28  # Gets the current value of the select entity.
29  value_fn: Callable[[Status], str | None]
30  # Gets all options of the select entity.
31  options_lambda: Callable[[Status], list[str] | None]
32  # Takes the value from the select entity and converts it for the api.
33  parameter_lambda: Callable[[str, Status], list[int]]
34 
35  protocol_listener: RoborockDataProtocol | None = None
36 
37 
38 SELECT_DESCRIPTIONS: list[RoborockSelectDescription] = [
40  key="water_box_mode",
41  translation_key="mop_intensity",
42  api_command=RoborockCommand.SET_WATER_BOX_CUSTOM_MODE,
43  value_fn=lambda data: data.water_box_mode_name,
44  entity_category=EntityCategory.CONFIG,
45  options_lambda=lambda data: data.water_box_mode.keys()
46  if data.water_box_mode is not None
47  else None,
48  parameter_lambda=lambda key, status: [status.get_mop_intensity_code(key)],
49  protocol_listener=RoborockDataProtocol.WATER_BOX_MODE,
50  ),
52  key="mop_mode",
53  translation_key="mop_mode",
54  api_command=RoborockCommand.SET_MOP_MODE,
55  value_fn=lambda data: data.mop_mode_name,
56  entity_category=EntityCategory.CONFIG,
57  options_lambda=lambda data: data.mop_mode.keys()
58  if data.mop_mode is not None
59  else None,
60  parameter_lambda=lambda key, status: [status.get_mop_mode_code(key)],
61  ),
62 ]
63 
64 
66  hass: HomeAssistant,
67  config_entry: RoborockConfigEntry,
68  async_add_entities: AddEntitiesCallback,
69 ) -> None:
70  """Set up Roborock select platform."""
71 
73  RoborockSelectEntity(coordinator, description, options)
74  for coordinator in config_entry.runtime_data.v1
75  for description in SELECT_DESCRIPTIONS
76  if (
77  options := description.options_lambda(
78  coordinator.roborock_device_info.props.status
79  )
80  )
81  is not None
82  )
85  f"selected_map_{coordinator.duid_slug}", coordinator
86  )
87  for coordinator in config_entry.runtime_data.v1
88  )
89 
90 
92  """A class to let you set options on a Roborock vacuum where the potential options are fixed."""
93 
94  entity_description: RoborockSelectDescription
95 
96  def __init__(
97  self,
98  coordinator: RoborockDataUpdateCoordinator,
99  entity_description: RoborockSelectDescription,
100  options: list[str],
101  ) -> None:
102  """Create a select entity."""
103  self.entity_descriptionentity_description = entity_description
104  super().__init__(
105  f"{entity_description.key}_{coordinator.duid_slug}",
106  coordinator,
107  entity_description.protocol_listener,
108  )
109  self._attr_options_attr_options = options
110 
111  async def async_select_option(self, option: str) -> None:
112  """Set the option."""
113  await self.sendsendsend(
114  self.entity_descriptionentity_description.api_command,
115  self.entity_descriptionentity_description.parameter_lambda(option, self._device_status_device_status),
116  )
117 
118  @property
119  def current_option(self) -> str | None:
120  """Get the current status of the select entity from device_status."""
121  return self.entity_descriptionentity_description.value_fn(self._device_status_device_status)
122 
123 
125  """A class to let you set the selected map on Roborock vacuum."""
126 
127  _attr_entity_category = EntityCategory.DIAGNOSTIC
128  _attr_translation_key = "selected_map"
129 
130  async def async_select_option(self, option: str) -> None:
131  """Set the option."""
132  for map_id, map_ in self.coordinator.maps.items():
133  if map_.name == option:
134  await self.sendsendsend(
135  RoborockCommand.LOAD_MULTI_MAP,
136  [map_id],
137  )
138  # Update the current map id manually so that nothing gets broken
139  # if another service hits the api.
140  self.coordinator.current_map = map_id
141  # We need to wait after updating the map
142  # so that other commands will be executed correctly.
143  await asyncio.sleep(MAP_SLEEP)
144  break
145 
146  @property
147  def options(self) -> list[str]:
148  """Gets all of the names of rooms that we are currently aware of."""
149  return [roborock_map.name for roborock_map in self.coordinator.maps.values()]
150 
151  @property
152  def current_option(self) -> str | None:
153  """Get the current status of the select entity from device_status."""
154  if (
155  (current_map := self.coordinator.current_map) is not None
156  and current_map in self.coordinator.maps
157  ): # 63 means it is searching for a map.
158  return self.coordinator.maps[current_map].name
159  return None
dict send(self, RoborockCommand|str command, dict[str, Any]|list[Any]|int|None params=None)
Definition: entity.py:153
dict send(self, RoborockCommand|str command, dict[str, Any]|list[Any]|int|None params=None)
Definition: entity.py:60
None __init__(self, RoborockDataUpdateCoordinator coordinator, RoborockSelectDescription entity_description, list[str] options)
Definition: select.py:101
None async_setup_entry(HomeAssistant hass, RoborockConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: select.py:69