Restore target temperature for generic thermostat (#10635)

* Restore target temp for generic thermostat

* Fix lint
This commit is contained in:
Lukas Barth 2017-11-17 17:32:58 +01:00 committed by Daniel Høyer Iversen
parent be5f0fb3ac
commit 68d2076b56
2 changed files with 37 additions and 2 deletions

View File

@ -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."""

View File

@ -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)