From a97e7bb22d13709e16b9d7c89df1df9f9c49fa26 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 10 Oct 2017 12:16:19 -0700 Subject: [PATCH] Simplify track_same_state (#9795) --- .../components/automation/numeric_state.py | 4 ++-- homeassistant/components/automation/state.py | 4 +++- .../components/binary_sensor/template.py | 6 +++--- homeassistant/helpers/event.py | 14 ++++---------- tests/helpers/test_event.py | 18 +++++++++++------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/automation/numeric_state.py b/homeassistant/components/automation/numeric_state.py index 51b2ea89f0f..571888038a6 100644 --- a/homeassistant/components/automation/numeric_state.py +++ b/homeassistant/components/automation/numeric_state.py @@ -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) diff --git a/homeassistant/components/automation/state.py b/homeassistant/components/automation/state.py index e7a01cb7115..7ed44761be8 100644 --- a/homeassistant/components/automation/state.py +++ b/homeassistant/components/automation/state.py @@ -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) diff --git a/homeassistant/components/binary_sensor/template.py b/homeassistant/components/binary_sensor/template.py index 84afd01303f..9356d87d7ea 100644 --- a/homeassistant/components/binary_sensor/template.py +++ b/homeassistant/components/binary_sensor/template.py @@ -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) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 18669a40fd0..c25402597cc 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -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) diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index 9c325df181e..222bb46f8b6 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -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")