Address asyncio comments (#3663)
* Template platforms: create_task instead of yield from * Automation: less yielding, more create_tasking * Helpers.script: less yielding, more create_tasking * Deflake logbook test * Deflake automation reload config test * MQTT: Use async_add_job and threaded_listener_factory * Deflake other logbook test * lint * Add test for automation trigger service * MQTT client can be called from within async
This commit is contained in:
1 parent
f2a12b7ac2
commit
d58548dd1c
10 files changed
+123
-76
No files matched your search
@@ -9,11 +9,11 @@ from ..const import (
|
||||
from ..util import dt as dt_util
|
||||
from ..util.async import run_callback_threadsafe
|
||||
|
||||
# PyLint does not like the use of _threaded_factory
|
||||
# PyLint does not like the use of threaded_listener_factory
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
|
||||
def _threaded_factory(async_factory):
|
||||
def threaded_listener_factory(async_factory):
|
||||
"""Convert an async event helper to a threaded one."""
|
||||
@ft.wraps(async_factory)
|
||||
def factory(*args, **kwargs):
|
||||
@@ -83,7 +83,7 @@ def async_track_state_change(hass, entity_ids, action, from_state=None,
|
||||
return hass.bus.async_listen(EVENT_STATE_CHANGED, state_change_listener)
|
||||
|
||||
|
||||
track_state_change = _threaded_factory(async_track_state_change)
|
||||
track_state_change = threaded_listener_factory(async_track_state_change)
|
||||
|
||||
|
||||
def async_track_point_in_time(hass, action, point_in_time):
|
||||
@@ -100,7 +100,7 @@ def async_track_point_in_time(hass, action, point_in_time):
|
||||
utc_point_in_time)
|
||||
|
||||
|
||||
track_point_in_time = _threaded_factory(async_track_point_in_time)
|
||||
track_point_in_time = threaded_listener_factory(async_track_point_in_time)
|
||||
|
||||
|
||||
def async_track_point_in_utc_time(hass, action, point_in_time):
|
||||
@@ -133,7 +133,8 @@ def async_track_point_in_utc_time(hass, action, point_in_time):
|
||||
return async_unsub
|
||||
|
||||
|
||||
track_point_in_utc_time = _threaded_factory(async_track_point_in_utc_time)
|
||||
track_point_in_utc_time = threaded_listener_factory(
|
||||
async_track_point_in_utc_time)
|
||||
|
||||
|
||||
def async_track_sunrise(hass, action, offset=None):
|
||||
@@ -169,7 +170,7 @@ def async_track_sunrise(hass, action, offset=None):
|
||||
return remove_listener
|
||||
|
||||
|
||||
track_sunrise = _threaded_factory(async_track_sunrise)
|
||||
track_sunrise = threaded_listener_factory(async_track_sunrise)
|
||||
|
||||
|
||||
def async_track_sunset(hass, action, offset=None):
|
||||
@@ -205,7 +206,7 @@ def async_track_sunset(hass, action, offset=None):
|
||||
return remove_listener
|
||||
|
||||
|
||||
track_sunset = _threaded_factory(async_track_sunset)
|
||||
track_sunset = threaded_listener_factory(async_track_sunset)
|
||||
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
@@ -251,7 +252,7 @@ def async_track_utc_time_change(hass, action, year=None, month=None, day=None,
|
||||
pattern_time_change_listener)
|
||||
|
||||
|
||||
track_utc_time_change = _threaded_factory(async_track_utc_time_change)
|
||||
track_utc_time_change = threaded_listener_factory(async_track_utc_time_change)
|
||||
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
@@ -262,7 +263,7 @@ def async_track_time_change(hass, action, year=None, month=None, day=None,
|
||||
minute, second, local=True)
|
||||
|
||||
|
||||
track_time_change = _threaded_factory(async_track_time_change)
|
||||
track_time_change = threaded_listener_factory(async_track_time_change)
|
||||
|
||||
|
||||
def _process_state_match(parameter):
|
||||
|
||||
@@ -66,7 +66,7 @@ class Script():
|
||||
def async_run(self, variables: Optional[Sequence]=None) -> None:
|
||||
"""Run script.
|
||||
|
||||
Returns a coroutine.
|
||||
This method is a coroutine.
|
||||
"""
|
||||
if self._cur == -1:
|
||||
self._log('Running script')
|
||||
@@ -85,7 +85,7 @@ class Script():
|
||||
def script_delay(now):
|
||||
"""Called after delay is done."""
|
||||
self._async_unsub_delay_listener = None
|
||||
yield from self.async_run(variables)
|
||||
self.hass.loop.create_task(self.async_run(variables))
|
||||
|
||||
delay = action[CONF_DELAY]
|
||||
|
||||
@@ -100,7 +100,8 @@ class Script():
|
||||
self.hass, script_delay,
|
||||
date_util.utcnow() + delay)
|
||||
self._cur = cur + 1
|
||||
self._trigger_change_listener()
|
||||
if self._change_listener:
|
||||
self.hass.async_add_job(self._change_listener)
|
||||
return
|
||||
|
||||
elif CONF_CONDITION in action:
|
||||
@@ -115,7 +116,8 @@ class Script():
|
||||
|
||||
self._cur = -1
|
||||
self.last_action = None
|
||||
self._trigger_change_listener()
|
||||
if self._change_listener:
|
||||
self.hass.async_add_job(self._change_listener)
|
||||
|
||||
def stop(self) -> None:
|
||||
"""Stop running script."""
|
||||
@@ -128,11 +130,15 @@ class Script():
|
||||
|
||||
self._cur = -1
|
||||
self._async_remove_listener()
|
||||
self._trigger_change_listener()
|
||||
if self._change_listener:
|
||||
self.hass.async_add_job(self._change_listener)
|
||||
|
||||
@asyncio.coroutine
|
||||
def _async_call_service(self, action, variables):
|
||||
"""Call the service specified in the action."""
|
||||
"""Call the service specified in the action.
|
||||
|
||||
This method is a coroutine.
|
||||
"""
|
||||
self.last_action = action.get(CONF_ALIAS, 'call service')
|
||||
self._log("Executing step %s" % self.last_action)
|
||||
yield from service.async_call_from_config(
|
||||
@@ -165,10 +171,3 @@ class Script():
|
||||
msg = "Script {}: {}".format(self.name, msg)
|
||||
|
||||
_LOGGER.info(msg)
|
||||
|
||||
def _trigger_change_listener(self):
|
||||
"""Trigger the change listener."""
|
||||
if not self._change_listener:
|
||||
return
|
||||
|
||||
self.hass.async_add_job(self._change_listener)
|
||||
Reference in new issue
Block a user