Core track same state for a period / Allow on platforms (#9273)
* Core track state period / Allow on platforms * Add tests * fix lint * fix tests * add new tracker to automation state * update schema * fix bug * revert validate string * Fix bug * Set arguments to async_check_funct * add logic into numeric_state * fix numeric_state * Add tests * fix retrigger state * cleanup * Add delay function to template binary_sensor * Fix tests & lint * add more tests * fix lint * Address comments * fix test & lint
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""The tests for the Template Binary sensor platform."""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
@@ -10,10 +11,12 @@ 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
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.helpers.restore_state import DATA_RESTORE_CACHE
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, assert_setup_component, mock_component)
|
||||
get_test_home_assistant, assert_setup_component, mock_component,
|
||||
async_fire_time_changed)
|
||||
|
||||
|
||||
class TestBinarySensorTemplate(unittest.TestCase):
|
||||
@@ -103,19 +106,20 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
||||
vs = run_callback_threadsafe(
|
||||
self.hass.loop, template.BinarySensorTemplate,
|
||||
self.hass, 'parent', 'Parent', 'motion',
|
||||
template_hlpr.Template('{{ 1 > 1 }}', self.hass), MATCH_ALL
|
||||
template_hlpr.Template('{{ 1 > 1 }}', self.hass), MATCH_ALL,
|
||||
None, None
|
||||
).result()
|
||||
self.assertFalse(vs.should_poll)
|
||||
self.assertEqual('motion', vs.device_class)
|
||||
self.assertEqual('Parent', vs.name)
|
||||
|
||||
vs.update()
|
||||
run_callback_threadsafe(self.hass.loop, vs.async_check_state).result()
|
||||
self.assertFalse(vs.is_on)
|
||||
|
||||
# pylint: disable=protected-access
|
||||
vs._template = template_hlpr.Template("{{ 2 > 1 }}", self.hass)
|
||||
|
||||
vs.update()
|
||||
run_callback_threadsafe(self.hass.loop, vs.async_check_state).result()
|
||||
self.assertTrue(vs.is_on)
|
||||
|
||||
def test_event(self):
|
||||
@@ -155,13 +159,14 @@ class TestBinarySensorTemplate(unittest.TestCase):
|
||||
vs = run_callback_threadsafe(
|
||||
self.hass.loop, template.BinarySensorTemplate,
|
||||
self.hass, 'parent', 'Parent', 'motion',
|
||||
template_hlpr.Template('{{ 1 > 1 }}', self.hass), MATCH_ALL
|
||||
template_hlpr.Template('{{ 1 > 1 }}', self.hass), MATCH_ALL,
|
||||
None, None
|
||||
).result()
|
||||
mock_render.side_effect = TemplateError('foo')
|
||||
vs.update()
|
||||
run_callback_threadsafe(self.hass.loop, vs.async_check_state).result()
|
||||
mock_render.side_effect = TemplateError(
|
||||
"UndefinedError: 'None' has no attribute")
|
||||
vs.update()
|
||||
run_callback_threadsafe(self.hass.loop, vs.async_check_state).result()
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -197,3 +202,124 @@ def test_restore_state(hass):
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_template_delay_on(hass):
|
||||
"""Test binary sensor template delay on."""
|
||||
config = {
|
||||
'binary_sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test': {
|
||||
'friendly_name': 'virtual thingy',
|
||||
'value_template':
|
||||
"{{ states.sensor.test_state.state == 'on' }}",
|
||||
'device_class': 'motion',
|
||||
'delay_on': 5
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
yield from setup.async_setup_component(hass, 'binary_sensor', config)
|
||||
yield from hass.async_start()
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'on')
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||
async_fire_time_changed(hass, future)
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
# check with time changes
|
||||
hass.states.async_set('sensor.test_state', 'off')
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'on')
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'off')
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||
async_fire_time_changed(hass, future)
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_template_delay_off(hass):
|
||||
"""Test binary sensor template delay off."""
|
||||
config = {
|
||||
'binary_sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test': {
|
||||
'friendly_name': 'virtual thingy',
|
||||
'value_template':
|
||||
"{{ states.sensor.test_state.state == 'on' }}",
|
||||
'device_class': 'motion',
|
||||
'delay_off': 5
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
hass.states.async_set('sensor.test_state', 'on')
|
||||
yield from setup.async_setup_component(hass, 'binary_sensor', config)
|
||||
yield from hass.async_start()
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'off')
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||
async_fire_time_changed(hass, future)
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'off'
|
||||
|
||||
# check with time changes
|
||||
hass.states.async_set('sensor.test_state', 'on')
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'off')
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
hass.states.async_set('sensor.test_state', 'on')
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||
async_fire_time_changed(hass, future)
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.test')
|
||||
assert state.state == 'on'
|
||||
|
||||
Reference in New Issue
Block a user