Skip to content

Commit

Permalink
πŸ› Write HA state after initialization only
Browse files Browse the repository at this point in the history
If we write entity to HA state before having registered entities, it
creates a shallow entity without unique_id.
The only reason we write to HA state is to make sure HA took into
account our changes to various attributes which is not necessary at
initialization time.

Change-Id: Ifce6c1d1aee3b0d6ec927950d78434191777554c
Reference: #47
  • Loading branch information
kamaradclimber committed Nov 14, 2022
1 parent f815a51 commit b5d0f7f
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions custom_components/aquarea/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,9 @@ def __init__(
self._operating_mode = OperatingMode(0) # i.e None

self._mode = ZoneClimateMode.DIRECT
# this line should be last since it leads to write state so object must be completely ready
self.change_mode(ZoneClimateMode.DIRECT)
self.change_mode(ZoneClimateMode.DIRECT, initialization=True)

def change_mode(self, mode: ZoneClimateMode):
def change_mode(self, mode: ZoneClimateMode, initialization:bool=False):
_LOGGER.warn(f"Changing mode to {mode} for zone {self.zone_id}")
self._mode = mode
if mode == ZoneClimateMode.COMPENSATION:
Expand All @@ -311,7 +310,10 @@ def change_mode(self, mode: ZoneClimateMode):
self._attr_min_temp = 15
self._attr_max_temp = 25
self._attr_target_temperature_step = 1
self.async_write_ha_state()
if not initialization:
# during initialization we cannot write HA state because entities are not registered yet.
# Otherwise it triggers https://github.com/kamaradclimber/heishamon-homeassistant/issues/47
self.async_write_ha_state()

async def async_set_temperature(self, **kwargs) -> None:
temperature = kwargs.get("temperature")
Expand Down

0 comments on commit b5d0f7f

Please sign in to comment.