Bugfix restore startup state ()

This commit is contained in:
Johann Kellerman 2017-02-24 06:06:21 +02:00 committed by Paulus Schoutsen
parent 7fcc3ae00a
commit 477f621705
3 changed files with 9 additions and 6 deletions
homeassistant

View file

@ -36,6 +36,7 @@ IGNORE_DOMAINS = ('zone', 'scene',)
def last_recorder_run():
"""Retireve the last closed recorder run from the DB."""
recorder.get_instance()
rec_runs = recorder.get_model('RecorderRuns')
with recorder.session_scope() as session:
res = recorder.query(rec_runs).order_by(rec_runs.end.desc()).first()

View file

@ -146,7 +146,7 @@ class InputBoolean(ToggleEntity):
state = yield from async_get_last_state(self.hass, self.entity_id)
if not state:
return
self._state = state.state == 'on'
self._state = state.state == STATE_ON
@asyncio.coroutine
def async_turn_on(self, **kwargs):

View file

@ -48,13 +48,15 @@ def _load_restore_cache(hass: HomeAssistant):
@asyncio.coroutine
def async_get_last_state(hass, entity_id: str):
"""Helper to restore state."""
if (_RECORDER not in hass.config.components or
hass.state != CoreState.starting):
return None
if DATA_RESTORE_CACHE in hass.data:
return hass.data[DATA_RESTORE_CACHE].get(entity_id)
if (_RECORDER not in hass.config.components or
hass.state not in (CoreState.starting, CoreState.not_running)):
_LOGGER.error("Cache can only be loaded during startup, not %s",
hass.state)
return None
if _LOCK not in hass.data:
hass.data[_LOCK] = asyncio.Lock(loop=hass.loop)
@ -63,7 +65,7 @@ def async_get_last_state(hass, entity_id: str):
yield from hass.loop.run_in_executor(
None, _load_restore_cache, hass)
return hass.data[DATA_RESTORE_CACHE].get(entity_id)
return hass.data.get(DATA_RESTORE_CACHE, {}).get(entity_id)
@asyncio.coroutine