Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for deCONZ select entities."""
2 
3 from __future__ import annotations
4 
5 from pydeconz.models.event import EventType
6 from pydeconz.models.sensor.air_purifier import AirPurifier, AirPurifierFanMode
7 from pydeconz.models.sensor.presence import (
8  Presence,
9  PresenceConfigDeviceMode,
10  PresenceConfigSensitivity,
11  PresenceConfigTriggerDistance,
12 )
13 
14 from homeassistant.components.select import DOMAIN as SELECT_DOMAIN, SelectEntity
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import EntityCategory
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .entity import DeconzDevice
21 from .hub import DeconzHub
22 
23 SENSITIVITY_TO_DECONZ = {
24  "High": PresenceConfigSensitivity.HIGH.value,
25  "Medium": PresenceConfigSensitivity.MEDIUM.value,
26  "Low": PresenceConfigSensitivity.LOW.value,
27 }
28 DECONZ_TO_SENSITIVITY = {value: key for key, value in SENSITIVITY_TO_DECONZ.items()}
29 
30 
32  hass: HomeAssistant,
33  config_entry: ConfigEntry,
34  async_add_entities: AddEntitiesCallback,
35 ) -> None:
36  """Set up the deCONZ button entity."""
37  hub = DeconzHub.get_hub(hass, config_entry)
38  hub.entities[SELECT_DOMAIN] = set()
39 
40  @callback
41  def async_add_air_purifier_sensor(_: EventType, sensor_id: str) -> None:
42  """Add air purifier select entity from deCONZ."""
43  sensor = hub.api.sensors.air_purifier[sensor_id]
45 
46  hub.register_platform_add_device_callback(
47  async_add_air_purifier_sensor,
48  hub.api.sensors.air_purifier,
49  )
50 
51  @callback
52  def async_add_presence_sensor(_: EventType, sensor_id: str) -> None:
53  """Add presence select entity from deCONZ."""
54  sensor = hub.api.sensors.presence[sensor_id]
55  if sensor.presence_event is not None:
57  [
58  DeconzPresenceDeviceModeSelect(sensor, hub),
61  ]
62  )
63 
64  hub.register_platform_add_device_callback(
65  async_add_presence_sensor,
66  hub.api.sensors.presence,
67  )
68 
69 
70 class DeconzAirPurifierFanMode(DeconzDevice[AirPurifier], SelectEntity):
71  """Representation of a deCONZ air purifier fan mode entity."""
72 
73  _name_suffix = "Fan Mode"
74  unique_id_suffix = "fan_mode"
75  _update_key = "mode"
76 
77  _attr_entity_category = EntityCategory.CONFIG
78  _attr_options = [
79  AirPurifierFanMode.OFF.value,
80  AirPurifierFanMode.AUTO.value,
81  AirPurifierFanMode.SPEED_1.value,
82  AirPurifierFanMode.SPEED_2.value,
83  AirPurifierFanMode.SPEED_3.value,
84  AirPurifierFanMode.SPEED_4.value,
85  AirPurifierFanMode.SPEED_5.value,
86  ]
87 
88  TYPE = SELECT_DOMAIN
89 
90  @property
91  def current_option(self) -> str:
92  """Return the selected entity option to represent the entity state."""
93  return self._device.fan_mode.value
94 
95  async def async_select_option(self, option: str) -> None:
96  """Change the selected option."""
97  await self.hub.api.sensors.air_purifier.set_config(
98  id=self._device.resource_id,
99  fan_mode=AirPurifierFanMode(option),
100  )
101 
102 
103 class DeconzPresenceDeviceModeSelect(DeconzDevice[Presence], SelectEntity):
104  """Representation of a deCONZ presence device mode entity."""
105 
106  _name_suffix = "Device Mode"
107  unique_id_suffix = "device_mode"
108  _update_key = "devicemode"
109 
110  _attr_entity_category = EntityCategory.CONFIG
111  _attr_options = [
112  PresenceConfigDeviceMode.LEFT_AND_RIGHT.value,
113  PresenceConfigDeviceMode.UNDIRECTED.value,
114  ]
115 
116  TYPE = SELECT_DOMAIN
117 
118  @property
119  def current_option(self) -> str | None:
120  """Return the selected entity option to represent the entity state."""
121  if self._device.device_mode is not None:
122  return self._device.device_mode.value
123  return None
124 
125  async def async_select_option(self, option: str) -> None:
126  """Change the selected option."""
127  await self.hub.api.sensors.presence.set_config(
128  id=self._device.resource_id,
129  device_mode=PresenceConfigDeviceMode(option),
130  )
131 
132 
133 class DeconzPresenceSensitivitySelect(DeconzDevice[Presence], SelectEntity):
134  """Representation of a deCONZ presence sensitivity entity."""
135 
136  _name_suffix = "Sensitivity"
137  unique_id_suffix = "sensitivity"
138  _update_key = "sensitivity"
139 
140  _attr_entity_category = EntityCategory.CONFIG
141  _attr_options = list(SENSITIVITY_TO_DECONZ)
142 
143  TYPE = SELECT_DOMAIN
144 
145  @property
146  def current_option(self) -> str | None:
147  """Return the selected entity option to represent the entity state."""
148  if self._device.sensitivity is not None:
149  return DECONZ_TO_SENSITIVITY[self._device.sensitivity]
150  return None
151 
152  async def async_select_option(self, option: str) -> None:
153  """Change the selected option."""
154  await self.hub.api.sensors.presence.set_config(
155  id=self._device.resource_id,
156  sensitivity=SENSITIVITY_TO_DECONZ[option],
157  )
158 
159 
160 class DeconzPresenceTriggerDistanceSelect(DeconzDevice[Presence], SelectEntity):
161  """Representation of a deCONZ presence trigger distance entity."""
162 
163  _name_suffix = "Trigger Distance"
164  unique_id_suffix = "trigger_distance"
165  _update_key = "triggerdistance"
166 
167  _attr_entity_category = EntityCategory.CONFIG
168  _attr_options = [
169  PresenceConfigTriggerDistance.FAR.value,
170  PresenceConfigTriggerDistance.MEDIUM.value,
171  PresenceConfigTriggerDistance.NEAR.value,
172  ]
173 
174  TYPE = SELECT_DOMAIN
175 
176  @property
177  def current_option(self) -> str | None:
178  """Return the selected entity option to represent the entity state."""
179  if self._device.trigger_distance is not None:
180  return self._device.trigger_distance.value
181  return None
182 
183  async def async_select_option(self, option: str) -> None:
184  """Change the selected option."""
185  await self.hub.api.sensors.presence.set_config(
186  id=self._device.resource_id,
187  trigger_distance=PresenceConfigTriggerDistance(option),
188  )
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: select.py:35