Skip to content

Commit

Permalink
Fix preset mode support and add tests
Browse files Browse the repository at this point in the history
- use standard HA presets where they line up
- add translations for non-standard 'reduced' preset
- fix a few bugs/typos to get presets working correctly
- add unit test for setting preset mode
  • Loading branch information
tonyroberts committed Nov 14, 2023
1 parent 846f00c commit 95e40dd
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 11 deletions.
29 changes: 18 additions & 11 deletions custom_components/wundasmart/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
ClimateEntityFeature,
HVACAction,
HVACMode,
PRESET_ECO,
PRESET_COMFORT
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
Expand Down Expand Up @@ -37,6 +39,14 @@
HVACMode.HEAT,
]

PRESET_REDUCED = "reduced"

SUPPORTED_PRESET_MODES = [
PRESET_REDUCED,
PRESET_ECO,
PRESET_COMFORT
]

PARALLEL_UPDATES = 1


Expand Down Expand Up @@ -71,7 +81,7 @@ class Device(CoordinatorEntity[WundasmartDataUpdateCoordinator], ClimateEntity):

_attr_hvac_modes = SUPPORTED_HVAC_MODES
_attr_temperature_unit = TEMP_CELSIUS
_attr_preset_modes = ["reduced", "eco", "comfort"]
_attr_preset_modes = SUPPORTED_PRESET_MODES

def __init__(
self,
Expand Down Expand Up @@ -101,6 +111,7 @@ def __init__(
self._attr_target_temperature = None
self._attr_current_humidity = None
self._attr_hvac_mode = HVACMode.AUTO
self._attr_preset_mode = None

# Update with initial state
self.__update_state()
Expand Down Expand Up @@ -259,21 +270,17 @@ async def async_set_hvac_mode(self, hvac_mode: HVACMode):
await self.coordinator.async_request_refresh()

async def async_set_preset_mode(self, preset_mode) -> None:
device = self.coordinator.data.get(self._wunda_id, {})
state = device.get("state", {})
sensor_state = device.get("sensor_state", {})

if preset_mode == "reduced":
if preset_mode == PRESET_REDUCED:
# Set the preset to your t_lo temperate
t_preset = float(state.get("t_lo")) or float(sensor.state("t-lo"))
t_preset = float(self.__state["t_lo"])

elif preset_mode == "eco":
elif preset_mode == PRESET_ECO:
# Set the preset to your t_norm temperature
t_preset = float(state.get("t_norm")) or float(sensor.state("t_norm"))
t_preset = float(self.__state["t_norm"])

elif preset_mode == "comfort":
elif preset_mode == PRESET_COMFORT:
# Set the preset to your t_hi temperature
t_preset = float(state.get("t_hi")) or float(sensor.state("t_hi"))
t_preset = float(self.__state["t_hi"])
else:
raise NotImplementedError(f"Unsupported Preset mode {preset_mode}")

Expand Down
13 changes: 13 additions & 0 deletions custom_components/wundasmart/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,18 @@
"title": "Wundasmart configuration"
}
}
},
"entity": {
"climate": {
"balboa": {
"state_attributes": {
"preset_mode": {
"state": {
"reduced": "Reduced"
}
}
}
}
}
}
}
55 changes: 55 additions & 0 deletions tests/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,58 @@ async def test_hvac_mode_when_manually_turned_off(hass: HomeAssistant, config):
assert state
assert state.state == HVACAction.OFF
assert state.attributes["hvac_action"] == HVACAction.OFF


async def test_set_presets(hass: HomeAssistant, config):
entry = MockConfigEntry(domain=DOMAIN, data=config)
entry.add_to_hass(hass)

data = deserialize_get_devices_fixture(load_fixture("test_set_presets.json"))
with patch("custom_components.wundasmart.get_devices", return_value=data), \
patch("custom_components.wundasmart.climate.send_command", return_value=None) as mock:
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get("climate.test_room")

assert state
assert state.attributes["temperature"] == 0

# set the preset 'reduced'
await hass.services.async_call("climate", "set_preset_mode", {
"entity_id": "climate.test_room",
"preset_mode": "reduced"
})
await hass.async_block_till_done()

# Check send_command was called correctly
assert mock.call_count == 1
assert mock.call_args.kwargs["params"]
assert mock.call_args.kwargs["params"]["roomid"] == 121
assert mock.call_args.kwargs["params"]["temp"] == 14.0

# set the preset 'eco'
await hass.services.async_call("climate", "set_preset_mode", {
"entity_id": "climate.test_room",
"preset_mode": "eco"
})
await hass.async_block_till_done()

# Check send_command was called correctly
assert mock.call_count == 2
assert mock.call_args.kwargs["params"]
assert mock.call_args.kwargs["params"]["roomid"] == 121
assert mock.call_args.kwargs["params"]["temp"] == 19.0

# set the preset 'comfort'
await hass.services.async_call("climate", "set_preset_mode", {
"entity_id": "climate.test_room",
"preset_mode": "comfort"
})
await hass.async_block_till_done()

# Check send_command was called correctly
assert mock.call_count == 3
assert mock.call_args.kwargs["params"]
assert mock.call_args.kwargs["params"]["roomid"] == 121
assert mock.call_args.kwargs["params"]["temp"] == 21.0

0 comments on commit 95e40dd

Please sign in to comment.