Skip to content

Commit

Permalink
Use 'heat' flags instead of 'temp_pre' for gettig hvac action (#9)
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
tonyroberts authored Nov 14, 2023
1 parent 91fff6c commit a7b0e8a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
60 changes: 42 additions & 18 deletions custom_components/wundasmart/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,29 +210,53 @@ def __set_preset_mode(self):
def __set_hvac_state(self):
"""Set the hvac action and hvac mode from the coordinator data."""
state = self.__state

temp_pre = 0
if state.get("temp_pre") is not None:
try:
# tp appears to be the following flags:
# - 00000001 (0x01) indicates a manual override is set until the next manual override
# - 00000100 (0x04) indicates the set point temperature has been set to 'off'
# - 00010000 (0x10) indicates a manual override has been set
# - 00100000 (0x20) indicates heating demand
# - 10000000 (0x80) indicates the adaptive start mode is active
flags = int(state["temp_pre"])
self._attr_hvac_mode = (
HVACMode.OFF if flags & (0x10 | 0x4) == (0x10 | 0x4) # manually set to off
else HVACMode.HEAT if (flags & (0x10 | 0x80)) == 0x10 # manually set to heat
else HVACMode.AUTO
)
self._attr_hvac_action = (
HVACAction.PREHEATING if ((flags & (0x80 | 0x20)) == (0x80 | 0x20))
else HVACAction.HEATING if flags & 0x20
else HVACAction.OFF if flags & 0x4
else HVACAction.IDLE
)
# temp_pre appears to be the following flags:
# - 0000 0001 (0x01) indicates a manual override is set until the next manual override
# - 0000 0100 (0x04) indicates the set point temperature has been set to 'off'
# - 0001 0000 (0x10) indicates a manual override has been set
# - 0010 0000 (0x20) indicates heating demand
# - 1000 0000 (0x80) indicates the adaptive start mode is active
temp_pre = int(state["temp_pre"])
except (ValueError, TypeError):
_LOGGER.warning(f"Unexpected 'temp_pre' value '{state['temp_pre']}' for {self._attr_name}")

heat = 0
if state.get("heat") is not None:
try:
# heat appears to be the following flags:
# - 0000 0001 (0x01) indicates heat is being delivered
# - 0000 0010 (0x02) indicates heating demand
# - 0000 0100 (0x04) not sure what this means, always seem to be set
#
# eg. when off, heat is 4
# demand but no heat delivered (pump delay?), heat is 6
# demand and providing heat, heat is 7
# no demand but heat still on (pump delay?), heat is 5
heat = int(state["heat"])
except (ValueError, TypeError):
_LOGGER.warning(f"Unexpected 'heat' value '{state['heat']}' for {self._attr_name}")

self._attr_hvac_mode = (
HVACMode.OFF if temp_pre & (0x10 | 0x4) == (0x10 | 0x4) # manually set to off
else HVACMode.HEAT if (temp_pre & (0x10 | 0x80)) == 0x10 # manually set to heat
else HVACMode.AUTO
)

adaptive_start = temp_pre & 0x80
heating = heat & 0x1
demand = heat & 0x2

self._attr_hvac_action = (
HVACAction.PREHEATING if adaptive_start and heating
else HVACAction.HEATING if heating and demand
else HVACAction.IDLE if heating or demand
else HVACAction.OFF
)

def __update_state(self):
self.__set_current_temperature()
self.__set_current_humidity()
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/test_get_devices1.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"t_lo": "14.00",
"t_norm": "19.00",
"t_hi": "21.00",
"heat": "4",
"heat": "5",
"temp_pre": "4",
"prg": "1",
"lock": "0",
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/test_get_devices2.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"t_lo": "14.00",
"t_norm": "19.00",
"t_hi": "21.00",
"heat": "4",
"heat": "7",
"temp_pre": "176",
"prg": "1",
"lock": "0",
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/test_set_temperature.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"t_lo": "14.00",
"t_norm": "19.00",
"t_hi": "21.00",
"heat": "4",
"heat": "6",
"temp_pre": "48",
"prg": "1",
"lock": "0",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def test_climate(hass: HomeAssistant, config):
assert state.attributes["current_humidity"] == 66.57
assert state.attributes["temperature"] == 0
assert state.state == "auto"
assert state.attributes["hvac_action"] == HVACAction.OFF
assert state.attributes["hvac_action"] == HVACAction.IDLE

coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
assert coordinator
Expand Down Expand Up @@ -83,7 +83,7 @@ async def test_set_temperature(hass: HomeAssistant, config):
assert state.attributes["current_temperature"] == 16.0
assert state.attributes["temperature"] == 20
assert state.state == "heat"
assert state.attributes["hvac_action"] == HVACAction.HEATING
assert state.attributes["hvac_action"] == HVACAction.IDLE


async def test_trvs_only(hass: HomeAssistant, config):
Expand Down

0 comments on commit a7b0e8a

Please sign in to comment.