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

@ -1,17 +1,20 @@
"""The tests for the demo light component."""
# pylint: disable=protected-access
import asyncio
import unittest
from homeassistant.bootstrap import setup_component
from homeassistant.core import State, CoreState
from homeassistant.bootstrap import setup_component, async_setup_component
import homeassistant.components.light as light
from homeassistant.helpers.restore_state import DATA_RESTORE_CACHE
from tests.common import get_test_home_assistant
ENTITY_LIGHT = 'light.bed_light'
class TestDemoClimate(unittest.TestCase):
"""Test the demo climate hvac."""
class TestDemoLight(unittest.TestCase):
"""Test the demo light."""
# pylint: disable=invalid-name
def setUp(self):
@ -60,3 +63,36 @@ class TestDemoClimate(unittest.TestCase):
light.turn_off(self.hass, ENTITY_LIGHT)
self.hass.block_till_done()
self.assertFalse(light.is_on(self.hass, ENTITY_LIGHT))
@asyncio.coroutine
def test_restore_state(hass):
"""Test state gets restored."""
hass.config.components.add('recorder')
hass.state = CoreState.starting
hass.data[DATA_RESTORE_CACHE] = {
'light.bed_light': State('light.bed_light', 'on', {
'brightness': 'value-brightness',
'color_temp': 'value-color_temp',
'rgb_color': 'value-rgb_color',
'xy_color': 'value-xy_color',
'white_value': 'value-white_value',
'effect': 'value-effect',
}),
}
yield from async_setup_component(hass, 'light', {
'light': {
'platform': 'demo',
}})
state = hass.states.get('light.bed_light')
assert state is not None
assert state.entity_id == 'light.bed_light'
assert state.state == 'on'
assert state.attributes.get('brightness') == 'value-brightness'
assert state.attributes.get('color_temp') == 'value-color_temp'
assert state.attributes.get('rgb_color') == 'value-rgb_color'
assert state.attributes.get('xy_color') == 'value-xy_color'
assert state.attributes.get('white_value') == 'value-white_value'
assert state.attributes.get('effect') == 'value-effect'