From b8253b1b477ec0414fa2ca47876b993a2a138d94 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 10 Feb 2022 18:10:21 +0100 Subject: [PATCH] Plugwise HVAC/Preset mode fixes (#66273) --- homeassistant/components/plugwise/climate.py | 28 +++++++++++--------- homeassistant/components/plugwise/const.py | 2 -- tests/components/plugwise/test_climate.py | 19 ++++++++++--- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/plugwise/climate.py b/homeassistant/components/plugwise/climate.py index dba041f1028..f8e1b4f981c 100644 --- a/homeassistant/components/plugwise/climate.py +++ b/homeassistant/components/plugwise/climate.py @@ -21,13 +21,11 @@ from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP, DOMAIN, SCHEDULE_OFF, SCHEDULE_ON +from .const import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP, DOMAIN from .coordinator import PlugwiseDataUpdateCoordinator from .entity import PlugwiseEntity from .util import plugwise_command -HVAC_MODES_HEAT_ONLY = [HVAC_MODE_HEAT, HVAC_MODE_AUTO, HVAC_MODE_OFF] -HVAC_MODES_HEAT_COOL = [HVAC_MODE_HEAT, HVAC_MODE_COOL, HVAC_MODE_AUTO, HVAC_MODE_OFF] THERMOSTAT_CLASSES = ["thermostat", "zone_thermostat", "thermostatic_radiator_valve"] @@ -50,10 +48,7 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity): _attr_max_temp = DEFAULT_MAX_TEMP _attr_min_temp = DEFAULT_MIN_TEMP - _attr_preset_modes = None - _attr_supported_features = SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE _attr_temperature_unit = TEMP_CELSIUS - _attr_hvac_modes = HVAC_MODES_HEAT_ONLY def __init__( self, @@ -67,12 +62,17 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity): self._attr_name = self.device.get("name") # Determine preset modes + self._attr_supported_features = SUPPORT_TARGET_TEMPERATURE if presets := self.device.get("presets"): + self._attr_supported_features |= SUPPORT_PRESET_MODE self._attr_preset_modes = list(presets) # Determine hvac modes and current hvac mode + self._attr_hvac_modes = [HVAC_MODE_HEAT, HVAC_MODE_OFF] if self.coordinator.data.gateway.get("cooling_present"): - self._attr_hvac_modes = HVAC_MODES_HEAT_COOL + self._attr_hvac_modes.append(HVAC_MODE_COOL) + if self.device.get("available_schedules") != ["None"]: + self._attr_hvac_modes.append(HVAC_MODE_AUTO) @property def current_temperature(self) -> float | None: @@ -130,16 +130,20 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity): @plugwise_command async def async_set_hvac_mode(self, hvac_mode: str) -> None: """Set the hvac mode.""" - state = SCHEDULE_OFF if hvac_mode == HVAC_MODE_AUTO: - state = SCHEDULE_ON + if ( + schedule_temperature := self.device.get("schedule_temperature") + ) is None: + raise ValueError("Cannot set HVAC mode to Auto: No schedule available") + await self.coordinator.api.set_temperature( - self.device["location"], self.device.get("schedule_temperature") + self.device["location"], schedule_temperature ) - self._attr_target_temperature = self.device.get("schedule_temperature") await self.coordinator.api.set_schedule_state( - self.device["location"], self.device.get("last_used"), state + self.device["location"], + self.device.get("last_used"), + "true" if hvac_mode == HVAC_MODE_AUTO else "false", ) @plugwise_command diff --git a/homeassistant/components/plugwise/const.py b/homeassistant/components/plugwise/const.py index 1beeff501b0..ab4bde47d91 100644 --- a/homeassistant/components/plugwise/const.py +++ b/homeassistant/components/plugwise/const.py @@ -14,8 +14,6 @@ FLOW_STRETCH = "stretch (Stretch)" FLOW_TYPE = "flow_type" GATEWAY = "gateway" PW_TYPE = "plugwise_type" -SCHEDULE_OFF = "false" -SCHEDULE_ON = "true" SMILE = "smile" STRETCH = "stretch" STRETCH_USERNAME = "stretch" diff --git a/tests/components/plugwise/test_climate.py b/tests/components/plugwise/test_climate.py index 5eb60ebeb6f..5c9df093487 100644 --- a/tests/components/plugwise/test_climate.py +++ b/tests/components/plugwise/test_climate.py @@ -5,6 +5,7 @@ import pytest from homeassistant.components.climate.const import ( HVAC_MODE_AUTO, + HVAC_MODE_COOL, HVAC_MODE_HEAT, HVAC_MODE_OFF, ) @@ -22,7 +23,7 @@ async def test_adam_climate_entity_attributes(hass, mock_smile_adam): state = hass.states.get("climate.zone_lisa_wk") attrs = state.attributes - assert attrs["hvac_modes"] == [HVAC_MODE_HEAT, HVAC_MODE_AUTO, HVAC_MODE_OFF] + assert attrs["hvac_modes"] == [HVAC_MODE_HEAT, HVAC_MODE_OFF, HVAC_MODE_AUTO] assert "preset_modes" in attrs assert "no_frost" in attrs["preset_modes"] @@ -38,7 +39,7 @@ async def test_adam_climate_entity_attributes(hass, mock_smile_adam): state = hass.states.get("climate.zone_thermostat_jessie") attrs = state.attributes - assert attrs["hvac_modes"] == [HVAC_MODE_HEAT, HVAC_MODE_AUTO, HVAC_MODE_OFF] + assert attrs["hvac_modes"] == [HVAC_MODE_HEAT, HVAC_MODE_OFF, HVAC_MODE_AUTO] assert "preset_modes" in attrs assert "no_frost" in attrs["preset_modes"] @@ -157,7 +158,7 @@ async def test_anna_climate_entity_attributes(hass, mock_smile_anna): attrs = state.attributes assert "hvac_modes" in attrs - assert "heat" in attrs["hvac_modes"] + assert attrs["hvac_modes"] == [HVAC_MODE_HEAT, HVAC_MODE_OFF, HVAC_MODE_COOL] assert "preset_modes" in attrs assert "no_frost" in attrs["preset_modes"] @@ -214,3 +215,15 @@ async def test_anna_climate_entity_climate_changes(hass, mock_smile_anna): mock_smile_anna.set_schedule_state.assert_called_with( "c784ee9fdab44e1395b8dee7d7a497d5", None, "false" ) + + # Auto mode is not available, no schedules + with pytest.raises(ValueError): + await hass.services.async_call( + "climate", + "set_hvac_mode", + {"entity_id": "climate.anna", "hvac_mode": "auto"}, + blocking=True, + ) + + assert mock_smile_anna.set_temperature.call_count == 1 + assert mock_smile_anna.set_schedule_state.call_count == 1