From f3add292d50a4309be4cb698b6694e92edcca4a2 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Sat, 19 Feb 2022 20:57:29 -0800 Subject: [PATCH] Update nest climate set_temperature to allow hvac_mode (#66909) --- homeassistant/components/nest/climate_sdm.py | 11 +++- tests/components/nest/test_climate_sdm.py | 59 ++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/nest/climate_sdm.py b/homeassistant/components/nest/climate_sdm.py index 77b3b24331b..ff8cffcf7fa 100644 --- a/homeassistant/components/nest/climate_sdm.py +++ b/homeassistant/components/nest/climate_sdm.py @@ -16,6 +16,7 @@ from google_nest_sdm.thermostat_traits import ( from homeassistant.components.climate import ClimateEntity from homeassistant.components.climate.const import ( + ATTR_HVAC_MODE, ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_LOW, CURRENT_HVAC_COOL, @@ -311,18 +312,22 @@ class ThermostatEntity(ClimateEntity): async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperature.""" + hvac_mode = self.hvac_mode + if kwargs.get(ATTR_HVAC_MODE) is not None: + hvac_mode = kwargs[ATTR_HVAC_MODE] + await self.async_set_hvac_mode(hvac_mode) low_temp = kwargs.get(ATTR_TARGET_TEMP_LOW) high_temp = kwargs.get(ATTR_TARGET_TEMP_HIGH) temp = kwargs.get(ATTR_TEMPERATURE) if ThermostatTemperatureSetpointTrait.NAME not in self._device.traits: return trait = self._device.traits[ThermostatTemperatureSetpointTrait.NAME] - if self.preset_mode == PRESET_ECO or self.hvac_mode == HVAC_MODE_HEAT_COOL: + if self.preset_mode == PRESET_ECO or hvac_mode == HVAC_MODE_HEAT_COOL: if low_temp and high_temp: await trait.set_range(low_temp, high_temp) - elif self.hvac_mode == HVAC_MODE_COOL and temp: + elif hvac_mode == HVAC_MODE_COOL and temp: await trait.set_cool(temp) - elif self.hvac_mode == HVAC_MODE_HEAT and temp: + elif hvac_mode == HVAC_MODE_HEAT and temp: await trait.set_heat(temp) async def async_set_preset_mode(self, preset_mode: str) -> None: diff --git a/tests/components/nest/test_climate_sdm.py b/tests/components/nest/test_climate_sdm.py index 6b100969ea9..5f3efa362b3 100644 --- a/tests/components/nest/test_climate_sdm.py +++ b/tests/components/nest/test_climate_sdm.py @@ -677,6 +677,65 @@ async def test_thermostat_set_heat( } +async def test_thermostat_set_temperature_hvac_mode( + hass: HomeAssistant, + setup_platform: PlatformSetup, + auth: FakeAuth, + create_device: CreateDevice, +) -> None: + """Test setting HVAC mode while setting temperature.""" + create_device.create( + { + "sdm.devices.traits.ThermostatHvac": {"status": "OFF"}, + "sdm.devices.traits.ThermostatMode": { + "availableModes": ["HEAT", "COOL", "HEATCOOL", "OFF"], + "mode": "OFF", + }, + "sdm.devices.traits.ThermostatTemperatureSetpoint": { + "coolCelsius": 25.0, + }, + }, + ) + await setup_platform() + + assert len(hass.states.async_all()) == 1 + thermostat = hass.states.get("climate.my_thermostat") + assert thermostat is not None + assert thermostat.state == HVAC_MODE_OFF + + await common.async_set_temperature(hass, temperature=24.0, hvac_mode=HVAC_MODE_COOL) + await hass.async_block_till_done() + + assert auth.method == "post" + assert auth.url == DEVICE_COMMAND + assert auth.json == { + "command": "sdm.devices.commands.ThermostatTemperatureSetpoint.SetCool", + "params": {"coolCelsius": 24.0}, + } + + await common.async_set_temperature(hass, temperature=26.0, hvac_mode=HVAC_MODE_HEAT) + await hass.async_block_till_done() + + assert auth.method == "post" + assert auth.url == DEVICE_COMMAND + assert auth.json == { + "command": "sdm.devices.commands.ThermostatTemperatureSetpoint.SetHeat", + "params": {"heatCelsius": 26.0}, + } + + await common.async_set_temperature( + hass, target_temp_low=20.0, target_temp_high=24.0, hvac_mode=HVAC_MODE_HEAT_COOL + ) + await hass.async_block_till_done() + + assert auth.method == "post" + assert auth.url == DEVICE_COMMAND + assert auth.json == { + "command": "sdm.devices.commands.ThermostatTemperatureSetpoint.SetRange", + "params": {"heatCelsius": 20.0, "coolCelsius": 24.0}, + } + + async def test_thermostat_set_heat_cool( hass: HomeAssistant, setup_platform: PlatformSetup,