Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Support for ZHA controls using the select platform."""
2 
3 from __future__ import annotations
4 
5 import functools
6 import logging
7 from typing import Any
8 
9 from homeassistant.components.select import SelectEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform
12 from homeassistant.core import HomeAssistant, State, callback
13 from homeassistant.helpers.dispatcher import async_dispatcher_connect
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .entity import ZHAEntity
17 from .helpers import (
18  SIGNAL_ADD_ENTITIES,
19  EntityData,
20  async_add_entities as zha_async_add_entities,
21  convert_zha_error_to_ha_error,
22  get_zha_data,
23 )
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up the Zigbee Home Automation siren from config entry."""
34  zha_data = get_zha_data(hass)
35  entities_to_create = zha_data.platforms[Platform.SELECT]
36 
38  hass,
39  SIGNAL_ADD_ENTITIES,
40  functools.partial(
41  zha_async_add_entities,
42  async_add_entities,
43  ZHAEnumSelectEntity,
44  entities_to_create,
45  ),
46  )
47  config_entry.async_on_unload(unsub)
48 
49 
51  """Representation of a ZHA select entity."""
52 
53  def __init__(self, entity_data: EntityData, **kwargs: Any) -> None:
54  """Initialize the ZHA select entity."""
55  super().__init__(entity_data, **kwargs)
56  self._attr_options_attr_options = self.entity_data.entity.info_object.options
57 
58  @property
59  def current_option(self) -> str | None:
60  """Return the selected entity option to represent the entity state."""
61  return self.entity_data.entity.current_option
62 
63  @convert_zha_error_to_ha_error
64  async def async_select_option(self, option: str) -> None:
65  """Change the selected option."""
66  await self.entity_data.entity.async_select_option(option=option)
67  self.async_write_ha_stateasync_write_ha_state()
68 
69  @callback
70  def restore_external_state_attributes(self, state: State) -> None:
71  """Restore entity state."""
72  if state.state and state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
73  self.entity_data.entity.restore_external_state_attributes(
74  state=state.state,
75  )
None restore_external_state_attributes(self, State state)
Definition: select.py:70
None __init__(self, EntityData entity_data, **Any kwargs)
Definition: select.py:53
HAZHAData get_zha_data(HomeAssistant hass)
Definition: helpers.py:1020
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: select.py:32
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103