1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Restore_state helper to restore entity states from the DB on startup (#4614)

* Restore states

* feedback

* Remove component move into recorder

* space

* helper

* Address my own comments

* Improve test coverage

* Add test for light restore state
This commit is contained in:
Johann Kellerman
2017-02-21 09:40:27 +02:00
committed by Paulus Schoutsen
parent 2b9fb73032
commit fdc373f27e
18 changed files with 425 additions and 184 deletions

View File

@@ -22,7 +22,7 @@ from homeassistant.const import (
STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, EVENT_TIME_CHANGED,
EVENT_STATE_CHANGED, EVENT_PLATFORM_DISCOVERED, ATTR_SERVICE,
ATTR_DISCOVERED, SERVER_PORT)
from homeassistant.components import sun, mqtt
from homeassistant.components import sun, mqtt, recorder
from homeassistant.components.http.auth import auth_middleware
from homeassistant.components.http.const import (
KEY_USE_X_FORWARDED_FOR, KEY_BANS_ENABLED, KEY_TRUSTED_NETWORKS)
@@ -452,3 +452,31 @@ def assert_setup_component(count, domain=None):
res_len = 0 if res is None else len(res)
assert res_len == count, 'setup_component failed, expected {} got {}: {}' \
.format(count, res_len, res)
def init_recorder_component(hass, add_config=None, db_ready_callback=None):
"""Initialize the recorder."""
config = dict(add_config) if add_config else {}
config[recorder.CONF_DB_URL] = 'sqlite://' # In memory DB
saved_recorder = recorder.Recorder
class Recorder2(saved_recorder):
"""Recorder with a callback after db_ready."""
def _setup_connection(self):
"""Setup the connection and run the callback."""
super(Recorder2, self)._setup_connection()
if db_ready_callback:
_LOGGER.debug('db_ready_callback start (db_ready not set,'
'never use get_instance in the callback)')
db_ready_callback()
_LOGGER.debug('db_ready_callback completed')
with patch('homeassistant.components.recorder.Recorder',
side_effect=Recorder2):
assert setup_component(hass, recorder.DOMAIN,
{recorder.DOMAIN: config})
assert recorder.DOMAIN in hass.config.components
recorder.get_instance().block_till_db_ready()
_LOGGER.info("In-memory recorder successfully started")