Skip to content
Snippets Groups Projects
Unverified Commit 566cbc71 authored by Matthias Lohr's avatar Matthias Lohr Committed by GitHub
Browse files

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

* add select entity to allow aroma therapy slot selection

* improved translation readability
parent 3f2a51bc
Branches
Tags
No related merge requests found
...@@ -2,9 +2,12 @@ ...@@ -2,9 +2,12 @@
from __future__ import annotations 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.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
...@@ -14,6 +17,37 @@ from . import ToloSaunaCoordinatorEntity, ToloSaunaUpdateCoordinator ...@@ -14,6 +17,37 @@ from . import ToloSaunaCoordinatorEntity, ToloSaunaUpdateCoordinator
from .const import DOMAIN 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( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: ConfigEntry,
...@@ -21,29 +55,39 @@ async def async_setup_entry( ...@@ -21,29 +55,39 @@ async def async_setup_entry(
) -> None: ) -> None:
"""Set up select entities for TOLO Sauna.""" """Set up select entities for TOLO Sauna."""
coordinator = hass.data[DOMAIN][entry.entry_id] 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): class ToloSelectEntity(ToloSaunaCoordinatorEntity, SelectEntity):
"""TOLO Sauna lamp mode select.""" """TOLO select entity."""
_attr_entity_category = EntityCategory.CONFIG _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__( def __init__(
self, coordinator: ToloSaunaUpdateCoordinator, entry: ConfigEntry self,
coordinator: ToloSaunaUpdateCoordinator,
entry: ConfigEntry,
entity_description: ToloSelectEntityDescription,
) -> None: ) -> None:
"""Initialize lamp mode select entity.""" """Initialize TOLO select entity."""
super().__init__(coordinator, entry) 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 @property
def current_option(self) -> str: def current_option(self) -> str:
"""Return current lamp mode.""" """Return current select option."""
return self.coordinator.data.settings.lamp_mode.name.lower() return self.entity_description.getter(self.coordinator.data.settings)
def select_option(self, option: str) -> None: def select_option(self, option: str) -> None:
"""Select lamp mode.""" """Select a select option."""
self.coordinator.client.set_lamp_mode(LampMode[option.upper()]) self.entity_description.setter(self.coordinator.client, option)
...@@ -61,6 +61,13 @@ ...@@ -61,6 +61,13 @@
"automatic": "Automatic", "automatic": "Automatic",
"manual": "Manual" "manual": "Manual"
} }
},
"aroma_therapy_slot": {
"name": "Aroma therapy slot",
"state": {
"a": "Slot A",
"b": "Slot B"
}
} }
}, },
"sensor": { "sensor": {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment