Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for KNX/IP select entities."""
2 
3 from __future__ import annotations
4 
5 from xknx import XKNX
6 from xknx.devices import Device as XknxDevice, RawValue
7 
8 from homeassistant import config_entries
9 from homeassistant.components.select import SelectEntity
10 from homeassistant.const import (
11  CONF_ENTITY_CATEGORY,
12  CONF_NAME,
13  CONF_PAYLOAD,
14  STATE_UNAVAILABLE,
15  STATE_UNKNOWN,
16  Platform,
17 )
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.helpers.restore_state import RestoreEntity
21 from homeassistant.helpers.typing import ConfigType
22 
23 from . import KNXModule
24 from .const import (
25  CONF_PAYLOAD_LENGTH,
26  CONF_RESPOND_TO_READ,
27  CONF_STATE_ADDRESS,
28  CONF_SYNC_STATE,
29  KNX_ADDRESS,
30  KNX_MODULE_KEY,
31 )
32 from .entity import KnxYamlEntity
33 from .schema import SelectSchema
34 
35 
37  hass: HomeAssistant,
38  config_entry: config_entries.ConfigEntry,
39  async_add_entities: AddEntitiesCallback,
40 ) -> None:
41  """Set up select(s) for KNX platform."""
42  knx_module = hass.data[KNX_MODULE_KEY]
43  config: list[ConfigType] = knx_module.config_yaml[Platform.SELECT]
44 
45  async_add_entities(KNXSelect(knx_module, entity_config) for entity_config in config)
46 
47 
48 def _create_raw_value(xknx: XKNX, config: ConfigType) -> RawValue:
49  """Return a KNX RawValue to be used within XKNX."""
50  return RawValue(
51  xknx,
52  name=config[CONF_NAME],
53  payload_length=config[CONF_PAYLOAD_LENGTH],
54  group_address=config[KNX_ADDRESS],
55  group_address_state=config.get(CONF_STATE_ADDRESS),
56  respond_to_read=config[CONF_RESPOND_TO_READ],
57  sync_state=config[CONF_SYNC_STATE],
58  )
59 
60 
62  """Representation of a KNX select."""
63 
64  _device: RawValue
65 
66  def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
67  """Initialize a KNX select."""
68  super().__init__(
69  knx_module=knx_module,
70  device=_create_raw_value(knx_module.xknx, config),
71  )
72  self._option_payloads: dict[str, int] = {
73  option[SelectSchema.CONF_OPTION]: option[CONF_PAYLOAD]
74  for option in config[SelectSchema.CONF_OPTIONS]
75  }
76  self._attr_options_attr_options = list(self._option_payloads)
77  self._attr_current_option_attr_current_option = None
78  self._attr_entity_category_attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
79  self._attr_unique_id_attr_unique_id = str(self._device_device.remote_value.group_address)
80 
81  async def async_added_to_hass(self) -> None:
82  """Restore last state."""
83  await super().async_added_to_hass()
84  if not self._device_device.remote_value.readable and (
85  last_state := await self.async_get_last_stateasync_get_last_state()
86  ):
87  if (
88  last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE)
89  and (option := self._option_payloads.get(last_state.state)) is not None
90  ):
91  self._device_device.remote_value.update_value(option)
92 
93  def after_update_callback(self, device: XknxDevice) -> None:
94  """Call after device was updated."""
95  self._attr_current_option_attr_current_option = self.option_from_payloadoption_from_payload(
96  self._device_device.remote_value.value
97  )
98  super().after_update_callback(device)
99 
100  def option_from_payload(self, payload: int | None) -> str | None:
101  """Return the option a given payload is assigned to."""
102  try:
103  return next(
104  key for key, value in self._option_payloads.items() if value == payload
105  )
106  except StopIteration:
107  return None
108 
109  async def async_select_option(self, option: str) -> None:
110  """Change the selected option."""
111  payload = self._option_payloads[option]
112  await self._device_device.set(payload)
None __init__(self, KNXModule knx_module, ConfigType config)
Definition: select.py:66
str|None option_from_payload(self, int|None payload)
Definition: select.py:100
None async_select_option(self, str option)
Definition: select.py:109
None after_update_callback(self, XknxDevice device)
Definition: select.py:93
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: select.py:40
RawValue _create_raw_value(XKNX xknx, ConfigType config)
Definition: select.py:48