Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for Overkiz select."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from dataclasses import dataclass
7 
8 from pyoverkiz.enums import OverkizCommand, OverkizCommandParam, OverkizState
9 
10 from homeassistant.components.select import SelectEntity, SelectEntityDescription
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import HomeAssistantOverkizData
17 from .const import DOMAIN, IGNORED_OVERKIZ_DEVICES
18 from .entity import OverkizDescriptiveEntity
19 
20 
21 @dataclass(frozen=True, kw_only=True)
23  """Class to describe an Overkiz select entity."""
24 
25  select_option: Callable[[str, Callable[..., Awaitable[None]]], Awaitable[None]]
26 
27 
29  option: str, execute_command: Callable[..., Awaitable[None]]
30 ) -> Awaitable[None]:
31  """Change the selected option for Open/Closed/Pedestrian."""
32  return execute_command(
33  {
34  OverkizCommandParam.CLOSED: OverkizCommand.CLOSE,
35  OverkizCommandParam.OPEN: OverkizCommand.OPEN,
36  OverkizCommandParam.PEDESTRIAN: OverkizCommand.SET_PEDESTRIAN_POSITION,
37  }[OverkizCommandParam(option)]
38  )
39 
40 
42  option: str, execute_command: Callable[..., Awaitable[None]]
43 ) -> Awaitable[None]:
44  """Change the selected option for Open/Closed/Partial."""
45  return execute_command(
46  {
47  OverkizCommandParam.CLOSED: OverkizCommand.CLOSE,
48  OverkizCommandParam.OPEN: OverkizCommand.OPEN,
49  OverkizCommandParam.PARTIAL: OverkizCommand.PARTIAL_POSITION,
50  }[OverkizCommandParam(option)]
51  )
52 
53 
55  option: str, execute_command: Callable[..., Awaitable[None]]
56 ) -> Awaitable[None]:
57  """Change the selected option for Memorized Simple Volume."""
58  return execute_command(OverkizCommand.SET_MEMORIZED_SIMPLE_VOLUME, option)
59 
60 
62  option: str, execute_command: Callable[..., Awaitable[None]]
63 ) -> Awaitable[None]:
64  """Change the selected option for Active Zone(s)."""
65  # Turn alarm off when empty zone is selected
66  if option == "":
67  return execute_command(OverkizCommand.ALARM_OFF)
68 
69  return execute_command(OverkizCommand.ALARM_ZONE_ON, option)
70 
71 
72 SELECT_DESCRIPTIONS: list[OverkizSelectDescription] = [
74  key=OverkizState.CORE_OPEN_CLOSED_PEDESTRIAN,
75  name="Position",
76  icon="mdi:content-save-cog",
77  options=[
78  OverkizCommandParam.OPEN,
79  OverkizCommandParam.PEDESTRIAN,
80  OverkizCommandParam.CLOSED,
81  ],
82  select_option=_select_option_open_closed_pedestrian,
83  translation_key="open_closed_pedestrian",
84  ),
86  key=OverkizState.CORE_OPEN_CLOSED_PARTIAL,
87  name="Position",
88  icon="mdi:content-save-cog",
89  options=[
90  OverkizCommandParam.OPEN,
91  OverkizCommandParam.PARTIAL,
92  OverkizCommandParam.CLOSED,
93  ],
94  select_option=_select_option_open_closed_partial,
95  translation_key="open_closed_partial",
96  ),
98  key=OverkizState.IO_MEMORIZED_SIMPLE_VOLUME,
99  name="Memorized simple volume",
100  icon="mdi:volume-high",
101  options=[OverkizCommandParam.STANDARD, OverkizCommandParam.HIGHEST],
102  select_option=_select_option_memorized_simple_volume,
103  entity_category=EntityCategory.CONFIG,
104  translation_key="memorized_simple_volume",
105  ),
106  # SomfyHeatingTemperatureInterface
108  key=OverkizState.OVP_HEATING_TEMPERATURE_INTERFACE_OPERATING_MODE,
109  name="Operating mode",
110  icon="mdi:sun-snowflake",
111  options=[OverkizCommandParam.HEATING, OverkizCommandParam.COOLING],
112  select_option=lambda option, execute_command: execute_command(
113  OverkizCommand.SET_OPERATING_MODE, option
114  ),
115  entity_category=EntityCategory.CONFIG,
116  ),
117  # StatefulAlarmController
119  key=OverkizState.CORE_ACTIVE_ZONES,
120  name="Active zones",
121  icon="mdi:shield-lock",
122  options=["", "A", "B", "C", "A,B", "B,C", "A,C", "A,B,C"],
123  select_option=_select_option_active_zone,
124  ),
125 ]
126 
127 SUPPORTED_STATES = {description.key: description for description in SELECT_DESCRIPTIONS}
128 
129 
131  hass: HomeAssistant,
132  entry: ConfigEntry,
133  async_add_entities: AddEntitiesCallback,
134 ) -> None:
135  """Set up the Overkiz select from a config entry."""
136  data: HomeAssistantOverkizData = hass.data[DOMAIN][entry.entry_id]
137  entities: list[OverkizSelect] = []
138 
139  for device in data.coordinator.data.values():
140  if (
141  device.widget in IGNORED_OVERKIZ_DEVICES
142  or device.ui_class in IGNORED_OVERKIZ_DEVICES
143  ):
144  continue
145 
146  entities.extend(
148  device.device_url,
149  data.coordinator,
150  description,
151  )
152  for state in device.definition.states
153  if (description := SUPPORTED_STATES.get(state.qualified_name))
154  )
155 
156  async_add_entities(entities)
157 
158 
160  """Representation of an Overkiz Select entity."""
161 
162  entity_description: OverkizSelectDescription
163 
164  @property
165  def current_option(self) -> str | None:
166  """Return the selected entity option to represent the entity state."""
167  if state := self.devicedevice.states.get(self.entity_descriptionentity_description.key):
168  return str(state.value)
169 
170  return None
171 
172  async def async_select_option(self, option: str) -> None:
173  """Change the selected option."""
174  await self.entity_descriptionentity_description.select_option(
175  option, self.executorexecutor.async_execute_command
176  )
Awaitable[None] _select_option_active_zone(str option, Callable[..., Awaitable[None]] execute_command)
Definition: select.py:63
Awaitable[None] _select_option_open_closed_partial(str option, Callable[..., Awaitable[None]] execute_command)
Definition: select.py:43
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:134
Awaitable[None] _select_option_open_closed_pedestrian(str option, Callable[..., Awaitable[None]] execute_command)
Definition: select.py:30
Awaitable[None] _select_option_memorized_simple_volume(str option, Callable[..., Awaitable[None]] execute_command)
Definition: select.py:56