diff --git a/homeassistant/components/geo_location/trigger.py b/homeassistant/components/geo_location/trigger.py index 415f5f48de5..aad281da117 100644 --- a/homeassistant/components/geo_location/trigger.py +++ b/homeassistant/components/geo_location/trigger.py @@ -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", diff --git a/homeassistant/components/homeassistant/triggers/event.py b/homeassistant/components/homeassistant/triggers/event.py index afd2b40e842..1bd6a37cd1c 100644 --- a/homeassistant/components/homeassistant/triggers/event.py +++ b/homeassistant/components/homeassistant/triggers/event.py @@ -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, diff --git a/homeassistant/components/homeassistant/triggers/homeassistant.py b/homeassistant/components/homeassistant/triggers/homeassistant.py index baca145e19c..0b4fd8a08c3 100644 --- a/homeassistant/components/homeassistant/triggers/homeassistant.py +++ b/homeassistant/components/homeassistant/triggers/homeassistant.py @@ -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", diff --git a/homeassistant/components/homeassistant/triggers/numeric_state.py b/homeassistant/components/homeassistant/triggers/numeric_state.py index b5a21aa8404..a6e3b33ae97 100644 --- a/homeassistant/components/homeassistant/triggers/numeric_state.py +++ b/homeassistant/components/homeassistant/triggers/numeric_state.py @@ -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, diff --git a/homeassistant/components/homeassistant/triggers/state.py b/homeassistant/components/homeassistant/triggers/state.py index 915856951d2..5dd7335f56e 100644 --- a/homeassistant/components/homeassistant/triggers/state.py +++ b/homeassistant/components/homeassistant/triggers/state.py @@ -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, diff --git a/homeassistant/components/homeassistant/triggers/time.py b/homeassistant/components/homeassistant/triggers/time.py index a152b1e8571..baa5cc59a50 100644 --- a/homeassistant/components/homeassistant/triggers/time.py +++ b/homeassistant/components/homeassistant/triggers/time.py @@ -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}}, ) diff --git a/homeassistant/components/homeassistant/triggers/time_pattern.py b/homeassistant/components/homeassistant/triggers/time_pattern.py index 5f03fb593d6..b79c0074360 100644 --- a/homeassistant/components/homeassistant/triggers/time_pattern.py +++ b/homeassistant/components/homeassistant/triggers/time_pattern.py @@ -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", diff --git a/homeassistant/components/litejet/trigger.py b/homeassistant/components/litejet/trigger.py index d8b8ede8339..9fe3089c356 100644 --- a/homeassistant/components/litejet/trigger.py +++ b/homeassistant/components/litejet/trigger.py @@ -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", diff --git a/homeassistant/components/mqtt/trigger.py b/homeassistant/components/mqtt/trigger.py index 1ba7905120f..58ec51c4b1b 100644 --- a/homeassistant/components/mqtt/trigger.py +++ b/homeassistant/components/mqtt/trigger.py @@ -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 diff --git a/homeassistant/components/sun/trigger.py b/homeassistant/components/sun/trigger.py index c21726cf0ae..756a73fc2b0 100644 --- a/homeassistant/components/sun/trigger.py +++ b/homeassistant/components/sun/trigger.py @@ -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", diff --git a/homeassistant/components/template/trigger.py b/homeassistant/components/template/trigger.py index 5dcee0a7347..5d748edb841 100644 --- a/homeassistant/components/template/trigger.py +++ b/homeassistant/components/template/trigger.py @@ -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", diff --git a/homeassistant/components/webhook/trigger.py b/homeassistant/components/webhook/trigger.py index cc03f74922f..0cdc7f103f5 100644 --- a/homeassistant/components/webhook/trigger.py +++ b/homeassistant/components/webhook/trigger.py @@ -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 diff --git a/homeassistant/components/zone/trigger.py b/homeassistant/components/zone/trigger.py index 1f0856513dd..9958d201810 100644 --- a/homeassistant/components/zone/trigger.py +++ b/homeassistant/components/zone/trigger.py @@ -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",