Use async_track_state_change_event for automation numeric_state (#37255)

Calling async_track_state_change_event directly
is faster than async_track_state_change (see #37251) and has
slightly lower latency triggering state updates
This commit is contained in:
J. Nick Koston 2020-07-05 15:05:53 -05:00 committed by GitHub
parent 404b1f40d6
commit c1aaceebb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,7 +14,10 @@ from homeassistant.const import (
) )
from homeassistant.core import CALLBACK_TYPE, callback from homeassistant.core import CALLBACK_TYPE, callback
from homeassistant.helpers import condition, config_validation as cv, template from homeassistant.helpers import condition, config_validation as cv, template
from homeassistant.helpers.event import async_track_same_state, async_track_state_change from homeassistant.helpers.event import (
async_track_same_state,
async_track_state_change_event,
)
# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs # mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs
# mypy: no-check-untyped-defs # mypy: no-check-untyped-defs
@ -94,8 +97,11 @@ async def async_attach_trigger(
) )
@callback @callback
def state_automation_listener(entity, from_s, to_s): def state_automation_listener(event):
"""Listen for state changes and calls action.""" """Listen for state changes and calls action."""
entity = event.data.get("entity_id")
from_s = event.data.get("old_state")
to_s = event.data.get("new_state")
@callback @callback
def call_action(): def call_action():
@ -168,7 +174,7 @@ async def async_attach_trigger(
else: else:
call_action() call_action()
unsub = async_track_state_change(hass, entity_id, state_automation_listener) unsub = async_track_state_change_event(hass, entity_id, state_automation_listener)
@callback @callback
def async_remove(): def async_remove():