From a454dc650f5c0fee0185d6c6f1b0e1d2004ebdc4 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Sat, 20 Oct 2018 22:24:43 +0300 Subject: [PATCH 1/6] Resolves /home-assistant/home-assistant#17433 Away mode temperature issue fix for generic_thermostat --- .../components/climate/generic_thermostat.py | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/climate/generic_thermostat.py b/homeassistant/components/climate/generic_thermostat.py index 258699ff90acba..9ab5eaf74fcb7b 100644 --- a/homeassistant/components/climate/generic_thermostat.py +++ b/homeassistant/components/climate/generic_thermostat.py @@ -380,15 +380,21 @@ def is_away_mode_on(self): async def async_turn_away_mode_on(self): """Turn away mode on by setting it on away hold indefinitely.""" - self._is_away = True - self._saved_target_temp = self._target_temp - self._target_temp = self._away_temp - await self._async_control_heating() - await self.async_update_ha_state() + if not self._is_away: + self._is_away = True + self._saved_target_temp = self._target_temp + self._target_temp = self._away_temp + await self._async_control_heating() + await self.async_update_ha_state() + else: + _LOGGER.debug("Away mode is already turned on for %s", self.entity_id) async def async_turn_away_mode_off(self): """Turn away off.""" - self._is_away = False - self._target_temp = self._saved_target_temp - await self._async_control_heating() - await self.async_update_ha_state() + if self._is_away: + self._is_away = False + self._target_temp = self._saved_target_temp + await self._async_control_heating() + await self.async_update_ha_state() + else: + _LOGGER.debug("Away mode is already turned off for %s", self.entity_id) From c8105bc1b34f1c6d8e6182393727ea11a2ade3b2 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Sat, 20 Oct 2018 22:36:34 +0300 Subject: [PATCH 2/6] Debug messages removed from generic_thermostat.py --- homeassistant/components/climate/generic_thermostat.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/homeassistant/components/climate/generic_thermostat.py b/homeassistant/components/climate/generic_thermostat.py index 9ab5eaf74fcb7b..ca331774dafe4b 100644 --- a/homeassistant/components/climate/generic_thermostat.py +++ b/homeassistant/components/climate/generic_thermostat.py @@ -386,8 +386,6 @@ async def async_turn_away_mode_on(self): self._target_temp = self._away_temp await self._async_control_heating() await self.async_update_ha_state() - else: - _LOGGER.debug("Away mode is already turned on for %s", self.entity_id) async def async_turn_away_mode_off(self): """Turn away off.""" @@ -396,5 +394,3 @@ async def async_turn_away_mode_off(self): self._target_temp = self._saved_target_temp await self._async_control_heating() await self.async_update_ha_state() - else: - _LOGGER.debug("Away mode is already turned off for %s", self.entity_id) From 27e5d78b21dca5d826ff0a204a7ba618528b76ca Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Mon, 22 Oct 2018 14:25:22 +0300 Subject: [PATCH 3/6] Test for repeat away_mode set Test for fix of generic thermostat issue when away_mode was set several times in a row. --- .../climate/test_generic_thermostat.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/components/climate/test_generic_thermostat.py b/tests/components/climate/test_generic_thermostat.py index 47ec621aeb5987..85d94b3213bc45 100644 --- a/tests/components/climate/test_generic_thermostat.py +++ b/tests/components/climate/test_generic_thermostat.py @@ -220,6 +220,24 @@ def test_set_away_mode_and_restore_prev_temp(self): self.hass.block_till_done() state = self.hass.states.get(ENTITY) self.assertEqual(23, state.attributes.get('temperature')) + + def test_set_away_mode_twice_and_restore_prev_temp(self): + """Test the setting away mode twice in a row + + Verify original temperature is restored. + """ + common.set_temperature(self.hass, 23) + self.hass.block_till_done() + common.set_away_mode(self.hass, True) + self.hass.block_till_done() + common.set_away_mode(self.hass, True) + self.hass.block_till_done() + state = self.hass.states.get(ENTITY) + self.assertEqual(16, state.attributes.get('temperature')) + common.set_away_mode(self.hass, False) + self.hass.block_till_done() + state = self.hass.states.get(ENTITY) + self.assertEqual(23, state.attributes.get('temperature')) def test_sensor_bad_value(self): """Test sensor that have None as state.""" From def674926191fe70d051a3b6c015c036bae64b66 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Mon, 22 Oct 2018 17:58:08 +0300 Subject: [PATCH 4/6] Code style fix in generic_thermostat --- .../components/climate/generic_thermostat.py | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/climate/generic_thermostat.py b/homeassistant/components/climate/generic_thermostat.py index ca331774dafe4b..ad8875462fdd8c 100644 --- a/homeassistant/components/climate/generic_thermostat.py +++ b/homeassistant/components/climate/generic_thermostat.py @@ -380,17 +380,19 @@ def is_away_mode_on(self): async def async_turn_away_mode_on(self): """Turn away mode on by setting it on away hold indefinitely.""" - if not self._is_away: - self._is_away = True - self._saved_target_temp = self._target_temp - self._target_temp = self._away_temp - await self._async_control_heating() - await self.async_update_ha_state() + if self._is_away: + return + self._is_away = True + self._saved_target_temp = self._target_temp + self._target_temp = self._away_temp + await self._async_control_heating() + await self.async_update_ha_state() async def async_turn_away_mode_off(self): """Turn away off.""" - if self._is_away: - self._is_away = False - self._target_temp = self._saved_target_temp - await self._async_control_heating() - await self.async_update_ha_state() + if not self._is_away: + return + self._is_away = False + self._target_temp = self._saved_target_temp + await self._async_control_heating() + await self.async_update_ha_state() From 59f4fddeaf04e4e53ca84860513debe0b925d6fb Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Mon, 22 Oct 2018 21:32:00 +0300 Subject: [PATCH 5/6] Remove blank line in the end of generic_thermostat From 3deaad02cdb475534a208c2f981f413749466863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=B8yer=20Iversen?= Date: Tue, 23 Oct 2018 07:44:52 +0200 Subject: [PATCH 6/6] Fix style --- tests/components/climate/test_generic_thermostat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/components/climate/test_generic_thermostat.py b/tests/components/climate/test_generic_thermostat.py index 85d94b3213bc45..8bbcbc8f840d12 100644 --- a/tests/components/climate/test_generic_thermostat.py +++ b/tests/components/climate/test_generic_thermostat.py @@ -220,9 +220,9 @@ def test_set_away_mode_and_restore_prev_temp(self): self.hass.block_till_done() state = self.hass.states.get(ENTITY) self.assertEqual(23, state.attributes.get('temperature')) - + def test_set_away_mode_twice_and_restore_prev_temp(self): - """Test the setting away mode twice in a row + """Test the setting away mode twice in a row. Verify original temperature is restored. """