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:
Pascal Vizeli authored and GitHub committed 2017-09-05 02:01:01 +02:00
1 parent 67828cb7a2
commit ed699896cb
8 files changed
+548 -88

No files matched your search

@@ -1,11 +1,16 @@
"""The tests for numeric state automation."""
from datetime import timedelta
import unittest
from unittest.mock import patch
import homeassistant.components.automation as automation
from homeassistant.core import callback
from homeassistant.setup import setup_component
import homeassistant.components.automation as automation
import homeassistant.util.dt as dt_util
from tests.common import get_test_home_assistant, mock_component
from tests.common import (
get_test_home_assistant, mock_component, fire_time_changed,
assert_setup_component)
# pylint: disable=invalid-name
@@ -576,3 +581,126 @@ class TestAutomationNumericState(unittest.TestCase):
self.hass.block_till_done()
self.assertEqual(2, len(self.calls))
def test_if_fails_setup_bad_for(self):
"""Test for setup failure for bad for."""
with assert_setup_component(0):
assert setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'numeric_state',
'entity_id': 'test.entity',
'above': 8,
'below': 12,
'for': {
'invalid': 5
},
},
'action': {
'service': 'homeassistant.turn_on',
}
}})
def test_if_fails_setup_for_without_above_below(self):
"""Test for setup failures for missing above or below."""
with assert_setup_component(0):
assert setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'numeric_state',
'entity_id': 'test.entity',
'for': {
'seconds': 5
},
},
'action': {
'service': 'homeassistant.turn_on',
}
}})
def test_if_not_fires_on_entity_change_with_for(self):
"""Test for not firing on entity change with for."""
assert setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'numeric_state',
'entity_id': 'test.entity',
'above': 8,
'below': 12,
'for': {
'seconds': 5
},
},
'action': {
'service': 'test.automation'
}
}
})
self.hass.states.set('test.entity', 9)
self.hass.block_till_done()
self.hass.states.set('test.entity', 15)
self.hass.block_till_done()
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
self.hass.block_till_done()
self.assertEqual(0, len(self.calls))
def test_if_fires_on_entity_change_with_for_attribute_change(self):
"""Test for firing on entity change with for and attribute change."""
assert setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'numeric_state',
'entity_id': 'test.entity',
'above': 8,
'below': 12,
'for': {
'seconds': 5
},
},
'action': {
'service': 'test.automation'
}
}
})
utcnow = dt_util.utcnow()
with patch('homeassistant.core.dt_util.utcnow') as mock_utcnow:
mock_utcnow.return_value = utcnow
self.hass.states.set('test.entity', 9)
self.hass.block_till_done()
mock_utcnow.return_value += timedelta(seconds=4)
fire_time_changed(self.hass, mock_utcnow.return_value)
self.hass.states.set('test.entity', 9,
attributes={"mock_attr": "attr_change"})
self.hass.block_till_done()
self.assertEqual(0, len(self.calls))
mock_utcnow.return_value += timedelta(seconds=4)
fire_time_changed(self.hass, mock_utcnow.return_value)
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
def test_if_fires_on_entity_change_with_for(self):
"""Test for firing on entity change with for."""
assert setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
'platform': 'numeric_state',
'entity_id': 'test.entity',
'above': 8,
'below': 12,
'for': {
'seconds': 5
},
},
'action': {
'service': 'test.automation'
}
}
})
self.hass.states.set('test.entity', 9)
self.hass.block_till_done()
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
+133 -7
View File
@@ -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'