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

Template binary_sensor change flow / add restore (#6343)

* Template binary_sensor change flow / add restore

* fix lint
This commit is contained in:
Pascal Vizeli
2017-03-02 08:50:41 +01:00
committed by GitHub
parent 31bf5b8ff0
commit f3870a8a48
2 changed files with 92 additions and 20 deletions

View File

@@ -1,15 +1,19 @@
"""The tests for the Template Binary sensor platform."""
import asyncio
import unittest
from unittest import mock
from homeassistant.const import EVENT_STATE_CHANGED, MATCH_ALL
from homeassistant.core import CoreState, State
from homeassistant.const import MATCH_ALL
import homeassistant.bootstrap as bootstrap
from homeassistant.components.binary_sensor import template
from homeassistant.exceptions import TemplateError
from homeassistant.helpers import template as template_hlpr
from homeassistant.util.async import run_callback_threadsafe
from homeassistant.helpers.restore_state import DATA_RESTORE_CACHE
from tests.common import get_test_home_assistant, assert_setup_component
from tests.common import (
get_test_home_assistant, assert_setup_component, mock_component)
class TestBinarySensorTemplate(unittest.TestCase):
@@ -26,8 +30,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
"""Stop everything that was started."""
self.hass.stop()
@mock.patch.object(template, 'BinarySensorTemplate')
def test_setup(self, mock_template):
def test_setup(self):
""""Test the setup."""
config = {
'binary_sensor': {
@@ -117,18 +120,34 @@ class TestBinarySensorTemplate(unittest.TestCase):
def test_event(self):
""""Test the event."""
vs = run_callback_threadsafe(
self.hass.loop, template.BinarySensorTemplate,
self.hass, 'parent', 'Parent', 'motion',
template_hlpr.Template('{{ 1 > 1 }}', self.hass), MATCH_ALL
).result()
vs.update_ha_state()
config = {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test': {
'friendly_name': 'virtual thingy',
'value_template':
"{{ states.sensor.test_state.state == 'on' }}",
'device_class': 'motion',
},
},
},
}
with assert_setup_component(1):
assert bootstrap.setup_component(
self.hass, 'binary_sensor', config)
self.hass.start()
self.hass.block_till_done()
with mock.patch.object(vs, 'async_update') as mock_update:
self.hass.bus.fire(EVENT_STATE_CHANGED)
self.hass.block_till_done()
assert mock_update.call_count == 1
state = self.hass.states.get('binary_sensor.test')
assert state.state == 'off'
self.hass.states.set('sensor.test_state', 'on')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test')
assert state.state == 'on'
@mock.patch('homeassistant.helpers.template.Template.render')
def test_update_template_error(self, mock_render):
@@ -143,3 +162,38 @@ class TestBinarySensorTemplate(unittest.TestCase):
mock_render.side_effect = TemplateError(
"UndefinedError: 'None' has no attribute")
vs.update()
@asyncio.coroutine
def test_restore_state(hass):
"""Ensure states are restored on startup."""
hass.data[DATA_RESTORE_CACHE] = {
'binary_sensor.test': State('binary_sensor.test', 'on'),
}
hass.state = CoreState.starting
mock_component(hass, 'recorder')
config = {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test': {
'friendly_name': 'virtual thingy',
'value_template':
"{{ states.sensor.test_state.state == 'on' }}",
'device_class': 'motion',
},
},
},
}
yield from bootstrap.async_setup_component(hass, 'binary_sensor', config)
state = hass.states.get('binary_sensor.test')
assert state.state == 'on'
yield from hass.async_start()
yield from hass.async_block_till_done()
state = hass.states.get('binary_sensor.test')
assert state.state == 'off'