Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for select entities."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from thinqconnect import DeviceType
8 from thinqconnect.devices.const import Property as ThinQProperty
9 from thinqconnect.integration import ActiveMode
10 
11 from homeassistant.components.select import SelectEntity, SelectEntityDescription
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from . import ThinqConfigEntry
16 from .coordinator import DeviceDataUpdateCoordinator
17 from .entity import ThinQEntity
18 
19 SELECT_DESC: dict[ThinQProperty, SelectEntityDescription] = {
20  ThinQProperty.MONITORING_ENABLED: SelectEntityDescription(
21  key=ThinQProperty.MONITORING_ENABLED,
22  translation_key=ThinQProperty.MONITORING_ENABLED,
23  ),
24  ThinQProperty.COOK_MODE: SelectEntityDescription(
25  key=ThinQProperty.COOK_MODE,
26  translation_key=ThinQProperty.COOK_MODE,
27  ),
28  ThinQProperty.DISPLAY_LIGHT: SelectEntityDescription(
29  key=ThinQProperty.DISPLAY_LIGHT,
30  translation_key=ThinQProperty.DISPLAY_LIGHT,
31  ),
32  ThinQProperty.CURRENT_JOB_MODE: SelectEntityDescription(
33  key=ThinQProperty.CURRENT_JOB_MODE,
34  translation_key=ThinQProperty.CURRENT_JOB_MODE,
35  ),
36  ThinQProperty.FRESH_AIR_FILTER: SelectEntityDescription(
37  key=ThinQProperty.FRESH_AIR_FILTER,
38  translation_key=ThinQProperty.FRESH_AIR_FILTER,
39  ),
40 }
41 AIR_FLOW_SELECT_DESC: dict[ThinQProperty, SelectEntityDescription] = {
42  ThinQProperty.WIND_STRENGTH: SelectEntityDescription(
43  key=ThinQProperty.WIND_STRENGTH,
44  translation_key=ThinQProperty.WIND_STRENGTH,
45  ),
46  ThinQProperty.WIND_ANGLE: SelectEntityDescription(
47  key=ThinQProperty.WIND_ANGLE,
48  translation_key=ThinQProperty.WIND_ANGLE,
49  ),
50 }
51 OPERATION_SELECT_DESC: dict[ThinQProperty, SelectEntityDescription] = {
52  ThinQProperty.AIR_CLEAN_OPERATION_MODE: SelectEntityDescription(
53  key=ThinQProperty.AIR_CLEAN_OPERATION_MODE,
54  translation_key="air_clean_operation_mode",
55  ),
56  ThinQProperty.DISH_WASHER_OPERATION_MODE: SelectEntityDescription(
57  key=ThinQProperty.DISH_WASHER_OPERATION_MODE,
58  translation_key="operation_mode",
59  ),
60  ThinQProperty.DRYER_OPERATION_MODE: SelectEntityDescription(
61  key=ThinQProperty.DRYER_OPERATION_MODE,
62  translation_key="operation_mode",
63  ),
64  ThinQProperty.HYGIENE_DRY_MODE: SelectEntityDescription(
65  key=ThinQProperty.HYGIENE_DRY_MODE,
66  translation_key=ThinQProperty.HYGIENE_DRY_MODE,
67  ),
68  ThinQProperty.LIGHT_BRIGHTNESS: SelectEntityDescription(
69  key=ThinQProperty.LIGHT_BRIGHTNESS,
70  translation_key=ThinQProperty.LIGHT_BRIGHTNESS,
71  ),
72  ThinQProperty.OVEN_OPERATION_MODE: SelectEntityDescription(
73  key=ThinQProperty.OVEN_OPERATION_MODE,
74  translation_key="operation_mode",
75  ),
76  ThinQProperty.STYLER_OPERATION_MODE: SelectEntityDescription(
77  key=ThinQProperty.STYLER_OPERATION_MODE,
78  translation_key="operation_mode",
79  ),
80  ThinQProperty.WASHER_OPERATION_MODE: SelectEntityDescription(
81  key=ThinQProperty.WASHER_OPERATION_MODE,
82  translation_key="operation_mode",
83  ),
84 }
85 
86 DEVICE_TYPE_SELECT_MAP: dict[DeviceType, tuple[SelectEntityDescription, ...]] = {
87  DeviceType.AIR_CONDITIONER: (
88  SELECT_DESC[ThinQProperty.MONITORING_ENABLED],
89  OPERATION_SELECT_DESC[ThinQProperty.AIR_CLEAN_OPERATION_MODE],
90  ),
91  DeviceType.AIR_PURIFIER_FAN: (
92  AIR_FLOW_SELECT_DESC[ThinQProperty.WIND_STRENGTH],
93  AIR_FLOW_SELECT_DESC[ThinQProperty.WIND_ANGLE],
94  SELECT_DESC[ThinQProperty.DISPLAY_LIGHT],
95  SELECT_DESC[ThinQProperty.CURRENT_JOB_MODE],
96  ),
97  DeviceType.AIR_PURIFIER: (
98  AIR_FLOW_SELECT_DESC[ThinQProperty.WIND_STRENGTH],
99  SELECT_DESC[ThinQProperty.CURRENT_JOB_MODE],
100  ),
101  DeviceType.DEHUMIDIFIER: (AIR_FLOW_SELECT_DESC[ThinQProperty.WIND_STRENGTH],),
102  DeviceType.DISH_WASHER: (
103  OPERATION_SELECT_DESC[ThinQProperty.DISH_WASHER_OPERATION_MODE],
104  ),
105  DeviceType.DRYER: (OPERATION_SELECT_DESC[ThinQProperty.DRYER_OPERATION_MODE],),
106  DeviceType.HUMIDIFIER: (
107  AIR_FLOW_SELECT_DESC[ThinQProperty.WIND_STRENGTH],
108  SELECT_DESC[ThinQProperty.DISPLAY_LIGHT],
109  SELECT_DESC[ThinQProperty.CURRENT_JOB_MODE],
110  OPERATION_SELECT_DESC[ThinQProperty.HYGIENE_DRY_MODE],
111  ),
112  DeviceType.OVEN: (
113  SELECT_DESC[ThinQProperty.COOK_MODE],
114  OPERATION_SELECT_DESC[ThinQProperty.OVEN_OPERATION_MODE],
115  ),
116  DeviceType.REFRIGERATOR: (SELECT_DESC[ThinQProperty.FRESH_AIR_FILTER],),
117  DeviceType.STYLER: (OPERATION_SELECT_DESC[ThinQProperty.STYLER_OPERATION_MODE],),
118  DeviceType.WASHCOMBO_MAIN: (
119  OPERATION_SELECT_DESC[ThinQProperty.WASHER_OPERATION_MODE],
120  ),
121  DeviceType.WASHCOMBO_MINI: (
122  OPERATION_SELECT_DESC[ThinQProperty.WASHER_OPERATION_MODE],
123  ),
124  DeviceType.WASHER: (OPERATION_SELECT_DESC[ThinQProperty.WASHER_OPERATION_MODE],),
125  DeviceType.WASHTOWER_DRYER: (
126  OPERATION_SELECT_DESC[ThinQProperty.WASHER_OPERATION_MODE],
127  ),
128  DeviceType.WASHTOWER: (
129  OPERATION_SELECT_DESC[ThinQProperty.DRYER_OPERATION_MODE],
130  OPERATION_SELECT_DESC[ThinQProperty.WASHER_OPERATION_MODE],
131  ),
132  DeviceType.WASHTOWER_WASHER: (
133  OPERATION_SELECT_DESC[ThinQProperty.WASHER_OPERATION_MODE],
134  ),
135  DeviceType.WATER_HEATER: (SELECT_DESC[ThinQProperty.CURRENT_JOB_MODE],),
136  DeviceType.WINE_CELLAR: (OPERATION_SELECT_DESC[ThinQProperty.LIGHT_BRIGHTNESS],),
137 }
138 
139 _LOGGER = logging.getLogger(__name__)
140 
141 
143  hass: HomeAssistant,
144  entry: ThinqConfigEntry,
145  async_add_entities: AddEntitiesCallback,
146 ) -> None:
147  """Set up an entry for select platform."""
148  entities: list[ThinQSelectEntity] = []
149  for coordinator in entry.runtime_data.coordinators.values():
150  if (
151  descriptions := DEVICE_TYPE_SELECT_MAP.get(
152  coordinator.api.device.device_type
153  )
154  ) is not None:
155  for description in descriptions:
156  entities.extend(
157  ThinQSelectEntity(coordinator, description, property_id)
158  for property_id in coordinator.api.get_active_idx(
159  description.key, ActiveMode.WRITABLE
160  )
161  )
162 
163  if entities:
164  async_add_entities(entities)
165 
166 
168  """Represent a thinq select platform."""
169 
170  def __init__(
171  self,
172  coordinator: DeviceDataUpdateCoordinator,
173  entity_description: SelectEntityDescription,
174  property_id: str,
175  ) -> None:
176  """Initialize a select entity."""
177  super().__init__(coordinator, entity_description, property_id)
178 
179  self._attr_options_attr_options = self.datadatadatadata.options if self.datadatadatadata.options is not None else []
180 
181  def _update_status(self) -> None:
182  """Update status itself."""
183  super()._update_status()
184 
185  if self.datadatadatadata.value:
186  self._attr_current_option_attr_current_option = str(self.datadatadatadata.value)
187  else:
188  self._attr_current_option_attr_current_option = None
189 
190  _LOGGER.debug(
191  "[%s:%s] update status: %s -> %s, options:%s",
192  self.coordinator.device_name,
193  self.property_idproperty_id,
194  self.datadatadatadata.value,
195  self.current_optioncurrent_option,
196  self.optionsoptions,
197  )
198 
199  async def async_select_option(self, option: str) -> None:
200  """Change the selected option."""
201  _LOGGER.debug(
202  "[%s:%s] async_select_option: %s",
203  self.coordinator.device_name,
204  self.property_idproperty_id,
205  option,
206  )
207  await self.async_call_apiasync_call_api(self.coordinator.api.post(self.property_idproperty_id, option))
None async_call_api(self, Coroutine[Any, Any, Any] target, Callable[[], None]|None on_fail_method=None)
Definition: entity.py:101
None __init__(self, DeviceDataUpdateCoordinator coordinator, SelectEntityDescription entity_description, str property_id)
Definition: select.py:175
None async_setup_entry(HomeAssistant hass, ThinqConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:146