Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support led_brightness for Mi Air Humidifier."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass, field
6 import logging
7 from typing import NamedTuple
8 
9 from miio.fan_common import LedBrightness as FanLedBrightness
10 from miio.integrations.airpurifier.dmaker.airfresh_t2017 import (
11  DisplayOrientation as AirfreshT2017DisplayOrientation,
12  PtcLevel as AirfreshT2017PtcLevel,
13 )
14 from miio.integrations.airpurifier.zhimi.airfresh import (
15  LedBrightness as AirfreshLedBrightness,
16 )
17 from miio.integrations.airpurifier.zhimi.airpurifier import (
18  LedBrightness as AirpurifierLedBrightness,
19  OperationMode as AirpurifierOperationMode,
20 )
21 from miio.integrations.airpurifier.zhimi.airpurifier_miot import (
22  LedBrightness as AirpurifierMiotLedBrightness,
23 )
24 from miio.integrations.humidifier.zhimi.airhumidifier import (
25  LedBrightness as AirhumidifierLedBrightness,
26 )
27 from miio.integrations.humidifier.zhimi.airhumidifier_miot import (
28  LedBrightness as AirhumidifierMiotLedBrightness,
29 )
30 
31 from homeassistant.components.select import SelectEntity, SelectEntityDescription
32 from homeassistant.config_entries import ConfigEntry
33 from homeassistant.const import CONF_DEVICE, CONF_MODEL, EntityCategory
34 from homeassistant.core import HomeAssistant, callback
35 from homeassistant.helpers.entity_platform import AddEntitiesCallback
36 
37 from .const import (
38  CONF_FLOW_TYPE,
39  DOMAIN,
40  KEY_COORDINATOR,
41  KEY_DEVICE,
42  MODEL_AIRFRESH_T2017,
43  MODEL_AIRFRESH_VA2,
44  MODEL_AIRFRESH_VA4,
45  MODEL_AIRHUMIDIFIER_CA1,
46  MODEL_AIRHUMIDIFIER_CA4,
47  MODEL_AIRHUMIDIFIER_CB1,
48  MODEL_AIRHUMIDIFIER_V1,
49  MODEL_AIRPURIFIER_3,
50  MODEL_AIRPURIFIER_3H,
51  MODEL_AIRPURIFIER_4,
52  MODEL_AIRPURIFIER_4_PRO,
53  MODEL_AIRPURIFIER_M1,
54  MODEL_AIRPURIFIER_M2,
55  MODEL_AIRPURIFIER_MA2,
56  MODEL_AIRPURIFIER_PROH,
57  MODEL_AIRPURIFIER_PROH_EU,
58  MODEL_AIRPURIFIER_ZA1,
59  MODEL_FAN_SA1,
60  MODEL_FAN_V2,
61  MODEL_FAN_V3,
62  MODEL_FAN_ZA1,
63  MODEL_FAN_ZA3,
64  MODEL_FAN_ZA4,
65 )
66 from .entity import XiaomiCoordinatedMiioEntity
67 
68 ATTR_DISPLAY_ORIENTATION = "display_orientation"
69 ATTR_LED_BRIGHTNESS = "led_brightness"
70 ATTR_PTC_LEVEL = "ptc_level"
71 ATTR_MODE = "mode"
72 
73 _LOGGER = logging.getLogger(__name__)
74 
75 
76 @dataclass(frozen=True)
78  """A class that describes select entities."""
79 
80  attr_name: str = ""
81  options_map: dict = field(default_factory=dict)
82  set_method: str = ""
83  set_method_error_message: str = ""
84 
85 
86 class AttributeEnumMapping(NamedTuple):
87  """Class to mapping Attribute to Enum Class."""
88 
89  attr_name: str
90  enum_class: type
91 
92 
93 MODEL_TO_ATTR_MAP: dict[str, list] = {
94  MODEL_AIRFRESH_T2017: [
95  AttributeEnumMapping(ATTR_DISPLAY_ORIENTATION, AirfreshT2017DisplayOrientation),
96  AttributeEnumMapping(ATTR_PTC_LEVEL, AirfreshT2017PtcLevel),
97  ],
98  MODEL_AIRFRESH_VA2: [
99  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirfreshLedBrightness)
100  ],
101  MODEL_AIRFRESH_VA4: [
102  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirfreshLedBrightness)
103  ],
104  MODEL_AIRHUMIDIFIER_CA1: [
105  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirhumidifierLedBrightness)
106  ],
107  MODEL_AIRHUMIDIFIER_CA4: [
108  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirhumidifierMiotLedBrightness)
109  ],
110  MODEL_AIRHUMIDIFIER_CB1: [
111  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirhumidifierLedBrightness)
112  ],
113  MODEL_AIRHUMIDIFIER_V1: [
114  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirhumidifierLedBrightness)
115  ],
116  MODEL_AIRPURIFIER_MA2: [AttributeEnumMapping(ATTR_MODE, AirpurifierOperationMode)],
117  MODEL_AIRPURIFIER_3: [
118  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirpurifierMiotLedBrightness)
119  ],
120  MODEL_AIRPURIFIER_ZA1: [
121  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirpurifierMiotLedBrightness)
122  ],
123  MODEL_AIRPURIFIER_3H: [
124  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirpurifierMiotLedBrightness)
125  ],
126  MODEL_AIRPURIFIER_4: [
127  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirpurifierMiotLedBrightness)
128  ],
129  MODEL_AIRPURIFIER_4_PRO: [
130  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirpurifierMiotLedBrightness)
131  ],
132  MODEL_AIRPURIFIER_M1: [
133  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirpurifierLedBrightness)
134  ],
135  MODEL_AIRPURIFIER_M2: [
136  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirpurifierLedBrightness)
137  ],
138  MODEL_AIRPURIFIER_PROH: [
139  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirpurifierMiotLedBrightness)
140  ],
141  MODEL_AIRPURIFIER_PROH_EU: [
142  AttributeEnumMapping(ATTR_LED_BRIGHTNESS, AirpurifierMiotLedBrightness)
143  ],
144  MODEL_FAN_SA1: [AttributeEnumMapping(ATTR_LED_BRIGHTNESS, FanLedBrightness)],
145  MODEL_FAN_V2: [AttributeEnumMapping(ATTR_LED_BRIGHTNESS, FanLedBrightness)],
146  MODEL_FAN_V3: [AttributeEnumMapping(ATTR_LED_BRIGHTNESS, FanLedBrightness)],
147  MODEL_FAN_ZA1: [AttributeEnumMapping(ATTR_LED_BRIGHTNESS, FanLedBrightness)],
148  MODEL_FAN_ZA3: [AttributeEnumMapping(ATTR_LED_BRIGHTNESS, FanLedBrightness)],
149  MODEL_FAN_ZA4: [AttributeEnumMapping(ATTR_LED_BRIGHTNESS, FanLedBrightness)],
150 }
151 
152 SELECTOR_TYPES = (
154  key=ATTR_DISPLAY_ORIENTATION,
155  attr_name=ATTR_DISPLAY_ORIENTATION,
156  name="Display Orientation",
157  options_map={
158  "Portrait": "Forward",
159  "LandscapeLeft": "Left",
160  "LandscapeRight": "Right",
161  },
162  set_method="set_display_orientation",
163  set_method_error_message="Setting the display orientation failed.",
164  icon="mdi:tablet",
165  translation_key="display_orientation",
166  options=["forward", "left", "right"],
167  entity_category=EntityCategory.CONFIG,
168  ),
170  key=ATTR_MODE,
171  attr_name=ATTR_MODE,
172  name="Mode",
173  set_method="set_mode",
174  set_method_error_message="Setting the mode of the fan failed.",
175  icon="mdi:fan",
176  translation_key="airpurifier_mode",
177  options=["silent", "auto", "favorite"],
178  entity_category=EntityCategory.CONFIG,
179  ),
181  key=ATTR_LED_BRIGHTNESS,
182  attr_name=ATTR_LED_BRIGHTNESS,
183  name="Led Brightness",
184  set_method="set_led_brightness",
185  set_method_error_message="Setting the led brightness failed.",
186  icon="mdi:brightness-6",
187  translation_key="led_brightness",
188  options=["bright", "dim", "off"],
189  entity_category=EntityCategory.CONFIG,
190  ),
192  key=ATTR_PTC_LEVEL,
193  attr_name=ATTR_PTC_LEVEL,
194  name="Auxiliary Heat Level",
195  set_method="set_ptc_level",
196  set_method_error_message="Setting the ptc level failed.",
197  icon="mdi:fire-circle",
198  translation_key="ptc_level",
199  options=["low", "medium", "high"],
200  entity_category=EntityCategory.CONFIG,
201  ),
202 )
203 
204 
206  hass: HomeAssistant,
207  config_entry: ConfigEntry,
208  async_add_entities: AddEntitiesCallback,
209 ) -> None:
210  """Set up the Selectors from a config entry."""
211  if config_entry.data[CONF_FLOW_TYPE] != CONF_DEVICE:
212  return
213 
214  model = config_entry.data[CONF_MODEL]
215  if model not in MODEL_TO_ATTR_MAP:
216  return
217 
218  unique_id = config_entry.unique_id
219  device = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE]
220  coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR]
221  attributes = MODEL_TO_ATTR_MAP[model]
222 
225  device,
226  config_entry,
227  f"{description.key}_{unique_id}",
228  coordinator,
229  description,
230  attribute.enum_class,
231  )
232  for description in SELECTOR_TYPES
233  for attribute in attributes
234  if description.key == attribute.attr_name
235  )
236 
237 
239  """Representation of a generic Xiaomi attribute selector."""
240 
241  def __init__(self, device, entry, unique_id, coordinator, description):
242  """Initialize the generic Xiaomi attribute selector."""
243  super().__init__(device, entry, unique_id, coordinator)
244  self.entity_descriptionentity_description = description
245 
246 
248  """Representation of a Xiaomi generic selector."""
249 
250  entity_description: XiaomiMiioSelectDescription
251 
252  def __init__(self, device, entry, unique_id, coordinator, description, enum_class):
253  """Initialize the generic Xiaomi attribute selector."""
254  super().__init__(device, entry, unique_id, coordinator, description)
255  self._current_attr_current_attr = enum_class(
256  self._extract_value_from_attribute(
257  self.coordinator.data, self.entity_descriptionentity_description.attr_name
258  )
259  )
260 
261  if description.options_map:
262  self._options_map_options_map = {}
263  for key, val in enum_class._member_map_.items(): # noqa: SLF001
264  self._options_map_options_map[description.options_map[key]] = val
265  else:
266  self._options_map_options_map = enum_class._member_map_ # noqa: SLF001
267  self._reverse_map_reverse_map = {val: key for key, val in self._options_map_options_map.items()}
268  self._enum_class_enum_class = enum_class
269 
270  @callback
272  """Fetch state from the device."""
273  try:
274  value = self._extract_value_from_attribute(
275  self.coordinator.data, self.entity_descriptionentity_description.attr_name
276  )
277  attr = self._enum_class_enum_class(value)
278  except ValueError: # if the value does not exist in
279  _LOGGER.debug(
280  "Value '%s' does not exist in enum %s", value, self._enum_class_enum_class
281  )
282  attr = None
283 
284  if attr is not None:
285  self._current_attr_current_attr = attr
286  self.async_write_ha_stateasync_write_ha_state()
287 
288  @property
289  def current_option(self) -> str | None:
290  """Return the current option."""
291  option = self._reverse_map_reverse_map.get(self._current_attr_current_attr)
292  if option is not None:
293  return option.lower()
294  return None
295 
296  async def async_select_option(self, option: str) -> None:
297  """Set an option of the miio device."""
298  await self.async_set_attrasync_set_attr(option.title())
299 
300  async def async_set_attr(self, attr_value: str):
301  """Set attr."""
302  method = getattr(self._device, self.entity_descriptionentity_description.set_method)
303  if await self._try_command(
304  self.entity_descriptionentity_description.set_method_error_message,
305  method,
306  self._enum_class_enum_class(self._options_map_options_map[attr_value]),
307  ):
308  self._current_attr_current_attr = self._options_map_options_map[attr_value]
309  self.async_write_ha_stateasync_write_ha_state()
def __init__(self, device, entry, unique_id, coordinator, description, enum_class)
Definition: select.py:252
def __init__(self, device, entry, unique_id, coordinator, description)
Definition: select.py:241
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: select.py:209