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 parent
67828cb7a2
commit
ed699896cb
8 files changed
+548
-88
No files matched your search
+107
-1
@@ -17,6 +17,7 @@ from homeassistant.helpers.event import (
|
||||
track_state_change,
|
||||
track_time_interval,
|
||||
track_template,
|
||||
track_same_state,
|
||||
track_sunrise,
|
||||
track_sunset,
|
||||
)
|
||||
@@ -24,7 +25,7 @@ from homeassistant.helpers.template import Template
|
||||
from homeassistant.components import sun
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, fire_time_changed
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
@@ -262,6 +263,111 @@ class TestEventHelpers(unittest.TestCase):
|
||||
self.assertEqual(2, len(wildcard_runs))
|
||||
self.assertEqual(2, len(wildercard_runs))
|
||||
|
||||
def test_track_same_state_simple_trigger(self):
|
||||
"""Test track_same_change with trigger simple."""
|
||||
thread_runs = []
|
||||
callback_runs = []
|
||||
coroutine_runs = []
|
||||
period = timedelta(minutes=1)
|
||||
|
||||
def thread_run_callback():
|
||||
thread_runs.append(1)
|
||||
|
||||
track_same_state(
|
||||
self.hass, 'on', period, thread_run_callback,
|
||||
entity_ids='light.Bowl')
|
||||
|
||||
@ha.callback
|
||||
def callback_run_callback():
|
||||
callback_runs.append(1)
|
||||
|
||||
track_same_state(
|
||||
self.hass, 'on', period, callback_run_callback,
|
||||
entity_ids='light.Bowl')
|
||||
|
||||
@asyncio.coroutine
|
||||
def coroutine_run_callback():
|
||||
coroutine_runs.append(1)
|
||||
|
||||
track_same_state(
|
||||
self.hass, 'on', period, coroutine_run_callback)
|
||||
|
||||
# Adding state to state machine
|
||||
self.hass.states.set("light.Bowl", "on")
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(thread_runs))
|
||||
self.assertEqual(0, len(callback_runs))
|
||||
self.assertEqual(0, len(coroutine_runs))
|
||||
|
||||
# change time to track and see if they trigger
|
||||
future = dt_util.utcnow() + period
|
||||
fire_time_changed(self.hass, future)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(thread_runs))
|
||||
self.assertEqual(1, len(callback_runs))
|
||||
self.assertEqual(1, len(coroutine_runs))
|
||||
|
||||
def test_track_same_state_simple_no_trigger(self):
|
||||
"""Test track_same_change with no trigger."""
|
||||
callback_runs = []
|
||||
period = timedelta(minutes=1)
|
||||
|
||||
@ha.callback
|
||||
def callback_run_callback():
|
||||
callback_runs.append(1)
|
||||
|
||||
track_same_state(
|
||||
self.hass, 'on', period, callback_run_callback,
|
||||
entity_ids='light.Bowl')
|
||||
|
||||
# Adding state to state machine
|
||||
self.hass.states.set("light.Bowl", "on")
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(callback_runs))
|
||||
|
||||
# Change state on state machine
|
||||
self.hass.states.set("light.Bowl", "off")
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(callback_runs))
|
||||
|
||||
# change time to track and see if they trigger
|
||||
future = dt_util.utcnow() + period
|
||||
fire_time_changed(self.hass, future)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(callback_runs))
|
||||
|
||||
def test_track_same_state_simple_trigger_check_funct(self):
|
||||
"""Test track_same_change with trigger and check funct."""
|
||||
callback_runs = []
|
||||
check_func = []
|
||||
period = timedelta(minutes=1)
|
||||
|
||||
@ha.callback
|
||||
def callback_run_callback():
|
||||
callback_runs.append(1)
|
||||
|
||||
@ha.callback
|
||||
def async_check_func(entity, from_s, to_s):
|
||||
check_func.append((entity, from_s, to_s))
|
||||
return 'on'
|
||||
|
||||
track_same_state(
|
||||
self.hass, 'on', period, callback_run_callback,
|
||||
entity_ids='light.Bowl', async_check_func=async_check_func)
|
||||
|
||||
# Adding state to state machine
|
||||
self.hass.states.set("light.Bowl", "on")
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(callback_runs))
|
||||
self.assertEqual('on', check_func[-1][2].state)
|
||||
self.assertEqual('light.bowl', check_func[-1][0])
|
||||
|
||||
# change time to track and see if they trigger
|
||||
future = dt_util.utcnow() + period
|
||||
fire_time_changed(self.hass, future)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(callback_runs))
|
||||
|
||||
def test_track_time_interval(self):
|
||||
"""Test tracking time interval."""
|
||||
specific_runs = []
|
||||
|
||||
Reference in new issue
Block a user