diff --git a/custom_components/aquarea/climate.py b/custom_components/aquarea/climate.py index 893f29c..1b9504a 100644 --- a/custom_components/aquarea/climate.py +++ b/custom_components/aquarea/climate.py @@ -12,6 +12,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util import slugify +from homeassistant.helpers.storage import Store from homeassistant.components.climate import ClimateEntityDescription from .definitions import OperatingMode @@ -159,7 +160,8 @@ def __init__( self._climate_mode = ZoneClimateMode.DIRECT self._mode = ZoneTemperatureMode.DIRECT self._mode_guessed = True - self.change_mode(ZoneTemperatureMode.DIRECT, initialization=True) + + self._store = Store(hass, version=1, key=self._attr_unique_id) # we only display heater by default self._attr_entity_registry_enabled_default = self.heater @@ -233,6 +235,14 @@ def change_mode(self, mode: ZoneTemperatureMode, initialization: bool = False): # Otherwise it triggers https://github.com/kamaradclimber/heishamon-homeassistant/issues/47 self.async_write_ha_state() self._mode_guessed = False + self._store.async_delay_save(self.build_data, delay=0) + + def build_data(self): + return { + "zone_sensor_mode": int(self._sensor_mode.value), + "zone_climate_mode": int(self._climate_mode.value), + "zone_temperature_mode": int(self._mode.value), + } async def async_set_temperature(self, **kwargs) -> None: temperature = kwargs.get("temperature") @@ -271,9 +281,18 @@ async def async_set_temperature(self, **kwargs) -> None: async def async_added_to_hass(self) -> None: """Subscribe to MQTT events.""" + await super().async_added_to_hass() + + stored_values = await self._store.async_load() + if stored_values: + self._sensor_mode = ZoneSensorMode(stored_values["zone_sensor_mode"]) + self._climate_mode = ZoneClimateMode(stored_values["zone_climate_mode"]) + self._mode = ZoneTemperatureMode(stored_values["zone_temperature_mode"]) + self.change_mode(self._mode) + else: + self.change_mode(ZoneTemperatureMode.DIRECT, initialization=True) + - async def async_added_to_hass(self) -> None: - """Subscribe to MQTT events.""" # per zone handle of sensory type to drive mode of operation @callback def sensor_mode_received(message):