diff --git a/homeassistant/components/history.py b/homeassistant/components/history.py index c4eada498da..254115c55b1 100644 --- a/homeassistant/components/history.py +++ b/homeassistant/components/history.py @@ -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() diff --git a/homeassistant/components/input_boolean.py b/homeassistant/components/input_boolean.py index 1817181b184..290820f3bd4 100644 --- a/homeassistant/components/input_boolean.py +++ b/homeassistant/components/input_boolean.py @@ -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): diff --git a/homeassistant/helpers/restore_state.py b/homeassistant/helpers/restore_state.py index dfed0f52413..1e463d316d4 100644 --- a/homeassistant/helpers/restore_state.py +++ b/homeassistant/helpers/restore_state.py @@ -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