1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add async_safe annotation (#3688)

* Add async_safe annotation

* More async_run_job

* coroutine -> async_save

* Lint

* Rename async_safe -> callback

* Add tests to core for different job types

* Add one more test with different type of callbacks

* Fix typing signature for callback methods

* Fix callback service executed method

* Fix method signatures for callback
This commit is contained in:
Paulus Schoutsen
2016-10-04 20:44:32 -07:00
committed by GitHub
parent be7401f4a2
commit 5085cdb0f7
19 changed files with 231 additions and 87 deletions

View File

@@ -4,9 +4,9 @@ Offer state listening automation rules.
For more details about this automation rule, please refer to the documentation
at https://home-assistant.io/components/automation/#state-trigger
"""
import asyncio
import voluptuous as vol
from homeassistant.core import callback
import homeassistant.util.dt as dt_util
from homeassistant.const import MATCH_ALL, CONF_PLATFORM
from homeassistant.helpers.event import (
@@ -43,14 +43,14 @@ def async_trigger(hass, config, action):
async_remove_state_for_cancel = None
async_remove_state_for_listener = None
@asyncio.coroutine
@callback
def state_automation_listener(entity, from_s, to_s):
"""Listen for state changes and calls action."""
nonlocal async_remove_state_for_cancel, async_remove_state_for_listener
def call_action():
"""Call action with right context."""
hass.async_add_job(action, {
hass.async_run_job(action, {
'trigger': {
'platform': 'state',
'entity_id': entity,
@@ -64,13 +64,13 @@ def async_trigger(hass, config, action):
call_action()
return
@asyncio.coroutine
@callback
def state_for_listener(now):
"""Fire on state changes after a delay and calls action."""
async_remove_state_for_cancel()
call_action()
@asyncio.coroutine
@callback
def state_for_cancel_listener(entity, inner_from_s, inner_to_s):
"""Fire on changes and cancel for listener if changed."""
if inner_to_s.state == to_s.state: