Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Select for Shelly."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Final
7 
8 from aioshelly.const import RPC_GENERATIONS
9 
11  DOMAIN as SELECT_PLATFORM,
12  SelectEntity,
13  SelectEntityDescription,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .coordinator import ShellyConfigEntry, ShellyRpcCoordinator
19 from .entity import (
20  RpcEntityDescription,
21  ShellyRpcAttributeEntity,
22  async_setup_entry_rpc,
23 )
24 from .utils import (
25  async_remove_orphaned_entities,
26  get_device_entry_gen,
27  get_virtual_component_ids,
28 )
29 
30 
31 @dataclass(frozen=True, kw_only=True)
33  """Class to describe a RPC select entity."""
34 
35 
36 RPC_SELECT_ENTITIES: Final = {
37  "enum": RpcSelectDescription(
38  key="enum",
39  sub_key="value",
40  has_entity_name=True,
41  ),
42 }
43 
44 
46  hass: HomeAssistant,
47  config_entry: ShellyConfigEntry,
48  async_add_entities: AddEntitiesCallback,
49 ) -> None:
50  """Set up selectors for device."""
51  if get_device_entry_gen(config_entry) in RPC_GENERATIONS:
52  coordinator = config_entry.runtime_data.rpc
53  assert coordinator
54 
56  hass, config_entry, async_add_entities, RPC_SELECT_ENTITIES, RpcSelect
57  )
58 
59  # the user can remove virtual components from the device configuration, so
60  # we need to remove orphaned entities
61  virtual_text_ids = get_virtual_component_ids(
62  coordinator.device.config, SELECT_PLATFORM
63  )
65  hass,
66  config_entry.entry_id,
67  coordinator.mac,
68  SELECT_PLATFORM,
69  virtual_text_ids,
70  "enum",
71  )
72 
73 
75  """Represent a RPC select entity."""
76 
77  entity_description: RpcSelectDescription
78 
79  def __init__(
80  self,
81  coordinator: ShellyRpcCoordinator,
82  key: str,
83  attribute: str,
84  description: RpcSelectDescription,
85  ) -> None:
86  """Initialize select."""
87  super().__init__(coordinator, key, attribute, description)
88 
89  self._attr_options_attr_options = list(self.option_mapoption_map.values())
90 
91  @property
92  def current_option(self) -> str | None:
93  """Return the selected entity option to represent the entity state."""
94  if not isinstance(self.attribute_valueattribute_value, str):
95  return None
96 
97  return self.option_mapoption_map[self.attribute_valueattribute_value]
98 
99  async def async_select_option(self, option: str) -> None:
100  """Change the value."""
101  await self.call_rpccall_rpc(
102  "Enum.Set", {"id": self._id_id, "value": self.reversed_option_mapreversed_option_map[option]}
103  )
Any call_rpc(self, str method, Any params)
Definition: entity.py:384
None async_select_option(self, str option)
Definition: select.py:99
None __init__(self, ShellyRpcCoordinator coordinator, str key, str attribute, RpcSelectDescription description)
Definition: select.py:85
None async_setup_entry_rpc(HomeAssistant hass, ShellyConfigEntry config_entry, AddEntitiesCallback async_add_entities, Mapping[str, RpcEntityDescription] sensors, Callable sensor_class)
Definition: entity.py:142
None async_setup_entry(HomeAssistant hass, ShellyConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: select.py:49
list[str] get_virtual_component_ids(dict[str, Any] config, str platform)
Definition: utils.py:528
None async_remove_orphaned_entities(HomeAssistant hass, str config_entry_id, str mac, str platform, Iterable[str] keys, str|None key_suffix=None)
Definition: utils.py:555
int get_device_entry_gen(ConfigEntry entry)
Definition: utils.py:353