From 68d2076b566749ce5a38d2712ede3ceb9d39f84c Mon Sep 17 00:00:00 2001 From: Lukas Barth Date: Fri, 17 Nov 2017 17:32:58 +0100 Subject: [PATCH] Restore target temperature for generic thermostat (#10635) * Restore target temp for generic thermostat * Fix lint --- .../components/climate/generic_thermostat.py | 12 +++++++++ .../climate/test_generic_thermostat.py | 27 +++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/climate/generic_thermostat.py b/homeassistant/components/climate/generic_thermostat.py index 4b86fa4067b..3f3470c1c86 100644 --- a/homeassistant/components/climate/generic_thermostat.py +++ b/homeassistant/components/climate/generic_thermostat.py @@ -21,6 +21,7 @@ from homeassistant.helpers import condition from homeassistant.helpers.event import ( async_track_state_change, async_track_time_interval) import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.restore_state import async_get_last_state _LOGGER = logging.getLogger(__name__) @@ -117,6 +118,17 @@ class GenericThermostat(ClimateDevice): if sensor_state: self._async_update_temp(sensor_state) + @asyncio.coroutine + def async_added_to_hass(self): + """Run when entity about to be added.""" + # If we have an old state and no target temp, restore + if self._target_temp is None: + old_state = yield from async_get_last_state(self.hass, + self.entity_id) + if old_state is not None: + self._target_temp = float( + old_state.attributes[ATTR_TEMPERATURE]) + @property def should_poll(self): """Return the polling state.""" diff --git a/tests/components/climate/test_generic_thermostat.py b/tests/components/climate/test_generic_thermostat.py index 25887211253..5982a6c16d8 100644 --- a/tests/components/climate/test_generic_thermostat.py +++ b/tests/components/climate/test_generic_thermostat.py @@ -6,7 +6,7 @@ from unittest import mock import pytz import homeassistant.core as ha -from homeassistant.core import callback +from homeassistant.core import callback, CoreState, State from homeassistant.setup import setup_component, async_setup_component from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, @@ -15,13 +15,15 @@ from homeassistant.const import ( STATE_ON, STATE_OFF, TEMP_CELSIUS, + ATTR_TEMPERATURE ) from homeassistant import loader from homeassistant.util.unit_system import METRIC_SYSTEM from homeassistant.util.async import run_coroutine_threadsafe from homeassistant.components import climate, input_boolean, switch import homeassistant.components as comps -from tests.common import assert_setup_component, get_test_home_assistant +from tests.common import (assert_setup_component, get_test_home_assistant, + mock_restore_cache) ENTITY = 'climate.test' @@ -892,3 +894,24 @@ def test_custom_setup_params(hass): assert state.attributes.get('min_temp') == MIN_TEMP assert state.attributes.get('max_temp') == MAX_TEMP assert state.attributes.get('temperature') == TARGET_TEMP + + +@asyncio.coroutine +def test_restore_state(hass): + """Ensure states are restored on startup.""" + mock_restore_cache(hass, ( + State('climate.test_thermostat', '0', {ATTR_TEMPERATURE: "20"}), + )) + + hass.state = CoreState.starting + + yield from async_setup_component( + hass, climate.DOMAIN, {'climate': { + 'platform': 'generic_thermostat', + 'name': 'test_thermostat', + 'heater': ENT_SWITCH, + 'target_sensor': ENT_SENSOR, + }}) + + state = hass.states.get('climate.test_thermostat') + assert(state.attributes[ATTR_TEMPERATURE] == 20)