Skip to content

Commit

Permalink
Issue #81 - recursive loop when security should be set
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Collin committed Apr 28, 2023
1 parent 3f498ff commit 64ce3aa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
24 changes: 13 additions & 11 deletions custom_components/versatile_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ async def async_turn_aux_heat_off(self) -> None:

raise NotImplementedError()

async def async_set_hvac_mode(self, hvac_mode):
async def async_set_hvac_mode(self, hvac_mode, need_control_heating=True):
"""Set new target hvac mode."""
_LOGGER.info("%s - Set hvac mode: %s", self, hvac_mode)

Expand All @@ -1201,13 +1201,13 @@ async def async_set_hvac_mode(self, hvac_mode):
self._hvac_mode = hvac_mode

# Delegate to all underlying
need_control_heating = False
sub_need_control_heating = False
for under in self._underlyings:
need_control_heating = (
sub_need_control_heating = (
await under.set_hvac_mode(hvac_mode) or need_control_heating
)

if need_control_heating:
if need_control_heating and sub_need_control_heating:
await self._async_control_heating(force=True)

# Ensure we update the current operation after changing the mode
Expand Down Expand Up @@ -1452,7 +1452,7 @@ async def try_window_condition(_):
self,
self._saved_hvac_mode,
)
await self.restore_hvac_mode()
await self.restore_hvac_mode(True)
elif self._window_state == STATE_ON:
_LOGGER.info(
"%s - Window is open. Set hvac_mode to '%s'", self, HVACMode.OFF
Expand Down Expand Up @@ -1857,7 +1857,7 @@ async def deactivate_window_auto(auto=False):
)
# Set attributes
self._window_auto_state = False
await self.restore_hvac_mode()
await self.restore_hvac_mode(True)

if self._window_call_cancel:
self._window_call_cancel()
Expand Down Expand Up @@ -1953,9 +1953,9 @@ def save_hvac_mode(self):
self._hvac_mode,
)

async def restore_hvac_mode(self):
async def restore_hvac_mode(self, need_control_heating=False):
"""Restore a previous hvac_mod"""
await self.async_set_hvac_mode(self._saved_hvac_mode)
await self.async_set_hvac_mode(self._saved_hvac_mode, need_control_heating)
_LOGGER.debug(
"%s - Restored hvac_mode - saved_hvac_mode is %s, hvac_mode is %s",
self,
Expand Down Expand Up @@ -2025,7 +2025,7 @@ async def check_overpowering(self) -> bool:
self._saved_preset_mode,
)
if self._is_over_climate:
await self.restore_hvac_mode()
await self.restore_hvac_mode(False)
await self.restore_preset_mode()
self.send_event(
EventType.POWER_EVENT,
Expand Down Expand Up @@ -2127,7 +2127,7 @@ async def check_security(self) -> bool:
await self._async_set_preset_mode_internal(PRESET_SECURITY)
# Turn off the underlying climate or heater if security default on_percent is 0
if self._is_over_climate or self._security_default_on_percent <= 0.0:
await self.async_set_hvac_mode(HVACMode.OFF)
await self.async_set_hvac_mode(HVACMode.OFF, False)
if self._prop_algorithm:
self._prop_algorithm.set_security(self._security_default_on_percent)

Expand Down Expand Up @@ -2161,7 +2161,7 @@ async def check_security(self) -> bool:
self._security_state = ret
# Restore hvac_mode if previously saved
if self._is_over_climate or self._security_default_on_percent <= 0.0:
await self.restore_hvac_mode()
await self.restore_hvac_mode(False)
await self.restore_preset_mode()
if self._prop_algorithm:
self._prop_algorithm.unset_security()
Expand Down Expand Up @@ -2294,6 +2294,8 @@ def update_custom_attributes(self):
self.get_preset_away_name(PRESET_COMFORT)
),
"power_temp": self._power_temp,
"target_temp": self.target_temperature,
"current_temp": self._cur_temp,
"ext_current_temperature": self._cur_ext_temp,
"current_power": self._current_power,
"current_power_max": self._current_power_max,
Expand Down
19 changes: 14 additions & 5 deletions custom_components/versatile_thermostat/underlyings.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,26 @@ def __init__(
self._should_relaunch_control_heating = False
self._on_time_sec = 0
self._off_time_sec = 0
self._hvac_mode = None

@property
def initial_delay_sec(self):
"""The initial delay for this class"""
return self._initial_delay_sec

async def set_hvac_mode(self, hvac_mode: HVACMode) -> bool:
"""Set the HVACmode. Returns true if we need to redo a control_heating"""
"""Set the HVACmode. Returns true if something have change"""

if hvac_mode == HVACMode.OFF:
if self.is_device_active:
await self.turn_off()
await self._cancel_cycle()
return True

if self._hvac_mode != hvac_mode:
self._hvac_mode = hvac_mode
return True
else:
return False

@property
def is_device_active(self):
Expand Down Expand Up @@ -421,10 +428,10 @@ def is_initialized(self) -> bool:
"""True if the underlying climate was found"""
return self._underlying_climate is not None

async def set_hvac_mode(self, hvac_mode: HVACMode):
"""Set the HVACmode of the underlying climate"""
async def set_hvac_mode(self, hvac_mode: HVACMode) -> bool:
"""Set the HVACmode of the underlying climate. Returns true if something have change"""
if not self.is_initialized:
return
return False

data = {ATTR_ENTITY_ID: self._entity_id, "hvac_mode": hvac_mode}
await self._hass.services.async_call(
Expand All @@ -433,6 +440,8 @@ async def set_hvac_mode(self, hvac_mode: HVACMode):
data,
)

return True

@property
def is_device_active(self):
"""If the toggleable device is currently active."""
Expand Down

0 comments on commit 64ce3aa

Please sign in to comment.