diff --git a/homeassistant/components/gree/climate.py b/homeassistant/components/gree/climate.py index 6a33e3341b0..17d2045ee90 100644 --- a/homeassistant/components/gree/climate.py +++ b/homeassistant/components/gree/climate.py @@ -193,7 +193,7 @@ class GreeClimateEntity(CoordinatorEntity, ClimateEntity): return HVAC_MODES.get(self.coordinator.device.mode) - async def async_set_hvac_mode(self, hvac_mode): + async def async_set_hvac_mode(self, hvac_mode) -> None: """Set new target hvac mode.""" if hvac_mode not in self.hvac_modes: raise ValueError(f"Invalid hvac_mode: {hvac_mode}") @@ -217,6 +217,20 @@ class GreeClimateEntity(CoordinatorEntity, ClimateEntity): await self.coordinator.push_state_update() self.async_write_ha_state() + async def async_turn_on(self) -> None: + """Turn on the device.""" + _LOGGER.debug("Turning on HVAC for device %s", self._name) + + self._device.power = True + await self._push_state_update() + + async def async_turn_off(self) -> None: + """Turn off the device.""" + _LOGGER.debug("Turning off HVAC for device %s", self._name) + + self._device.power = False + await self._push_state_update() + @property def hvac_modes(self) -> List[str]: """Return the HVAC modes support by the device.""" diff --git a/tests/components/gree/test_climate.py b/tests/components/gree/test_climate.py index 534168fa78e..cfb689589bf 100644 --- a/tests/components/gree/test_climate.py +++ b/tests/components/gree/test_climate.py @@ -51,6 +51,8 @@ from homeassistant.const import ( ATTR_FRIENDLY_NAME, ATTR_SUPPORTED_FEATURES, ATTR_TEMPERATURE, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, STATE_UNAVAILABLE, ) from homeassistant.setup import async_setup_component @@ -244,14 +246,14 @@ async def test_send_power_on(hass, discovery, device, mock_now): assert await hass.services.async_call( DOMAIN, - SERVICE_SET_HVAC_MODE, - {ATTR_ENTITY_ID: ENTITY_ID, ATTR_HVAC_MODE: HVAC_MODE_AUTO}, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ENTITY_ID}, blocking=True, ) state = hass.states.get(ENTITY_ID) assert state is not None - assert state.state == HVAC_MODE_AUTO + assert state.state != HVAC_MODE_OFF async def test_send_power_on_device_timeout(hass, discovery, device, mock_now): @@ -262,14 +264,58 @@ async def test_send_power_on_device_timeout(hass, discovery, device, mock_now): assert await hass.services.async_call( DOMAIN, - SERVICE_SET_HVAC_MODE, - {ATTR_ENTITY_ID: ENTITY_ID, ATTR_HVAC_MODE: HVAC_MODE_AUTO}, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ENTITY_ID}, blocking=True, ) state = hass.states.get(ENTITY_ID) assert state is not None - assert state.state == HVAC_MODE_AUTO + assert state.state != HVAC_MODE_OFF + + +async def test_send_power_off(hass, discovery, device, mock_now): + """Test for sending power off command to the device.""" + await async_setup_gree(hass) + + next_update = mock_now + timedelta(minutes=5) + with patch("homeassistant.util.dt.utcnow", return_value=next_update): + async_fire_time_changed(hass, next_update) + await hass.async_block_till_done() + + assert await hass.services.async_call( + DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_ID}, + blocking=True, + ) + + state = hass.states.get(ENTITY_ID) + assert state is not None + assert state.state == HVAC_MODE_OFF + + +async def test_send_power_off_device_timeout(hass, discovery, device, mock_now): + """Test for sending power off command to the device with a device timeout.""" + device().push_state_update.side_effect = DeviceTimeoutError + + await async_setup_gree(hass) + + next_update = mock_now + timedelta(minutes=5) + with patch("homeassistant.util.dt.utcnow", return_value=next_update): + async_fire_time_changed(hass, next_update) + await hass.async_block_till_done() + + assert await hass.services.async_call( + DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ENTITY_ID}, + blocking=True, + ) + + state = hass.states.get(ENTITY_ID) + assert state is not None + assert state.state == HVAC_MODE_OFF async def test_send_target_temperature(hass, discovery, device, mock_now):