1 """Support led_brightness for Mi Air Humidifier."""
3 from __future__
import annotations
5 from dataclasses
import dataclass, field
7 from typing
import NamedTuple
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,
14 from miio.integrations.airpurifier.zhimi.airfresh
import (
15 LedBrightness
as AirfreshLedBrightness,
17 from miio.integrations.airpurifier.zhimi.airpurifier
import (
18 LedBrightness
as AirpurifierLedBrightness,
19 OperationMode
as AirpurifierOperationMode,
21 from miio.integrations.airpurifier.zhimi.airpurifier_miot
import (
22 LedBrightness
as AirpurifierMiotLedBrightness,
24 from miio.integrations.humidifier.zhimi.airhumidifier
import (
25 LedBrightness
as AirhumidifierLedBrightness,
27 from miio.integrations.humidifier.zhimi.airhumidifier_miot
import (
28 LedBrightness
as AirhumidifierMiotLedBrightness,
45 MODEL_AIRHUMIDIFIER_CA1,
46 MODEL_AIRHUMIDIFIER_CA4,
47 MODEL_AIRHUMIDIFIER_CB1,
48 MODEL_AIRHUMIDIFIER_V1,
52 MODEL_AIRPURIFIER_4_PRO,
55 MODEL_AIRPURIFIER_MA2,
56 MODEL_AIRPURIFIER_PROH,
57 MODEL_AIRPURIFIER_PROH_EU,
58 MODEL_AIRPURIFIER_ZA1,
66 from .entity
import XiaomiCoordinatedMiioEntity
68 ATTR_DISPLAY_ORIENTATION =
"display_orientation"
69 ATTR_LED_BRIGHTNESS =
"led_brightness"
70 ATTR_PTC_LEVEL =
"ptc_level"
73 _LOGGER = logging.getLogger(__name__)
76 @dataclass(frozen=True)
78 """A class that describes select entities."""
81 options_map: dict = field(default_factory=dict)
83 set_method_error_message: str =
""
87 """Class to mapping Attribute to Enum Class."""
93 MODEL_TO_ATTR_MAP: dict[str, list] = {
94 MODEL_AIRFRESH_T2017: [
101 MODEL_AIRFRESH_VA4: [
104 MODEL_AIRHUMIDIFIER_CA1: [
107 MODEL_AIRHUMIDIFIER_CA4: [
110 MODEL_AIRHUMIDIFIER_CB1: [
113 MODEL_AIRHUMIDIFIER_V1: [
117 MODEL_AIRPURIFIER_3: [
120 MODEL_AIRPURIFIER_ZA1: [
123 MODEL_AIRPURIFIER_3H: [
126 MODEL_AIRPURIFIER_4: [
129 MODEL_AIRPURIFIER_4_PRO: [
132 MODEL_AIRPURIFIER_M1: [
135 MODEL_AIRPURIFIER_M2: [
138 MODEL_AIRPURIFIER_PROH: [
141 MODEL_AIRPURIFIER_PROH_EU: [
154 key=ATTR_DISPLAY_ORIENTATION,
155 attr_name=ATTR_DISPLAY_ORIENTATION,
156 name=
"Display Orientation",
158 "Portrait":
"Forward",
159 "LandscapeLeft":
"Left",
160 "LandscapeRight":
"Right",
162 set_method=
"set_display_orientation",
163 set_method_error_message=
"Setting the display orientation failed.",
165 translation_key=
"display_orientation",
166 options=[
"forward",
"left",
"right"],
167 entity_category=EntityCategory.CONFIG,
173 set_method=
"set_mode",
174 set_method_error_message=
"Setting the mode of the fan failed.",
176 translation_key=
"airpurifier_mode",
177 options=[
"silent",
"auto",
"favorite"],
178 entity_category=EntityCategory.CONFIG,
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,
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,
207 config_entry: ConfigEntry,
208 async_add_entities: AddEntitiesCallback,
210 """Set up the Selectors from a config entry."""
211 if config_entry.data[CONF_FLOW_TYPE] != CONF_DEVICE:
214 model = config_entry.data[CONF_MODEL]
215 if model
not in MODEL_TO_ATTR_MAP:
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]
227 f
"{description.key}_{unique_id}",
230 attribute.enum_class,
232 for description
in SELECTOR_TYPES
233 for attribute
in attributes
234 if description.key == attribute.attr_name
239 """Representation of a generic Xiaomi attribute selector."""
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)
248 """Representation of a Xiaomi generic selector."""
250 entity_description: XiaomiMiioSelectDescription
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)
256 self._extract_value_from_attribute(
261 if description.options_map:
263 for key, val
in enum_class._member_map_.items():
264 self.
_options_map_options_map[description.options_map[key]] = val
272 """Fetch state from the device."""
274 value = self._extract_value_from_attribute(
280 "Value '%s' does not exist in enum %s", value, self.
_enum_class_enum_class
290 """Return the current option."""
292 if option
is not None:
293 return option.lower()
297 """Set an option of the miio device."""
303 if await self._try_command(
def async_set_attr(self, str attr_value)
str|None current_option(self)
None async_select_option(self, str option)
def __init__(self, device, entry, unique_id, coordinator, description, enum_class)
def _handle_coordinator_update(self)
def __init__(self, device, entry, unique_id, coordinator, description)
None async_write_ha_state(self)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)