Skip to content

Commit

Permalink
✨ Store heatpump parameters locally
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kamaradclimber committed Sep 8, 2024
1 parent 3e41373 commit 73be04f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions custom_components/aquarea/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 73be04f

Please sign in to comment.