Make track_point_in_utc_time more async (#3428)

* Make track_point_in_utc_time more async

* Make track_point_in_time async friendly
This commit is contained in:
Paulus Schoutsen 2016-09-17 19:51:18 -07:00 committed by GitHub
parent aca375c312
commit 325220e009

View file

@ -6,6 +6,7 @@ from datetime import timedelta
from ..const import (
ATTR_NOW, EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, MATCH_ALL)
from ..util import dt as dt_util
from ..util.async import run_callback_threadsafe
def track_state_change(hass, entity_ids, action, from_state=None,
@ -59,9 +60,10 @@ def track_point_in_time(hass, action, point_in_time):
utc_point_in_time = dt_util.as_utc(point_in_time)
@ft.wraps(action)
@asyncio.coroutine
def utc_converter(utc_now):
"""Convert passed in UTC now to local now."""
action(dt_util.as_local(utc_now))
hass.async_add_job(action, dt_util.as_local(utc_now))
return track_point_in_utc_time(hass, utc_converter, utc_point_in_time)
@ -86,15 +88,19 @@ def track_point_in_utc_time(hass, action, point_in_time):
# listener gets lined up twice to be executed. This will make
# sure the second time it does nothing.
point_in_time_listener.run = True
async_remove()
def fire_action():
"""Run the point in time listener action."""
remove()
action(now)
hass.async_add_job(action, now)
hass.add_job(fire_action)
future = run_callback_threadsafe(
hass.loop, hass.bus.async_listen, EVENT_TIME_CHANGED,
point_in_time_listener)
async_remove = future.result()
def remove():
"""Remove listener."""
run_callback_threadsafe(hass.loop, async_remove).result()
remove = hass.bus.listen(EVENT_TIME_CHANGED, point_in_time_listener)
return remove