From 73be04f9a8e1455a59edc739c1876f92fc14b7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Seux?= Date: Sun, 8 Sep 2024 11:17:23 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Store=20heatpump=20parameters=20loc?= =?UTF-8?q?ally?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this patch, HA used to forgot temperature mode, sensor mode and other rarely changing parameters. It required a complicated dance at the start-up of the integration until we received the first MQTT messages giving us those values (especially to get the temperature boundaries). Now we simply store them locally and assume they haven't changed while HA was down. It should really help with #240 --- custom_components/aquarea/climate.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) 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):