Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Select platform for Sensibo integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import TYPE_CHECKING, Any
8 
9 from pysensibo.model import SensiboDevice
10 
11 from homeassistant.components.select import SelectEntity, SelectEntityDescription
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import HomeAssistantError
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import SensiboConfigEntry
17 from .const import DOMAIN
18 from .coordinator import SensiboDataUpdateCoordinator
19 from .entity import SensiboDeviceBaseEntity, async_handle_api_call
20 
21 PARALLEL_UPDATES = 0
22 
23 
24 @dataclass(frozen=True, kw_only=True)
26  """Class describing Sensibo Select entities."""
27 
28  data_key: str
29  value_fn: Callable[[SensiboDevice], str | None]
30  options_fn: Callable[[SensiboDevice], list[str] | None]
31  transformation: Callable[[SensiboDevice], dict | None]
32 
33 
34 DEVICE_SELECT_TYPES = (
36  key="horizontalSwing",
37  data_key="horizontal_swing_mode",
38  value_fn=lambda data: data.horizontal_swing_mode,
39  options_fn=lambda data: data.horizontal_swing_modes,
40  translation_key="horizontalswing",
41  transformation=lambda data: data.horizontal_swing_modes_translated,
42  ),
44  key="light",
45  data_key="light_mode",
46  value_fn=lambda data: data.light_mode,
47  options_fn=lambda data: data.light_modes,
48  translation_key="light",
49  transformation=lambda data: data.light_modes_translated,
50  ),
51 )
52 
53 
55  hass: HomeAssistant,
56  entry: SensiboConfigEntry,
57  async_add_entities: AddEntitiesCallback,
58 ) -> None:
59  """Set up Sensibo number platform."""
60 
61  coordinator = entry.runtime_data
62 
64  SensiboSelect(coordinator, device_id, description)
65  for device_id, device_data in coordinator.data.parsed.items()
66  for description in DEVICE_SELECT_TYPES
67  if description.key in device_data.full_features
68  )
69 
70 
72  """Representation of a Sensibo Select."""
73 
74  entity_description: SensiboSelectEntityDescription
75 
76  def __init__(
77  self,
78  coordinator: SensiboDataUpdateCoordinator,
79  device_id: str,
80  entity_description: SensiboSelectEntityDescription,
81  ) -> None:
82  """Initiate Sensibo Select."""
83  super().__init__(coordinator, device_id)
84  self.entity_descriptionentity_description = entity_description
85  self._attr_unique_id_attr_unique_id = f"{device_id}-{entity_description.key}"
86 
87  @property
88  def current_option(self) -> str | None:
89  """Return the current selected option."""
90  return self.entity_descriptionentity_description.value_fn(self.device_datadevice_data)
91 
92  @property
93  def options(self) -> list[str]:
94  """Return possible options."""
95  options = self.entity_descriptionentity_description.options_fn(self.device_datadevice_data)
96  if TYPE_CHECKING:
97  assert options is not None
98  return options
99 
100  async def async_select_option(self, option: str) -> None:
101  """Set state to the selected option."""
102  if self.entity_descriptionentity_description.key not in self.device_datadevice_data.active_features:
103  hvac_mode = self.device_datadevice_data.hvac_mode if self.device_datadevice_data.hvac_mode else ""
104  raise HomeAssistantError(
105  translation_domain=DOMAIN,
106  translation_key="select_option_not_available",
107  translation_placeholders={
108  "hvac_mode": hvac_mode,
109  "key": self.entity_descriptionentity_description.key,
110  },
111  )
112 
113  await self.async_send_api_callasync_send_api_call(
114  key=self.entity_descriptionentity_description.data_key,
115  value=option,
116  )
117 
118  @async_handle_api_call
119  async def async_send_api_call(self, key: str, value: Any) -> bool:
120  """Make service call to api."""
121  transformation = self.entity_descriptionentity_description.transformation(self.device_datadevice_data)
122  if TYPE_CHECKING:
123  assert transformation is not None
124 
125  data = {
126  "name": self.entity_descriptionentity_description.key,
127  "value": value,
128  "ac_states": self.device_datadevice_data.ac_states,
129  "assumed_state": False,
130  }
131  result = await self._client_client.async_set_ac_state_property(
132  self._device_id_device_id,
133  data["name"],
134  transformation[data["value"]],
135  data["ac_states"],
136  data["assumed_state"],
137  )
138  return bool(result.get("result", {}).get("status") == "Success")
None __init__(self, SensiboDataUpdateCoordinator coordinator, str device_id, SensiboSelectEntityDescription entity_description)
Definition: select.py:81
bool async_send_api_call(self, str key, Any value)
Definition: select.py:119
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, SensiboConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:58