From b4481269ec79091c4130aab8c623acabf3d70f20 Mon Sep 17 00:00:00 2001 From: Andrew Sayre <6730289+andrewsayre@users.noreply.github.com> Date: Fri, 19 Jul 2019 13:19:34 -0400 Subject: [PATCH] Turn on device before setting mode (#25314) --- .../components/smartthings/climate.py | 16 ++++-- tests/components/smartthings/test_climate.py | 50 +++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/smartthings/climate.py b/homeassistant/components/smartthings/climate.py index ca98f9827c8..d0c426ba9fd 100644 --- a/homeassistant/components/smartthings/climate.py +++ b/homeassistant/components/smartthings/climate.py @@ -326,8 +326,13 @@ class SmartThingsAirConditioner(SmartThingsEntity, ClimateDevice): if hvac_mode == HVAC_MODE_OFF: await self.async_turn_off() return - await self._device.set_air_conditioner_mode( - STATE_TO_AC_MODE[hvac_mode], set_status=True) + tasks = [] + # Turn on the device if it's off before setting mode. + if not self._device.status.switch: + tasks.append(self._device.switch_on(set_status=True)) + tasks.append(self._device.set_air_conditioner_mode( + STATE_TO_AC_MODE[hvac_mode], set_status=True)) + await asyncio.gather(*tasks) # State is set optimistically in the command above, therefore update # the entity state ahead of receiving the confirming push updates self.async_schedule_update_ha_state() @@ -338,7 +343,12 @@ class SmartThingsAirConditioner(SmartThingsEntity, ClimateDevice): # operation mode operation_mode = kwargs.get(ATTR_HVAC_MODE) if operation_mode: - tasks.append(self.async_set_hvac_mode(operation_mode)) + if operation_mode == HVAC_MODE_OFF: + tasks.append(self._device.switch_off(set_status=True)) + else: + if not self._device.status.switch: + tasks.append(self._device.switch_on(set_status=True)) + tasks.append(self.async_set_hvac_mode(operation_mode)) # temperature tasks.append(self._device.set_cooling_setpoint( kwargs[ATTR_TEMPERATURE], set_status=True)) diff --git a/tests/components/smartthings/test_climate.py b/tests/components/smartthings/test_climate.py index 306b496359f..bc16fe6add2 100644 --- a/tests/components/smartthings/test_climate.py +++ b/tests/components/smartthings/test_climate.py @@ -293,6 +293,23 @@ async def test_set_hvac_mode(hass, thermostat, air_conditioner): assert state.state == HVAC_MODE_COOL, entity_id +async def test_ac_set_hvac_mode_from_off(hass, air_conditioner): + """Test setting HVAC mode when the unit is off.""" + air_conditioner.status.update_attribute_value( + Attribute.air_conditioner_mode, 'heat') + air_conditioner.status.update_attribute_value(Attribute.switch, 'off') + await setup_platform(hass, CLIMATE_DOMAIN, devices=[air_conditioner]) + state = hass.states.get('climate.air_conditioner') + assert state.state == HVAC_MODE_OFF + await hass.services.async_call( + CLIMATE_DOMAIN, SERVICE_SET_HVAC_MODE, { + ATTR_ENTITY_ID: 'climate.air_conditioner', + ATTR_HVAC_MODE: HVAC_MODE_HEAT_COOL}, + blocking=True) + state = hass.states.get('climate.air_conditioner') + assert state.state == HVAC_MODE_HEAT_COOL + + async def test_ac_set_hvac_mode_off(hass, air_conditioner): """Test the AC HVAC mode can be turned off set successfully.""" await setup_platform(hass, CLIMATE_DOMAIN, devices=[air_conditioner]) @@ -376,6 +393,39 @@ async def test_set_temperature_ac_with_mode(hass, air_conditioner): assert state.state == HVAC_MODE_COOL +async def test_set_temperature_ac_with_mode_from_off(hass, air_conditioner): + """Test the temp and mode is set successfully when the unit is off.""" + air_conditioner.status.update_attribute_value( + Attribute.air_conditioner_mode, 'heat') + air_conditioner.status.update_attribute_value(Attribute.switch, 'off') + await setup_platform(hass, CLIMATE_DOMAIN, devices=[air_conditioner]) + assert hass.states.get('climate.air_conditioner').state == HVAC_MODE_OFF + await hass.services.async_call( + CLIMATE_DOMAIN, SERVICE_SET_TEMPERATURE, { + ATTR_ENTITY_ID: 'climate.air_conditioner', + ATTR_TEMPERATURE: 27, + ATTR_HVAC_MODE: HVAC_MODE_COOL}, + blocking=True) + state = hass.states.get('climate.air_conditioner') + assert state.attributes[ATTR_TEMPERATURE] == 27 + assert state.state == HVAC_MODE_COOL + + +async def test_set_temperature_ac_with_mode_to_off(hass, air_conditioner): + """Test the temp and mode is set successfully to turn off the unit.""" + await setup_platform(hass, CLIMATE_DOMAIN, devices=[air_conditioner]) + assert hass.states.get('climate.air_conditioner').state != HVAC_MODE_OFF + await hass.services.async_call( + CLIMATE_DOMAIN, SERVICE_SET_TEMPERATURE, { + ATTR_ENTITY_ID: 'climate.air_conditioner', + ATTR_TEMPERATURE: 27, + ATTR_HVAC_MODE: HVAC_MODE_OFF}, + blocking=True) + state = hass.states.get('climate.air_conditioner') + assert state.attributes[ATTR_TEMPERATURE] == 27 + assert state.state == HVAC_MODE_OFF + + async def test_set_temperature_with_mode(hass, thermostat): """Test the temperature and mode is set successfully.""" await setup_platform(hass, CLIMATE_DOMAIN, devices=[thermostat])