Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Plugwise Select component for Home Assistant."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from homeassistant.components.select import SelectEntity, SelectEntityDescription
8 from homeassistant.const import STATE_ON, EntityCategory
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from . import PlugwiseConfigEntry
13 from .const import SelectOptionsType, SelectType
14 from .coordinator import PlugwiseDataUpdateCoordinator
15 from .entity import PlugwiseEntity
16 from .util import plugwise_command
17 
18 
19 @dataclass(frozen=True, kw_only=True)
21  """Class describing Plugwise Select entities."""
22 
23  key: SelectType
24  options_key: SelectOptionsType
25 
26 
27 SELECT_TYPES = (
29  key="select_schedule",
30  translation_key="select_schedule",
31  options_key="available_schedules",
32  ),
34  key="select_regulation_mode",
35  translation_key="regulation_mode",
36  entity_category=EntityCategory.CONFIG,
37  options_key="regulation_modes",
38  ),
40  key="select_dhw_mode",
41  translation_key="dhw_mode",
42  entity_category=EntityCategory.CONFIG,
43  options_key="dhw_modes",
44  ),
46  key="select_gateway_mode",
47  translation_key="gateway_mode",
48  entity_category=EntityCategory.CONFIG,
49  options_key="gateway_modes",
50  ),
51 )
52 
53 
55  hass: HomeAssistant,
56  entry: PlugwiseConfigEntry,
57  async_add_entities: AddEntitiesCallback,
58 ) -> None:
59  """Set up the Smile selector from a config entry."""
60  coordinator = entry.runtime_data
61 
62  @callback
63  def _add_entities() -> None:
64  """Add Entities."""
65  if not coordinator.new_devices:
66  return
67 
69  PlugwiseSelectEntity(coordinator, device_id, description)
70  for device_id in coordinator.new_devices
71  for description in SELECT_TYPES
72  if description.options_key in coordinator.data.devices[device_id]
73  )
74 
75  _add_entities()
76  entry.async_on_unload(coordinator.async_add_listener(_add_entities))
77 
78 
80  """Represent Smile selector."""
81 
82  entity_description: PlugwiseSelectEntityDescription
83 
84  def __init__(
85  self,
86  coordinator: PlugwiseDataUpdateCoordinator,
87  device_id: str,
88  entity_description: PlugwiseSelectEntityDescription,
89  ) -> None:
90  """Initialise the selector."""
91  super().__init__(coordinator, device_id)
92  self._attr_unique_id_attr_unique_id = f"{device_id}-{entity_description.key}"
93  self.entity_descriptionentity_description = entity_description
94 
95  self._location_location = device_id
96  if (location := self.devicedevice.get("location")) is not None:
97  self._location_location = location
98 
99  @property
100  def current_option(self) -> str:
101  """Return the selected entity option to represent the entity state."""
102  return self.devicedevice[self.entity_descriptionentity_description.key]
103 
104  @property
105  def options(self) -> list[str]:
106  """Return the available select-options."""
107  return self.devicedevice[self.entity_descriptionentity_description.options_key]
108 
109  @plugwise_command
110  async def async_select_option(self, option: str) -> None:
111  """Change to the selected entity option.
112 
113  self._location and STATE_ON are required for the thermostat-schedule select.
114  """
115  await self.coordinator.api.set_select(
116  self.entity_descriptionentity_description.key, self._location_location, option, STATE_ON
117  )
None __init__(self, PlugwiseDataUpdateCoordinator coordinator, str device_id, PlugwiseSelectEntityDescription entity_description)
Definition: select.py:89
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, PlugwiseConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:58