8000 Simplify helpers.event.track_same_state API by balloob · Pull Request #9795 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Simplify helpers.event.track_same_state API #9795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions homeassistant/components/automation/numeric_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def call_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)
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/automation/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def call_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)
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/binary_sensor/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def should_poll(self):
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'
Expand Down Expand Up @@ -171,5 +171,5 @@ def set_state():

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)
14 changes: 4 additions & 10 deletions homeassistant/helpers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def template_condition_listener(entity_id, from_s, to_s):

@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.
Expand Down Expand Up @@ -152,14 +152,8 @@ def state_for_listener(now):
@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)
Expand Down
18 changes: 11 additions & 7 deletions tests/helpers/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,23 +274,26 @@ def thread_run_callback():
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
def callback_run_callback():
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
def coroutine_run_callback():
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")
Expand All @@ -317,7 +320,8 @@ def callback_run_callback():
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
Expand Down Expand Up @@ -349,11 +353,11 @@ def callback_run_callback():
@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")
Expand Down
0