Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """TOLO Sauna Select controls."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from tololib import ToloClient, ToloSettings
9 
10 from homeassistant.components.select import SelectEntity, SelectEntityDescription
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import DOMAIN, AromaTherapySlot, LampMode
17 from .coordinator import ToloSaunaUpdateCoordinator
18 from .entity import ToloSaunaCoordinatorEntity
19 
20 
21 @dataclass(frozen=True, kw_only=True)
23  """Class describing TOLO select entities."""
24 
25  options: list[str]
26  getter: Callable[[ToloSettings], str]
27  setter: Callable[[ToloClient, str], bool]
28 
29 
30 SELECTS = (
32  key="lamp_mode",
33  translation_key="lamp_mode",
34  options=[lamp_mode.name.lower() for lamp_mode in LampMode],
35  getter=lambda settings: settings.lamp_mode.name.lower(),
36  setter=lambda client, option: client.set_lamp_mode(
37  LampMode[option.upper()].value
38  ),
39  ),
41  key="aroma_therapy_slot",
42  translation_key="aroma_therapy_slot",
43  options=[
44  aroma_therapy_slot.name.lower() for aroma_therapy_slot in AromaTherapySlot
45  ],
46  getter=lambda settings: settings.aroma_therapy_slot.name.lower(),
47  setter=lambda client, option: client.set_aroma_therapy_slot(
48  AromaTherapySlot[option.upper()].value
49  ),
50  ),
51 )
52 
53 
55  hass: HomeAssistant,
56  entry: ConfigEntry,
57  async_add_entities: AddEntitiesCallback,
58 ) -> None:
59  """Set up select entities for TOLO Sauna."""
60  coordinator = hass.data[DOMAIN][entry.entry_id]
62  ToloSelectEntity(coordinator, entry, description) for description in SELECTS
63  )
64 
65 
67  """TOLO select entity."""
68 
69  _attr_entity_category = EntityCategory.CONFIG
70 
71  entity_description: ToloSelectEntityDescription
72 
73  def __init__(
74  self,
75  coordinator: ToloSaunaUpdateCoordinator,
76  entry: ConfigEntry,
77  entity_description: ToloSelectEntityDescription,
78  ) -> None:
79  """Initialize TOLO select entity."""
80  super().__init__(coordinator, entry)
81  self.entity_descriptionentity_description = entity_description
82  self._attr_unique_id_attr_unique_id = f"{entry.entry_id}_{entity_description.key}"
83 
84  @property
85  def options(self) -> list[str]:
86  """Return available select options."""
87  return self.entity_descriptionentity_description.options
88 
89  @property
90  def current_option(self) -> str:
91  """Return current select option."""
92  return self.entity_descriptionentity_description.getter(self.coordinator.data.settings)
93 
94  def select_option(self, option: str) -> None:
95  """Select a select option."""
96  self.entity_descriptionentity_description.setter(self.coordinator.client, option)
None __init__(self, ToloSaunaUpdateCoordinator coordinator, ConfigEntry entry, ToloSelectEntityDescription entity_description)
Definition: select.py:78
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: select.py:58