Skip to content

Commit

Permalink
Issue #120 - presence sensor not updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Collin committed Oct 15, 2023
1 parent 66297c6 commit 81900ce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
32 changes: 19 additions & 13 deletions custom_components/versatile_thermostat/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from homeassistant.core import HomeAssistant, callback, Event

from homeassistant.const import STATE_ON
from homeassistant.const import STATE_ON, STATE_OFF

from homeassistant.components.binary_sensor import (
BinarySensorEntity,
Expand Down Expand Up @@ -133,12 +133,14 @@ async def async_my_climate_changed(self, event: Event = None):
_LOGGER.debug("%s - climate state change", self._attr_unique_id)

old_state = self._attr_is_on
self._attr_is_on = (
self.my_climate.window_state == STATE_ON
or self.my_climate.window_auto_state == STATE_ON
)
if old_state != self._attr_is_on:
self.async_write_ha_state()
# Issue 120 - only take defined presence value
if self.my_climate.window_state in [STATE_ON, STATE_OFF] or self.my_climate.window_auto_state in [STATE_ON, STATE_OFF]:
self._attr_is_on = (
self.my_climate.window_state == STATE_ON
or self.my_climate.window_auto_state == STATE_ON
)
if old_state != self._attr_is_on:
self.async_write_ha_state()
return

@property
Expand Down Expand Up @@ -171,9 +173,11 @@ async def async_my_climate_changed(self, event: Event = None):
"""Called when my climate have change"""
_LOGGER.debug("%s - climate state change", self._attr_unique_id)
old_state = self._attr_is_on
self._attr_is_on = self.my_climate.motion_state == STATE_ON
if old_state != self._attr_is_on:
self.async_write_ha_state()
# Issue 120 - only take defined presence value
if self.my_climate.motion_state in [STATE_ON, STATE_OFF]:
self._attr_is_on = self.my_climate.motion_state == STATE_ON
if old_state != self._attr_is_on:
self.async_write_ha_state()
return

@property
Expand Down Expand Up @@ -204,9 +208,11 @@ async def async_my_climate_changed(self, event: Event = None):

_LOGGER.debug("%s - climate state change", self._attr_unique_id)
old_state = self._attr_is_on
self._attr_is_on = self.my_climate.presence_state == STATE_ON
if old_state != self._attr_is_on:
self.async_write_ha_state()
# Issue 120 - only take defined presence value
if self.my_climate.presence_state in [STATE_ON, STATE_OFF]:
self._attr_is_on = self.my_climate.presence_state == STATE_ON
if old_state != self._attr_is_on:
self.async_write_ha_state()
return

@property
Expand Down
11 changes: 6 additions & 5 deletions custom_components/versatile_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2299,29 +2299,29 @@ async def _async_control_heating(self, force=False, _=None):
)
try:
under.startup()
except UnknownEntity as err:
except UnknownEntity:
# still not found, we an stop here
return
return False

# Check overpowering condition
# Not necessary for switch because each switch is checking at startup
overpowering: bool = await self.check_overpowering()
if overpowering:
_LOGGER.debug("%s - End of cycle (overpowering)", self)
return
return True

security: bool = await self.check_security()
if security and self._is_over_climate:
_LOGGER.debug("%s - End of cycle (security and over climate)", self)
return
return True

# Stop here if we are off
if self._hvac_mode == HVACMode.OFF:
_LOGGER.debug("%s - End of cycle (HVAC_MODE_OFF)", self)
# A security to force stop heater if still active
if self._is_device_active:
await self._async_underlying_entity_turn_off()
return
return True

if not self._is_over_climate:
for under in self._underlyings:
Expand All @@ -2333,6 +2333,7 @@ async def _async_control_heating(self, force=False, _=None):
)

self.update_custom_attributes()
return True

def recalculate(self):
"""A utility function to force the calculation of a the algo and
Expand Down
6 changes: 2 additions & 4 deletions custom_components/versatile_thermostat/tests/test_bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ async def test_bug_56(

# try to call _async_control_heating
try:
await entity._async_control_heating()
ret = await entity._async_control_heating()
# an exception should be send
assert False
except UnknownEntity:
pass
assert ret is False
except Exception: # pylint: disable=broad-exception-caught
assert False

Expand Down

0 comments on commit 81900ce

Please sign in to comment.