From 84b71c9ddb3bc5b7536205a3376fd0909a09cfcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Mandari=C4=87?= <2945713+MislavMandaric@users.noreply.github.com> Date: Mon, 30 Oct 2023 22:10:47 +0100 Subject: [PATCH] Allow setting hvac mode through set_temperature climate method in Gree integration (#101196) * Allow setting hvac mode through set_temperature climate method * Suggested code simplification when reading hvac mode Co-authored-by: G Johansson * Remove unnecessary temperature unit handling from set temperature with hvac mode tests --------- Co-authored-by: G Johansson --- homeassistant/components/gree/climate.py | 4 +++ tests/components/gree/test_climate.py | 44 +++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/gree/climate.py b/homeassistant/components/gree/climate.py index 17d915feadb..b14b9cfaba4 100644 --- a/homeassistant/components/gree/climate.py +++ b/homeassistant/components/gree/climate.py @@ -17,6 +17,7 @@ from greeclimate.device import ( ) from homeassistant.components.climate import ( + ATTR_HVAC_MODE, FAN_AUTO, FAN_HIGH, FAN_LOW, @@ -158,6 +159,9 @@ class GreeClimateEntity(CoordinatorEntity[DeviceDataUpdateCoordinator], ClimateE if ATTR_TEMPERATURE not in kwargs: raise ValueError(f"Missing parameter {ATTR_TEMPERATURE}") + if hvac_mode := kwargs.get(ATTR_HVAC_MODE): + await self.async_set_hvac_mode(hvac_mode) + temperature = kwargs[ATTR_TEMPERATURE] _LOGGER.debug( "Setting temperature to %d for %s", diff --git a/tests/components/gree/test_climate.py b/tests/components/gree/test_climate.py index fe64b0ee7ef..82ad75b5d28 100644 --- a/tests/components/gree/test_climate.py +++ b/tests/components/gree/test_climate.py @@ -34,7 +34,11 @@ from homeassistant.components.climate import ( SWING_VERTICAL, HVACMode, ) -from homeassistant.components.gree.climate import FAN_MODES_REVERSE, HVAC_MODES_REVERSE +from homeassistant.components.gree.climate import ( + FAN_MODES_REVERSE, + HVAC_MODES, + HVAC_MODES_REVERSE, +) from homeassistant.components.gree.const import FAN_MEDIUM_HIGH, FAN_MEDIUM_LOW from homeassistant.const import ( ATTR_ENTITY_ID, @@ -384,6 +388,9 @@ async def test_send_target_temperature( """Test for sending target temperature command to the device.""" hass.config.units.temperature_unit = units + device().power = True + device().mode = HVAC_MODES_REVERSE.get(HVACMode.AUTO) + fake_device = device() if units == UnitOfTemperature.FAHRENHEIT: fake_device.temperature_units = 1 @@ -407,12 +414,47 @@ async def test_send_target_temperature( state.attributes.get(ATTR_CURRENT_TEMPERATURE) == fake_device.current_temperature ) + assert state.state == HVAC_MODES.get(fake_device.mode) # Reset config temperature_unit back to CELSIUS, required for # additional tests outside this component. hass.config.units.temperature_unit = UnitOfTemperature.CELSIUS +@pytest.mark.parametrize( + ("temperature", "hvac_mode"), + [ + (26, HVACMode.OFF), + (26, HVACMode.HEAT), + (26, HVACMode.COOL), + (26, HVACMode.AUTO), + (26, HVACMode.DRY), + (26, HVACMode.FAN_ONLY), + ], +) +async def test_send_target_temperature_with_hvac_mode( + hass: HomeAssistant, discovery, device, temperature, hvac_mode +) -> None: + """Test for sending target temperature command to the device alongside hvac mode.""" + await async_setup_gree(hass) + + await hass.services.async_call( + DOMAIN, + SERVICE_SET_TEMPERATURE, + { + ATTR_ENTITY_ID: ENTITY_ID, + ATTR_TEMPERATURE: temperature, + ATTR_HVAC_MODE: hvac_mode, + }, + blocking=True, + ) + + state = hass.states.get(ENTITY_ID) + assert state is not None + assert state.attributes.get(ATTR_TEMPERATURE) == temperature + assert state.state == hvac_mode + + @pytest.mark.parametrize( ("units", "temperature"), [(UnitOfTemperature.CELSIUS, 25), (UnitOfTemperature.FAHRENHEIT, 74)],