Add aroma therapy select entity for tolo integration (#113442)

* add select entity to allow aroma therapy slot selection

* improved translation readability
This commit is contained in:
Matthias Lohr 2024-03-14 21:03:41 +01:00 committed by GitHub
parent 3f2a51bcff
commit 566cbc71a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 65 additions and 14 deletions

View File

@ -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)

View File

@ -61,6 +61,13 @@
"automatic": "Automatic",
"manual": "Manual"
}
},
"aroma_therapy_slot": {
"name": "Aroma therapy slot",
"state": {
"a": "Slot A",
"b": "Slot B"
}
}
},
"sensor": {