77
88from huum .const import SaunaStatus
99from huum .exceptions import SafetyException
10- from huum .huum import Huum
11- from huum .schemas import HuumStatusResponse
1210
1311from homeassistant .components .climate import (
1412 ClimateEntity ,
1513 ClimateEntityFeature ,
1614 HVACMode ,
1715)
18- from homeassistant .config_entries import ConfigEntry
1916from homeassistant .const import ATTR_TEMPERATURE , PRECISION_WHOLE , UnitOfTemperature
2017from homeassistant .core import HomeAssistant
2118from homeassistant .exceptions import HomeAssistantError
2219from homeassistant .helpers .device_registry import DeviceInfo
2320from homeassistant .helpers .entity_platform import AddConfigEntryEntitiesCallback
21+ from homeassistant .helpers .update_coordinator import CoordinatorEntity
2422
2523from .const import DOMAIN
24+ from .coordinator import HuumConfigEntry , HuumDataUpdateCoordinator
2625
2726_LOGGER = logging .getLogger (__name__ )
2827
2928
3029async def async_setup_entry (
3130 hass : HomeAssistant ,
32- entry : ConfigEntry ,
31+ entry : HuumConfigEntry ,
3332 async_add_entities : AddConfigEntryEntitiesCallback ,
3433) -> None :
3534 """Set up the Huum sauna with config flow."""
36- huum_handler = hass . data . setdefault ( DOMAIN , {})[ entry .entry_id ]
35+ async_add_entities ([ HuumDevice ( entry .runtime_data )])
3736
38- async_add_entities ([HuumDevice (huum_handler , entry .entry_id )], True )
3937
40-
41- class HuumDevice (ClimateEntity ):
38+ class HuumDevice (CoordinatorEntity [HuumDataUpdateCoordinator ], ClimateEntity ):
4239 """Representation of a heater."""
4340
4441 _attr_hvac_modes = [HVACMode .HEAT , HVACMode .OFF ]
@@ -54,24 +51,22 @@ class HuumDevice(ClimateEntity):
5451 _attr_has_entity_name = True
5552 _attr_name = None
5653
57- _target_temperature : int | None = None
58- _status : HuumStatusResponse | None = None
59-
60- def __init__ (self , huum_handler : Huum , unique_id : str ) -> None :
54+ def __init__ (self , coordinator : HuumDataUpdateCoordinator ) -> None :
6155 """Initialize the heater."""
62- self ._attr_unique_id = unique_id
56+ super ().__init__ (coordinator )
57+
58+ self ._attr_unique_id = coordinator .config_entry .entry_id
6359 self ._attr_device_info = DeviceInfo (
64- identifiers = {(DOMAIN , unique_id )},
60+ identifiers = {(DOMAIN , coordinator . config_entry . entry_id )},
6561 name = "Huum sauna" ,
6662 manufacturer = "Huum" ,
63+ model = "UKU WiFi" ,
6764 )
6865
69- self ._huum_handler = huum_handler
70-
7166 @property
7267 def hvac_mode (self ) -> HVACMode :
7368 """Return hvac operation ie. heat, cool mode."""
74- if self ._status and self . _status .status == SaunaStatus .ONLINE_HEATING :
69+ if self .coordinator . data .status == SaunaStatus .ONLINE_HEATING :
7570 return HVACMode .HEAT
7671 return HVACMode .OFF
7772
@@ -85,41 +80,33 @@ def icon(self) -> str:
8580 @property
8681 def current_temperature (self ) -> int | None :
8782 """Return the current temperature."""
88- if (status := self ._status ) is not None :
89- return status .temperature
90- return None
83+ return self .coordinator .data .temperature
9184
9285 @property
9386 def target_temperature (self ) -> int :
9487 """Return the temperature we try to reach."""
95- return self ._target_temperature or int (self .min_temp )
88+ return self .coordinator . data . target_temperature or int (self .min_temp )
9689
9790 async def async_set_hvac_mode (self , hvac_mode : HVACMode ) -> None :
9891 """Set hvac mode."""
9992 if hvac_mode == HVACMode .HEAT :
10093 await self ._turn_on (self .target_temperature )
10194 elif hvac_mode == HVACMode .OFF :
102- await self ._huum_handler .turn_off ()
95+ await self .coordinator .huum .turn_off ()
96+ await self .coordinator .async_refresh ()
10397
10498 async def async_set_temperature (self , ** kwargs : Any ) -> None :
10599 """Set new target temperature."""
106100 temperature = kwargs .get (ATTR_TEMPERATURE )
107- if temperature is None :
101+ if temperature is None or self . hvac_mode != HVACMode . HEAT :
108102 return
109- self ._target_temperature = temperature
110-
111- if self .hvac_mode == HVACMode .HEAT :
112- await self ._turn_on (temperature )
113103
114- async def async_update (self ) -> None :
115- """Get the latest status data."""
116- self ._status = await self ._huum_handler .status ()
117- if self ._target_temperature is None or self .hvac_mode == HVACMode .HEAT :
118- self ._target_temperature = self ._status .target_temperature
104+ await self ._turn_on (temperature )
105+ await self .coordinator .async_refresh ()
119106
120107 async def _turn_on (self , temperature : int ) -> None :
121108 try :
122- await self ._huum_handler .turn_on (temperature )
109+ await self .coordinator . huum .turn_on (temperature )
123110 except (ValueError , SafetyException ) as err :
124111 _LOGGER .error (str (err ))
125112 raise HomeAssistantError (f"Unable to turn on sauna: { err } " ) from err
0 commit comments