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:
Paulus Schoutsen 2016-10-03 22:39:27 -07:00 committed by GitHub
parent f2a12b7ac2
commit d58548dd1c
10 changed files with 123 additions and 76 deletions

View file

@ -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)