|
| 1 | +"""Climate platform for Saunum Leil Sauna Control Unit.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import logging |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from pysaunum import MAX_TEMPERATURE, MIN_TEMPERATURE, SaunumException |
| 10 | + |
| 11 | +from homeassistant.components.climate import ( |
| 12 | + ClimateEntity, |
| 13 | + ClimateEntityFeature, |
| 14 | + HVACAction, |
| 15 | + HVACMode, |
| 16 | +) |
| 17 | +from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature |
| 18 | +from homeassistant.core import HomeAssistant |
| 19 | +from homeassistant.exceptions import HomeAssistantError |
| 20 | +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 21 | + |
| 22 | +from . import LeilSaunaConfigEntry |
| 23 | +from .const import DELAYED_REFRESH_SECONDS |
| 24 | +from .entity import LeilSaunaEntity |
| 25 | + |
| 26 | +_LOGGER = logging.getLogger(__name__) |
| 27 | + |
| 28 | +PARALLEL_UPDATES = 1 |
| 29 | + |
| 30 | + |
| 31 | +async def async_setup_entry( |
| 32 | + hass: HomeAssistant, |
| 33 | + entry: LeilSaunaConfigEntry, |
| 34 | + async_add_entities: AddConfigEntryEntitiesCallback, |
| 35 | +) -> None: |
| 36 | + """Set up Saunum Leil Sauna climate entity.""" |
| 37 | + coordinator = entry.runtime_data |
| 38 | + async_add_entities([LeilSaunaClimate(coordinator)]) |
| 39 | + |
| 40 | + |
| 41 | +class LeilSaunaClimate(LeilSaunaEntity, ClimateEntity): |
| 42 | + """Representation of a Saunum Leil Sauna climate entity.""" |
| 43 | + |
| 44 | + _attr_name = None |
| 45 | + _attr_hvac_modes = [HVACMode.OFF, HVACMode.HEAT] |
| 46 | + _attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE |
| 47 | + _attr_temperature_unit = UnitOfTemperature.CELSIUS |
| 48 | + _attr_min_temp = MIN_TEMPERATURE |
| 49 | + _attr_max_temp = MAX_TEMPERATURE |
| 50 | + |
| 51 | + @property |
| 52 | + def current_temperature(self) -> float | None: |
| 53 | + """Return the current temperature in Celsius.""" |
| 54 | + return self.coordinator.data.current_temperature |
| 55 | + |
| 56 | + @property |
| 57 | + def target_temperature(self) -> float | None: |
| 58 | + """Return the target temperature in Celsius.""" |
| 59 | + return self.coordinator.data.target_temperature |
| 60 | + |
| 61 | + @property |
| 62 | + def hvac_mode(self) -> HVACMode: |
| 63 | + """Return current HVAC mode.""" |
| 64 | + session_active = self.coordinator.data.session_active |
| 65 | + return HVACMode.HEAT if session_active else HVACMode.OFF |
| 66 | + |
| 67 | + @property |
| 68 | + def hvac_action(self) -> HVACAction | None: |
| 69 | + """Return current HVAC action.""" |
| 70 | + if not self.coordinator.data.session_active: |
| 71 | + return HVACAction.OFF |
| 72 | + |
| 73 | + heater_elements_active = self.coordinator.data.heater_elements_active |
| 74 | + return ( |
| 75 | + HVACAction.HEATING |
| 76 | + if heater_elements_active and heater_elements_active > 0 |
| 77 | + else HVACAction.IDLE |
| 78 | + ) |
| 79 | + |
| 80 | + async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None: |
| 81 | + """Set new HVAC mode.""" |
| 82 | + try: |
| 83 | + if hvac_mode == HVACMode.HEAT: |
| 84 | + await self.coordinator.client.async_start_session() |
| 85 | + else: |
| 86 | + await self.coordinator.client.async_stop_session() |
| 87 | + except SaunumException as err: |
| 88 | + raise HomeAssistantError(f"Failed to set HVAC mode to {hvac_mode}") from err |
| 89 | + |
| 90 | + # The device takes 1-2 seconds to turn heater elements on/off and |
| 91 | + # update heater_elements_active. Wait and refresh again to ensure |
| 92 | + # the HVAC action state reflects the actual heater status. |
| 93 | + await asyncio.sleep(DELAYED_REFRESH_SECONDS.total_seconds()) |
| 94 | + await self.coordinator.async_request_refresh() |
| 95 | + |
| 96 | + async def async_set_temperature(self, **kwargs: Any) -> None: |
| 97 | + """Set new target temperature.""" |
| 98 | + try: |
| 99 | + await self.coordinator.client.async_set_target_temperature( |
| 100 | + int(kwargs[ATTR_TEMPERATURE]) |
| 101 | + ) |
| 102 | + except SaunumException as err: |
| 103 | + raise HomeAssistantError( |
| 104 | + f"Failed to set temperature to {kwargs[ATTR_TEMPERATURE]}" |
| 105 | + ) from err |
| 106 | + |
| 107 | + await self.coordinator.async_request_refresh() |
0 commit comments