Simplify track_same_state (#9795)
This commit is contained in:
parent
fc47e9443b
commit
a97e7bb22d
5 changed files with 23 additions and 23 deletions
|
@ -99,8 +99,8 @@ def async_trigger(hass, config, action):
|
|||
return
|
||||
|
||||
async_remove_track_same = async_track_same_state(
|
||||
hass, True, time_delta, call_action, entity_ids=entity_id,
|
||||
async_check_func=check_numeric_state)
|
||||
hass, time_delta, call_action, entity_ids=entity_id,
|
||||
async_check_same_func=check_numeric_state)
|
||||
|
||||
unsub = async_track_state_change(
|
||||
hass, entity_id, state_automation_listener)
|
||||
|
|
|
@ -65,7 +65,9 @@ def async_trigger(hass, config, action):
|
|||
return
|
||||
|
||||
async_remove_track_same = async_track_same_state(
|
||||
hass, to_s.state, time_delta, call_action, entity_ids=entity_id)
|
||||
hass, time_delta, call_action,
|
||||
lambda _, _2, to_state: to_state.state == to_s.state,
|
||||
entity_ids=entity_id)
|
||||
|
||||
unsub = async_track_state_change(
|
||||
hass, entity_id, state_automation_listener, from_state, to_state)
|
||||
|
|
|
@ -135,7 +135,7 @@ class BinarySensorTemplate(BinarySensorDevice):
|
|||
return False
|
||||
|
||||
@callback
|
||||
def _async_render(self, *args):
|
||||
def _async_render(self):
|
||||
"""Get the state of template."""
|
||||
try:
|
||||
return self._template.async_render().lower() == 'true'
|
||||
|
@ -171,5 +171,5 @@ class BinarySensorTemplate(BinarySensorDevice):
|
|||
|
||||
period = self._delay_on if state else self._delay_off
|
||||
async_track_same_state(
|
||||
self.hass, state, period, set_state, entity_ids=self._entities,
|
||||
async_check_func=self._async_render)
|
||||
self.hass, period, set_state, entity_ids=self._entities,
|
||||
async_check_same_func=lambda *args: self._async_render() == state)
|
||||
|
|
|
@ -118,8 +118,8 @@ track_template = threaded_listener_factory(async_track_template)
|
|||
|
||||
@callback
|
||||
@bind_hass
|
||||
def async_track_same_state(hass, orig_value, period, action,
|
||||
async_check_func=None, entity_ids=MATCH_ALL):
|
||||
def async_track_same_state(hass, period, action, async_check_same_func,
|
||||
entity_ids=MATCH_ALL):
|
||||
"""Track the state of entities for a period and run a action.
|
||||
|
||||
If async_check_func is None it use the state of orig_value.
|
||||
|
@ -152,14 +152,8 @@ def async_track_same_state(hass, orig_value, period, action,
|
|||
@callback
|
||||
def state_for_cancel_listener(entity, from_state, to_state):
|
||||
"""Fire on changes and cancel for listener if changed."""
|
||||
if async_check_func:
|
||||
value = async_check_func(entity, from_state, to_state)
|
||||
else:
|
||||
value = to_state.state
|
||||
|
||||
if orig_value == value:
|
||||
return
|
||||
clear_listener()
|
||||
if not async_check_same_func(entity, from_state, to_state):
|
||||
clear_listener()
|
||||
|
||||
async_remove_state_for_listener = async_track_point_in_utc_time(
|
||||
hass, state_for_listener, dt_util.utcnow() + period)
|
||||
|
|
|
@ -274,7 +274,8 @@ class TestEventHelpers(unittest.TestCase):
|
|||
thread_runs.append(1)
|
||||
|
||||
track_same_state(
|
||||
self.hass, 'on', period, thread_run_callback,
|
||||
self.hass, period, thread_run_callback,
|
||||
lambda _, _2, to_s: to_s.state == 'on',
|
||||
entity_ids='light.Bowl')
|
||||
|
||||
@ha.callback
|
||||
|
@ -282,7 +283,8 @@ class TestEventHelpers(unittest.TestCase):
|
|||
callback_runs.append(1)
|
||||
|
||||
track_same_state(
|
||||
self.hass, 'on', period, callback_run_callback,
|
||||
self.hass, period, callback_run_callback,
|
||||
lambda _, _2, to_s: to_s.state == 'on',
|
||||
entity_ids='light.Bowl')
|
||||
|
||||
@asyncio.coroutine
|
||||
|
@ -290,7 +292,8 @@ class TestEventHelpers(unittest.TestCase):
|
|||
coroutine_runs.append(1)
|
||||
|
||||
track_same_state(
|
||||
self.hass, 'on', period, coroutine_run_callback)
|
||||
self.hass, period, coroutine_run_callback,
|
||||
lambda _, _2, to_s: to_s.state == 'on')
|
||||
|
||||
# Adding state to state machine
|
||||
self.hass.states.set("light.Bowl", "on")
|
||||
|
@ -317,7 +320,8 @@ class TestEventHelpers(unittest.TestCase):
|
|||
callback_runs.append(1)
|
||||
|
||||
track_same_state(
|
||||
self.hass, 'on', period, callback_run_callback,
|
||||
self.hass, period, callback_run_callback,
|
||||
lambda _, _2, to_s: to_s.state == 'on',
|
||||
entity_ids='light.Bowl')
|
||||
|
||||
# Adding state to state machine
|
||||
|
@ -349,11 +353,11 @@ class TestEventHelpers(unittest.TestCase):
|
|||
@ha.callback
|
||||
def async_check_func(entity, from_s, to_s):
|
||||
check_func.append((entity, from_s, to_s))
|
||||
return 'on'
|
||||
return True
|
||||
|
||||
track_same_state(
|
||||
self.hass, 'on', period, callback_run_callback,
|
||||
entity_ids='light.Bowl', async_check_func=async_check_func)
|
||||
self.hass, period, callback_run_callback,
|
||||
entity_ids='light.Bowl', async_check_same_func=async_check_func)
|
||||
|
||||
# Adding state to state machine
|
||||
self.hass.states.set("light.Bowl", "on")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue