Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for the Airzone Cloud select."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any, Final
8 
9 from aioairzone_cloud.common import AirQualityMode, OperationMode
10 from aioairzone_cloud.const import (
11  API_AQ_MODE_CONF,
12  API_MODE,
13  API_VALUE,
14  AZD_AQ_MODE_CONF,
15  AZD_MASTER,
16  AZD_MODE,
17  AZD_MODES,
18  AZD_ZONES,
19 )
20 
21 from homeassistant.components.select import SelectEntity, SelectEntityDescription
22 from homeassistant.const import EntityCategory
23 from homeassistant.core import HomeAssistant, callback
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 
26 from . import AirzoneCloudConfigEntry
27 from .coordinator import AirzoneUpdateCoordinator
28 from .entity import AirzoneEntity, AirzoneZoneEntity
29 
30 
31 @dataclass(frozen=True, kw_only=True)
33  """Class to describe an Airzone select entity."""
34 
35  api_param: str
36  options_dict: dict[str, Any]
37  options_fn: Callable[[dict[str, Any], dict[str, Any]], list[str]] = (
38  lambda zone_data, value: list(value)
39  )
40 
41 
42 AIR_QUALITY_MAP: Final[dict[str, str]] = {
43  "off": AirQualityMode.OFF,
44  "on": AirQualityMode.ON,
45  "auto": AirQualityMode.AUTO,
46 }
47 
48 MODE_MAP: Final[dict[str, int]] = {
49  "cool": OperationMode.COOLING,
50  "dry": OperationMode.DRY,
51  "fan": OperationMode.VENTILATION,
52  "heat": OperationMode.HEATING,
53  "heat_cool": OperationMode.AUTO,
54  "stop": OperationMode.STOP,
55 }
56 
57 
59  zone_data: dict[str, Any],
60  options: dict[str, int],
61 ) -> list[str]:
62  """Filter available modes."""
63  modes = zone_data.get(AZD_MODES, [])
64  return [k for k, v in options.items() if v in modes]
65 
66 
67 MAIN_ZONE_SELECT_TYPES: Final[tuple[AirzoneSelectDescription, ...]] = (
69  api_param=API_MODE,
70  key=AZD_MODE,
71  options_dict=MODE_MAP,
72  options_fn=main_zone_options,
73  translation_key="modes",
74  ),
75 )
76 
77 
78 ZONE_SELECT_TYPES: Final[tuple[AirzoneSelectDescription, ...]] = (
80  api_param=API_AQ_MODE_CONF,
81  entity_category=EntityCategory.CONFIG,
82  key=AZD_AQ_MODE_CONF,
83  options=list(AIR_QUALITY_MAP),
84  options_dict=AIR_QUALITY_MAP,
85  translation_key="air_quality",
86  ),
87 )
88 
89 
91  hass: HomeAssistant,
92  entry: AirzoneCloudConfigEntry,
93  async_add_entities: AddEntitiesCallback,
94 ) -> None:
95  """Add Airzone Cloud select from a config_entry."""
96  coordinator = entry.runtime_data
97 
98  # Zones
99  entities: list[AirzoneZoneSelect] = [
101  coordinator,
102  description,
103  zone_id,
104  zone_data,
105  )
106  for description in MAIN_ZONE_SELECT_TYPES
107  for zone_id, zone_data in coordinator.data.get(AZD_ZONES, {}).items()
108  if description.key in zone_data and zone_data.get(AZD_MASTER)
109  ]
110 
111  entities.extend(
113  coordinator,
114  description,
115  zone_id,
116  zone_data,
117  )
118  for description in ZONE_SELECT_TYPES
119  for zone_id, zone_data in coordinator.data.get(AZD_ZONES, {}).items()
120  if description.key in zone_data
121  )
122 
123  async_add_entities(entities)
124 
125 
127  """Define an Airzone Cloud select."""
128 
129  entity_description: AirzoneSelectDescription
130  values_dict: dict[str, str]
131 
132  @callback
133  def _handle_coordinator_update(self) -> None:
134  """Update attributes when the coordinator updates."""
135  self._async_update_attrs_async_update_attrs()
137 
138  def _get_current_option(self) -> str | None:
139  """Get current selected option."""
140  value = self.get_airzone_valueget_airzone_value(self.entity_description.key)
141  return self.values_dict.get(value)
142 
143  @callback
144  def _async_update_attrs(self) -> None:
145  """Update select attributes."""
146  self._attr_current_option_attr_current_option = self._get_current_option_get_current_option()
147 
148 
150  """Define an Airzone Cloud Zone select."""
151 
152  def __init__(
153  self,
154  coordinator: AirzoneUpdateCoordinator,
155  description: AirzoneSelectDescription,
156  zone_id: str,
157  zone_data: dict[str, Any],
158  ) -> None:
159  """Initialize."""
160  super().__init__(coordinator, zone_id, zone_data)
161 
162  self._attr_unique_id_attr_unique_id = f"{zone_id}_{description.key}"
163  self.entity_descriptionentity_description = description
164 
165  self._attr_options_attr_options = self.entity_descriptionentity_description.options_fn(
166  zone_data, description.options_dict
167  )
168 
169  self.values_dictvalues_dict = {v: k for k, v in description.options_dict.items()}
170 
171  self._async_update_attrs_async_update_attrs()
172 
173  async def async_select_option(self, option: str) -> None:
174  """Change the selected option."""
175  param = self.entity_descriptionentity_description.api_param
176  value = self.entity_descriptionentity_description.options_dict[option]
177  params: dict[str, Any] = {}
178  params[param] = {
179  API_VALUE: value,
180  }
181  await self._async_update_params_async_update_params_async_update_params(params)
None _async_update_params(self, dict[str, Any] params)
Definition: entity.py:53
None _async_update_params(self, dict[str, Any] params)
Definition: entity.py:327
None __init__(self, AirzoneUpdateCoordinator coordinator, AirzoneSelectDescription description, str zone_id, dict[str, Any] zone_data)
Definition: select.py:158
None async_setup_entry(HomeAssistant hass, AirzoneCloudConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:94
list[str] main_zone_options(dict[str, Any] zone_data, dict[str, int] options)
Definition: select.py:61
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88