From 325220e00945a9e40a012f29931c212b7b9a1b54 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 17 Sep 2016 19:51:18 -0700 Subject: [PATCH] Make track_point_in_utc_time more async (#3428) * Make track_point_in_utc_time more async * Make track_point_in_time async friendly --- homeassistant/helpers/event.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index ab0641cab9e..7331525c052 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -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