diff --git a/homeassistant/components/tolo/select.py b/homeassistant/components/tolo/select.py index 8d574f9dff5afc073f20f8ef9d59d134e6dd2ac7..f88152d6fa8fd9a74aea5072d0180697bbcbda6a 100644 --- a/homeassistant/components/tolo/select.py +++ b/homeassistant/components/tolo/select.py @@ -2,9 +2,12 @@ from __future__ import annotations -from tololib import LampMode +from collections.abc import Callable +from dataclasses import dataclass -from homeassistant.components.select import SelectEntity +from tololib import AromaTherapySlot, LampMode, ToloClient, ToloSettings + +from homeassistant.components.select import SelectEntity, SelectEntityDescription from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant @@ -14,6 +17,37 @@ from . import ToloSaunaCoordinatorEntity, ToloSaunaUpdateCoordinator from .const import DOMAIN +@dataclass(frozen=True, kw_only=True) +class ToloSelectEntityDescription(SelectEntityDescription): + """Class describing TOLO select entities.""" + + options: list[str] + getter: Callable[[ToloSettings], str] + setter: Callable[[ToloClient, str], bool] + + +SELECTS = ( + ToloSelectEntityDescription( + key="lamp_mode", + translation_key="lamp_mode", + options=[lamp_mode.name.lower() for lamp_mode in LampMode], + getter=lambda settings: settings.lamp_mode.name.lower(), + setter=lambda client, option: client.set_lamp_mode(LampMode[option.upper()]), + ), + ToloSelectEntityDescription( + key="aroma_therapy_slot", + translation_key="aroma_therapy_slot", + options=[ + aroma_therapy_slot.name.lower() for aroma_therapy_slot in AromaTherapySlot + ], + getter=lambda settings: settings.aroma_therapy_slot.name.lower(), + setter=lambda client, option: client.set_aroma_therapy_slot( + AromaTherapySlot[option.upper()] + ), + ), +) + + async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, @@ -21,29 +55,39 @@ async def async_setup_entry( ) -> None: """Set up select entities for TOLO Sauna.""" coordinator = hass.data[DOMAIN][entry.entry_id] - async_add_entities([ToloLampModeSelect(coordinator, entry)]) + async_add_entities( + ToloSelectEntity(coordinator, entry, description) for description in SELECTS + ) -class ToloLampModeSelect(ToloSaunaCoordinatorEntity, SelectEntity): - """TOLO Sauna lamp mode select.""" +class ToloSelectEntity(ToloSaunaCoordinatorEntity, SelectEntity): + """TOLO select entity.""" _attr_entity_category = EntityCategory.CONFIG - _attr_options = [lamp_mode.name.lower() for lamp_mode in LampMode] - _attr_translation_key = "lamp_mode" + + entity_description: ToloSelectEntityDescription def __init__( - self, coordinator: ToloSaunaUpdateCoordinator, entry: ConfigEntry + self, + coordinator: ToloSaunaUpdateCoordinator, + entry: ConfigEntry, + entity_description: ToloSelectEntityDescription, ) -> None: - """Initialize lamp mode select entity.""" + """Initialize TOLO select entity.""" super().__init__(coordinator, entry) + self.entity_description = entity_description + self._attr_unique_id = f"{entry.entry_id}_{entity_description.key}" - self._attr_unique_id = f"{entry.entry_id}_lamp_mode" + @property + def options(self) -> list[str]: + """Return available select options.""" + return self.entity_description.options @property def current_option(self) -> str: - """Return current lamp mode.""" - return self.coordinator.data.settings.lamp_mode.name.lower() + """Return current select option.""" + return self.entity_description.getter(self.coordinator.data.settings) def select_option(self, option: str) -> None: - """Select lamp mode.""" - self.coordinator.client.set_lamp_mode(LampMode[option.upper()]) + """Select a select option.""" + self.entity_description.setter(self.coordinator.client, option) diff --git a/homeassistant/components/tolo/strings.json b/homeassistant/components/tolo/strings.json index deab432425fefe14b0a6c0fc09bda51b895d8015..c55498b8d9200d6a9b233c616f8b800b949aa42c 100644 --- a/homeassistant/components/tolo/strings.json +++ b/homeassistant/components/tolo/strings.json @@ -61,6 +61,13 @@ "automatic": "Automatic", "manual": "Manual" } + }, + "aroma_therapy_slot": { + "name": "Aroma therapy slot", + "state": { + "a": "Slot A", + "b": "Slot B" + } } }, "sensor": {