diff --git a/.coveragerc b/.coveragerc index 54d543dfbf3..aff0979ca1d 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1429,6 +1429,7 @@ omit = homeassistant/components/tolo/number.py homeassistant/components/tolo/select.py homeassistant/components/tolo/sensor.py + homeassistant/components/tolo/switch.py homeassistant/components/toon/__init__.py homeassistant/components/toon/binary_sensor.py homeassistant/components/toon/climate.py diff --git a/homeassistant/components/tolo/__init__.py b/homeassistant/components/tolo/__init__.py index 165e5804d61..5fdcdea6c30 100644 --- a/homeassistant/components/tolo/__init__.py +++ b/homeassistant/components/tolo/__init__.py @@ -29,6 +29,7 @@ PLATFORMS = [ Platform.NUMBER, Platform.SELECT, Platform.SENSOR, + Platform.SWITCH, ] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/tolo/icons.json b/homeassistant/components/tolo/icons.json index 011df5788b2..7aeec065b41 100644 --- a/homeassistant/components/tolo/icons.json +++ b/homeassistant/components/tolo/icons.json @@ -42,6 +42,11 @@ "fan_timer_remaining": { "default": "mdi:fan-auto" } + }, + "switch": { + "aroma_therapy_on": { + "default": "mdi:scent" + } } } } diff --git a/homeassistant/components/tolo/strings.json b/homeassistant/components/tolo/strings.json index f48e26c5276..deab432425f 100644 --- a/homeassistant/components/tolo/strings.json +++ b/homeassistant/components/tolo/strings.json @@ -79,6 +79,14 @@ "fan_timer_remaining": { "name": "Fan timer" } + }, + "switch": { + "aroma_therapy_on": { + "name": "Aroma therapy" + }, + "salt_bath_on": { + "name": "Salt bath" + } } } } diff --git a/homeassistant/components/tolo/switch.py b/homeassistant/components/tolo/switch.py new file mode 100644 index 00000000000..b90f548ee76 --- /dev/null +++ b/homeassistant/components/tolo/switch.py @@ -0,0 +1,83 @@ +"""TOLO Sauna switch controls.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +from typing import Any + +from tololib import ToloClient, ToloStatus + +from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import ToloSaunaCoordinatorEntity, ToloSaunaUpdateCoordinator +from .const import DOMAIN + + +@dataclass(frozen=True, kw_only=True) +class ToloSwitchEntityDescription(SwitchEntityDescription): + """Class describing TOLO switch entities.""" + + getter: Callable[[ToloStatus], bool] + setter: Callable[[ToloClient, bool], bool] + + +SWITCHES = ( + ToloSwitchEntityDescription( + key="aroma_therapy_on", + translation_key="aroma_therapy_on", + getter=lambda status: status.aroma_therapy_on, + setter=lambda client, value: client.set_aroma_therapy_on(value), + ), + ToloSwitchEntityDescription( + key="salt_bath_on", + translation_key="salt_bath_on", + getter=lambda status: status.salt_bath_on, + setter=lambda client, value: client.set_salt_bath_on(value), + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up switch controls for TOLO Sauna.""" + coordinator = hass.data[DOMAIN][entry.entry_id] + async_add_entities( + ToloSwitchEntity(coordinator, entry, description) for description in SWITCHES + ) + + +class ToloSwitchEntity(ToloSaunaCoordinatorEntity, SwitchEntity): + """TOLO switch entity.""" + + entity_description: ToloSwitchEntityDescription + + def __init__( + self, + coordinator: ToloSaunaUpdateCoordinator, + entry: ConfigEntry, + entity_description: ToloSwitchEntityDescription, + ) -> None: + """Initialize TOLO switch entity.""" + super().__init__(coordinator, entry) + self.entity_description = entity_description + self._attr_unique_id = f"{entry.entry_id}_{entity_description.key}" + + @property + def is_on(self) -> bool: + """Return if the switch is currently on.""" + return self.entity_description.getter(self.coordinator.data.status) + + def turn_on(self, **kwargs: Any) -> None: + """Turn the switch on.""" + self.entity_description.setter(self.coordinator.client, True) + + def turn_off(self, **kwargs: Any) -> None: + """Turn the switch off.""" + self.entity_description.setter(self.coordinator.client, False)