Update triggers to use HassJob (#41450)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
pull/41476/head
J. Nick Koston 2020-10-08 02:44:34 -05:00 committed by GitHub
parent 7537a3a7c8
commit b51a160cce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 55 additions and 41 deletions

View File

@ -3,7 +3,7 @@ import voluptuous as vol
from homeassistant.components.geo_location import DOMAIN
from homeassistant.const import CONF_EVENT, CONF_PLATFORM, CONF_SOURCE, CONF_ZONE
from homeassistant.core import callback
from homeassistant.core import HassJob, callback
from homeassistant.helpers import condition, config_validation as cv
from homeassistant.helpers.config_validation import entity_domain
from homeassistant.helpers.event import TrackStates, async_track_state_change_filtered
@ -36,6 +36,7 @@ async def async_attach_trigger(hass, config, action, automation_info):
source = config.get(CONF_SOURCE).lower()
zone_entity_id = config.get(CONF_ZONE)
trigger_event = config.get(CONF_EVENT)
job = HassJob(action)
@callback
def state_change_listener(event):
@ -58,8 +59,8 @@ async def async_attach_trigger(hass, config, action, automation_info):
and from_match
and not to_match
):
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{
"trigger": {
"platform": "geo_location",

View File

@ -4,7 +4,7 @@ import logging
import voluptuous as vol
from homeassistant.const import CONF_PLATFORM
from homeassistant.core import callback
from homeassistant.core import HassJob, callback
from homeassistant.helpers import config_validation as cv
# mypy: allow-untyped-defs
@ -58,6 +58,8 @@ async def async_attach_trigger(
extra=vol.ALLOW_EXTRA,
)
job = HassJob(action)
@callback
def handle_event(event):
"""Listen for events and calls the action when data matches."""
@ -72,8 +74,8 @@ async def async_attach_trigger(
# If event doesn't match, skip event
return
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{
"trigger": {
"platform": platform_type,

View File

@ -4,7 +4,7 @@ import logging
import voluptuous as vol
from homeassistant.const import CONF_EVENT, CONF_PLATFORM, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import callback
from homeassistant.core import HassJob, callback
# mypy: allow-untyped-defs
@ -23,14 +23,15 @@ TRIGGER_SCHEMA = vol.Schema(
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for events based on configuration."""
event = config.get(CONF_EVENT)
job = HassJob(action)
if event == EVENT_SHUTDOWN:
@callback
def hass_shutdown(event):
"""Execute when Home Assistant is shutting down."""
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{
"trigger": {
"platform": "homeassistant",
@ -46,8 +47,8 @@ async def async_attach_trigger(hass, config, action, automation_info):
# Automation are enabled while hass is starting up, fire right away
# Check state because a config reload shouldn't trigger it.
if automation_info["home_assistant_start"]:
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{
"trigger": {
"platform": "homeassistant",

View File

@ -13,7 +13,7 @@ from homeassistant.const import (
CONF_PLATFORM,
CONF_VALUE_TEMPLATE,
)
from homeassistant.core import CALLBACK_TYPE, callback
from homeassistant.core import CALLBACK_TYPE, HassJob, callback
from homeassistant.helpers import condition, config_validation as cv, template
from homeassistant.helpers.event import (
async_track_same_state,
@ -73,6 +73,7 @@ async def async_attach_trigger(
entities_triggered = set()
period: dict = {}
attribute = config.get(CONF_ATTRIBUTE)
job = HassJob(action)
if value_template is not None:
value_template.hass = hass
@ -106,8 +107,8 @@ async def async_attach_trigger(
@callback
def call_action():
"""Call action with right context."""
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{
"trigger": {
"platform": platform_type,

View File

@ -7,7 +7,7 @@ import voluptuous as vol
from homeassistant import exceptions
from homeassistant.const import CONF_ATTRIBUTE, CONF_FOR, CONF_PLATFORM, MATCH_ALL
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, State, callback
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, State, callback
from homeassistant.helpers import config_validation as cv, template
from homeassistant.helpers.event import (
Event,
@ -83,6 +83,7 @@ async def async_attach_trigger(
match_from_state = process_state_match(from_state)
match_to_state = process_state_match(to_state)
attribute = config.get(CONF_ATTRIBUTE)
job = HassJob(action)
@callback
def state_automation_listener(event: Event):
@ -122,8 +123,8 @@ async def async_attach_trigger(
@callback
def call_action():
"""Call action with right context."""
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{
"trigger": {
"platform": platform_type,

View File

@ -6,7 +6,7 @@ import logging
import voluptuous as vol
from homeassistant.const import CONF_AT, CONF_PLATFORM
from homeassistant.core import callback
from homeassistant.core import HassJob, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.event import (
async_track_point_in_time,
@ -37,12 +37,13 @@ async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
entities = {}
removes = []
job = HassJob(action)
@callback
def time_automation_listener(description, now):
"""Listen for time changes and calls action."""
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{"trigger": {"platform": "time", "now": now, "description": description}},
)

View File

@ -4,7 +4,7 @@ import logging
import voluptuous as vol
from homeassistant.const import CONF_PLATFORM
from homeassistant.core import callback
from homeassistant.core import HassJob, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.event import async_track_time_change
@ -64,6 +64,7 @@ async def async_attach_trigger(hass, config, action, automation_info):
hours = config.get(CONF_HOURS)
minutes = config.get(CONF_MINUTES)
seconds = config.get(CONF_SECONDS)
job = HassJob(action)
# If larger units are specified, default the smaller units to zero
if minutes is None and hours is not None:
@ -74,8 +75,8 @@ async def async_attach_trigger(hass, config, action, automation_info):
@callback
def time_automation_listener(now):
"""Listen for time changes and calls action."""
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{
"trigger": {
"platform": "time_pattern",

View File

@ -4,7 +4,7 @@ import logging
import voluptuous as vol
from homeassistant.const import CONF_PLATFORM
from homeassistant.core import callback
from homeassistant.core import HassJob, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import track_point_in_utc_time
import homeassistant.util.dt as dt_util
@ -38,12 +38,13 @@ async def async_attach_trigger(hass, config, action, automation_info):
held_less_than = config.get(CONF_HELD_LESS_THAN)
pressed_time = None
cancel_pressed_more_than = None
job = HassJob(action)
@callback
def call_action():
"""Call action with right context."""
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{
"trigger": {
CONF_PLATFORM: "litejet",

View File

@ -5,7 +5,7 @@ import voluptuous as vol
from homeassistant.components import mqtt
from homeassistant.const import CONF_PAYLOAD, CONF_PLATFORM
from homeassistant.core import callback
from homeassistant.core import HassJob, callback
import homeassistant.helpers.config_validation as cv
# mypy: allow-untyped-defs
@ -35,6 +35,7 @@ async def async_attach_trigger(hass, config, action, automation_info):
payload = config.get(CONF_PAYLOAD)
encoding = config[CONF_ENCODING] or None
qos = config[CONF_QOS]
job = HassJob(action)
@callback
def mqtt_automation_listener(mqttmsg):
@ -53,7 +54,7 @@ async def async_attach_trigger(hass, config, action, automation_info):
except ValueError:
pass
hass.async_run_job(action, {"trigger": data})
hass.async_run_hass_job(job, {"trigger": data})
remove = await mqtt.async_subscribe(
hass, topic, mqtt_automation_listener, encoding=encoding, qos=qos

View File

@ -10,7 +10,7 @@ from homeassistant.const import (
CONF_PLATFORM,
SUN_EVENT_SUNRISE,
)
from homeassistant.core import callback
from homeassistant.core import HassJob, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import async_track_sunrise, async_track_sunset
@ -34,12 +34,13 @@ async def async_attach_trigger(hass, config, action, automation_info):
description = event
if offset:
description = f"{description} with offset"
job = HassJob(action)
@callback
def call_action():
"""Call action with right context."""
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{
"trigger": {
"platform": "sun",

View File

@ -5,7 +5,7 @@ import voluptuous as vol
from homeassistant import exceptions
from homeassistant.const import CONF_FOR, CONF_PLATFORM, CONF_VALUE_TEMPLATE
from homeassistant.core import callback
from homeassistant.core import HassJob, callback
from homeassistant.helpers import config_validation as cv, template
from homeassistant.helpers.event import (
TrackTemplate,
@ -36,6 +36,7 @@ async def async_attach_trigger(
time_delta = config.get(CONF_FOR)
template.attach(hass, time_delta)
delay_cancel = None
job = HassJob(action)
@callback
def template_listener(event, updates):
@ -58,8 +59,8 @@ async def async_attach_trigger(
@callback
def call_action(*_):
"""Call action with right context."""
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{
"trigger": {
"platform": "template",

View File

@ -6,7 +6,7 @@ from aiohttp import hdrs
import voluptuous as vol
from homeassistant.const import CONF_PLATFORM, CONF_WEBHOOK_ID
from homeassistant.core import callback
from homeassistant.core import HassJob, callback
import homeassistant.helpers.config_validation as cv
# mypy: allow-untyped-defs
@ -20,7 +20,7 @@ TRIGGER_SCHEMA = vol.Schema(
)
async def _handle_webhook(action, hass, webhook_id, request):
async def _handle_webhook(job, hass, webhook_id, request):
"""Handle incoming webhook."""
result = {"platform": "webhook", "webhook_id": webhook_id}
@ -31,17 +31,18 @@ async def _handle_webhook(action, hass, webhook_id, request):
result["query"] = request.query
result["description"] = "webhook"
hass.async_run_job(action, {"trigger": result})
hass.async_run_hass_job(job, {"trigger": result})
async def async_attach_trigger(hass, config, action, automation_info):
"""Trigger based on incoming webhooks."""
webhook_id = config.get(CONF_WEBHOOK_ID)
job = HassJob(action)
hass.components.webhook.async_register(
automation_info["domain"],
automation_info["name"],
webhook_id,
partial(_handle_webhook, action),
partial(_handle_webhook, job),
)
@callback

View File

@ -8,7 +8,7 @@ from homeassistant.const import (
CONF_PLATFORM,
CONF_ZONE,
)
from homeassistant.core import callback
from homeassistant.core import HassJob, callback
from homeassistant.helpers import condition, config_validation as cv, location
from homeassistant.helpers.event import async_track_state_change_event
@ -37,6 +37,7 @@ async def async_attach_trigger(hass, config, action, automation_info):
entity_id = config.get(CONF_ENTITY_ID)
zone_entity_id = config.get(CONF_ZONE)
event = config.get(CONF_EVENT)
job = HassJob(action)
@callback
def zone_automation_listener(zone_event):
@ -65,8 +66,8 @@ async def async_attach_trigger(hass, config, action, automation_info):
and not to_match
):
description = f"{entity} {_EVENT_DESCRIPTION[event]} {zone_state.attributes[ATTR_FRIENDLY_NAME]}"
hass.async_run_job(
action,
hass.async_run_hass_job(
job,
{
"trigger": {
"platform": "zone",