diff --git a/homeassistant/components/zwave_js/climate.py b/homeassistant/components/zwave_js/climate.py index 4ef13276fbe140..43363538500528 100644 --- a/homeassistant/components/zwave_js/climate.py +++ b/homeassistant/components/zwave_js/climate.py @@ -469,15 +469,15 @@ async def async_set_temperature(self, **kwargs: Any) -> None: async def async_set_hvac_mode(self, hvac_mode: str) -> None: """Set new target hvac mode.""" - if not self._current_mode: - # Thermostat(valve) with no support for setting a mode - raise ValueError( - f"Thermostat {self.entity_id} does not support setting a mode" - ) - hvac_mode_value = self._hvac_modes.get(hvac_mode) - if hvac_mode_value is None: + hvac_mode_id = self._hvac_modes.get(hvac_mode) + if hvac_mode_id is None: raise ValueError(f"Received an invalid hvac mode: {hvac_mode}") - await self.info.node.async_set_value(self._current_mode, hvac_mode_value) + + if not self._current_mode: + # Thermostat(valve) has no support for setting a mode, so we make it a no-op + return + + await self.info.node.async_set_value(self._current_mode, hvac_mode_id) async def async_set_preset_mode(self, preset_mode: str) -> None: """Set new target preset mode.""" diff --git a/tests/components/zwave_js/test_climate.py b/tests/components/zwave_js/test_climate.py index f86052b3692fca..fefa680ce77735 100644 --- a/tests/components/zwave_js/test_climate.py +++ b/tests/components/zwave_js/test_climate.py @@ -382,6 +382,30 @@ async def test_setpoint_thermostat(hass, client, climate_danfoss_lc_13, integrat blocking=True, ) + # Test setting illegal mode raises an error + with pytest.raises(ValueError): + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + { + ATTR_ENTITY_ID: CLIMATE_DANFOSS_LC13_ENTITY, + ATTR_HVAC_MODE: HVAC_MODE_COOL, + }, + blocking=True, + ) + + # Test that setting HVAC_MODE_HEAT works. If the no-op logic didn't work, this would + # raise an error + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + { + ATTR_ENTITY_ID: CLIMATE_DANFOSS_LC13_ENTITY, + ATTR_HVAC_MODE: HVAC_MODE_HEAT, + }, + blocking=True, + ) + assert len(client.async_send_command_no_wait.call_args_list) == 1 args = client.async_send_command_no_wait.call_args_list[0][0][0] assert args["command"] == "node.set_value"