Use TriggerActionType [core, homeassistant] (#76805)

This commit is contained in:
Marc Mueller 2022-08-15 18:06:16 +02:00 committed by GitHub
parent 19cf6089d6
commit 7360cce1c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 59 deletions

View File

@ -1,15 +1,12 @@
"""Home Assistant trigger dispatcher."""
import importlib
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.components.device_automation.trigger import (
DeviceAutomationTriggerProtocol,
)
from homeassistant.const import CONF_PLATFORM
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
@ -31,9 +28,9 @@ async def async_validate_trigger_config(
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
automation_info: AutomationTriggerInfo,
action: TriggerActionType,
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
"""Attach trigger of specified platform."""
platform = _get_trigger_platform(config)
return await platform.async_attach_trigger(hass, config, action, automation_info)
return await platform.async_attach_trigger(hass, config, action, trigger_info)

View File

@ -5,13 +5,10 @@ from typing import Any
import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import CONF_EVENT_DATA, CONF_PLATFORM
from homeassistant.core import CALLBACK_TYPE, Event, HassJob, HomeAssistant, callback
from homeassistant.helpers import config_validation as cv, template
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
CONF_EVENT_TYPE = "event_type"
@ -37,14 +34,14 @@ def _schema_value(value: Any) -> Any:
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
automation_info: AutomationTriggerInfo,
action: TriggerActionType,
trigger_info: TriggerInfo,
*,
platform_type: str = "event",
) -> CALLBACK_TYPE:
"""Listen for events based on configuration."""
trigger_data = automation_info["trigger_data"]
variables = automation_info["variables"]
trigger_data = trigger_info["trigger_data"]
variables = trigger_info["variables"]
template.attach(hass, config[CONF_EVENT_TYPE])
event_types = template.render_complex(

View File

@ -1,13 +1,10 @@
"""Offer Home Assistant core automation rules."""
import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import CONF_EVENT, CONF_PLATFORM, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
# mypy: allow-untyped-defs
@ -26,11 +23,11 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
automation_info: AutomationTriggerInfo,
action: TriggerActionType,
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
"""Listen for events based on configuration."""
trigger_data = automation_info["trigger_data"]
trigger_data = trigger_info["trigger_data"]
event = config.get(CONF_EVENT)
job = HassJob(action)
@ -56,7 +53,7 @@ async def async_attach_trigger(
# 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"]:
if trigger_info["home_assistant_start"]:
hass.async_run_hass_job(
job,
{

View File

@ -4,10 +4,6 @@ import logging
import voluptuous as vol
from homeassistant import exceptions
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import (
CONF_ABOVE,
CONF_ATTRIBUTE,
@ -28,6 +24,7 @@ from homeassistant.helpers.event import (
async_track_same_state,
async_track_state_change_event,
)
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs
@ -87,8 +84,8 @@ async def async_validate_trigger_config(
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
automation_info: AutomationTriggerInfo,
action: TriggerActionType,
trigger_info: TriggerInfo,
*,
platform_type: str = "numeric_state",
) -> CALLBACK_TYPE:
@ -105,8 +102,8 @@ async def async_attach_trigger(
attribute = config.get(CONF_ATTRIBUTE)
job = HassJob(action)
trigger_data = automation_info["trigger_data"]
_variables = automation_info["variables"] or {}
trigger_data = trigger_info["trigger_data"]
_variables = trigger_info["variables"] or {}
if value_template is not None:
value_template.hass = hass
@ -139,7 +136,7 @@ async def async_attach_trigger(
except exceptions.ConditionError as ex:
_LOGGER.warning(
"Error initializing '%s' trigger: %s",
automation_info["name"],
trigger_info["name"],
ex,
)
@ -185,7 +182,7 @@ async def async_attach_trigger(
try:
matching = check_numeric_state(entity_id, from_s, to_s)
except exceptions.ConditionError as ex:
_LOGGER.warning("Error in '%s' trigger: %s", automation_info["name"], ex)
_LOGGER.warning("Error in '%s' trigger: %s", trigger_info["name"], ex)
return
if not matching:
@ -201,7 +198,7 @@ async def async_attach_trigger(
except (exceptions.TemplateError, vol.Invalid) as ex:
_LOGGER.error(
"Error rendering '%s' for template: %s",
automation_info["name"],
trigger_info["name"],
ex,
)
return

View File

@ -7,10 +7,6 @@ import logging
import voluptuous as vol
from homeassistant import exceptions
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import CONF_ATTRIBUTE, CONF_FOR, CONF_PLATFORM, MATCH_ALL
from homeassistant.core import (
CALLBACK_TYPE,
@ -30,6 +26,7 @@ from homeassistant.helpers.event import (
async_track_state_change_event,
process_state_match,
)
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs
@ -97,8 +94,8 @@ async def async_validate_trigger_config(
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
automation_info: AutomationTriggerInfo,
action: TriggerActionType,
trigger_info: TriggerInfo,
*,
platform_type: str = "state",
) -> CALLBACK_TYPE:
@ -131,8 +128,8 @@ async def async_attach_trigger(
attribute = config.get(CONF_ATTRIBUTE)
job = HassJob(action)
trigger_data = automation_info["trigger_data"]
_variables = automation_info["variables"] or {}
trigger_data = trigger_info["trigger_data"]
_variables = trigger_info["variables"] or {}
@callback
def state_automation_listener(event: Event):
@ -193,7 +190,7 @@ async def async_attach_trigger(
call_action()
return
trigger_info = {
data = {
"trigger": {
"platform": "state",
"entity_id": entity,
@ -201,7 +198,7 @@ async def async_attach_trigger(
"to_state": to_s,
}
}
variables = {**_variables, **trigger_info}
variables = {**_variables, **data}
try:
period[entity] = cv.positive_time_period(
@ -209,7 +206,7 @@ async def async_attach_trigger(
)
except (exceptions.TemplateError, vol.Invalid) as ex:
_LOGGER.error(
"Error rendering '%s' for template: %s", automation_info["name"], ex
"Error rendering '%s' for template: %s", trigger_info["name"], ex
)
return

View File

@ -5,10 +5,6 @@ from functools import partial
import voluptuous as vol
from homeassistant.components import sensor
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import (
ATTR_DEVICE_CLASS,
CONF_AT,
@ -23,6 +19,7 @@ from homeassistant.helpers.event import (
async_track_state_change_event,
async_track_time_change,
)
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
import homeassistant.util.dt as dt_util
@ -45,11 +42,11 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
automation_info: AutomationTriggerInfo,
action: TriggerActionType,
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration."""
trigger_data = automation_info["trigger_data"]
trigger_data = trigger_info["trigger_data"]
entities: dict[str, CALLBACK_TYPE] = {}
removes = []
job = HassJob(action)

View File

@ -1,14 +1,11 @@
"""Offer time listening automation rules."""
import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import CONF_PLATFORM
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.event import async_track_time_change
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
# mypy: allow-untyped-defs, no-check-untyped-defs
@ -63,11 +60,11 @@ TRIGGER_SCHEMA = vol.All(
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
automation_info: AutomationTriggerInfo,
action: TriggerActionType,
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration."""
trigger_data = automation_info["trigger_data"]
trigger_data = trigger_info["trigger_data"]
hours = config.get(CONF_HOURS)
minutes = config.get(CONF_MINUTES)
seconds = config.get(CONF_SECONDS)