add switch platform for tolo (#113440)

* upgrade tololib dependency to v1.0.0

* add switch to enable/disable aroma therapy

* aroma therapy and salt bath switch

* Update homeassistant/components/tolo/strings.json

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* removed key from specific property list, it's required by default

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Matthias Lohr 2024-03-14 19:10:29 +01:00 committed by GitHub
parent 2aadd643ed
commit 7359d66d32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 98 additions and 0 deletions

View File

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

View File

@ -29,6 +29,7 @@ PLATFORMS = [
Platform.NUMBER,
Platform.SELECT,
Platform.SENSOR,
Platform.SWITCH,
]
_LOGGER = logging.getLogger(__name__)

View File

@ -42,6 +42,11 @@
"fan_timer_remaining": {
"default": "mdi:fan-auto"
}
},
"switch": {
"aroma_therapy_on": {
"default": "mdi:scent"
}
}
}
}

View File

@ -79,6 +79,14 @@
"fan_timer_remaining": {
"name": "Fan timer"
}
},
"switch": {
"aroma_therapy_on": {
"name": "Aroma therapy"
},
"salt_bath_on": {
"name": "Salt bath"
}
}
}
}

View File

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