Use TriggerActionType [core, t-z] (#76808)

This commit is contained in:
Marc Mueller 2022-08-15 18:15:20 +02:00 committed by GitHub
parent 7360cce1c2
commit 453cbc3e14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 47 deletions

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_FOR, CONF_PLATFORM, CONF_VALUE_TEMPLATE
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
from homeassistant.helpers import config_validation as cv, template
@ -17,6 +13,7 @@ from homeassistant.helpers.event import (
async_track_template_result,
)
from homeassistant.helpers.template import Template, result_as_boolean
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
# mypy: allow-untyped-defs, no-check-untyped-defs
@ -35,13 +32,13 @@ TRIGGER_SCHEMA = IF_ACTION_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,
*,
platform_type: str = "template",
) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration."""
trigger_data = automation_info["trigger_data"]
trigger_data = trigger_info["trigger_data"]
value_template: Template = config[CONF_VALUE_TEMPLATE]
value_template.hass = hass
time_delta = config.get(CONF_FOR)
@ -53,13 +50,13 @@ async def async_attach_trigger(
# Arm at setup if the template is already false.
try:
if not result_as_boolean(
value_template.async_render(automation_info["variables"])
value_template.async_render(trigger_info["variables"])
):
armed = True
except exceptions.TemplateError as ex:
_LOGGER.warning(
"Error initializing 'template' trigger for '%s': %s",
automation_info["name"],
trigger_info["name"],
ex,
)
@ -72,7 +69,7 @@ async def async_attach_trigger(
if isinstance(result, exceptions.TemplateError):
_LOGGER.warning(
"Error evaluating 'template' trigger for '%s': %s",
automation_info["name"],
trigger_info["name"],
result,
)
return
@ -134,7 +131,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
@ -144,7 +141,7 @@ async def async_attach_trigger(
info = async_track_template_result(
hass,
[TrackTemplate(value_template, automation_info["variables"])],
[TrackTemplate(value_template, trigger_info["variables"])],
template_listener,
)
unsub = info.async_remove

View File

@ -3,13 +3,10 @@ from __future__ import annotations
import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.components.device_automation import toggle_entity
from homeassistant.const import CONF_DOMAIN
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
from . import DOMAIN
@ -23,13 +20,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."""
return await toggle_entity.async_attach_trigger(
hass, config, action, automation_info
)
return await toggle_entity.async_attach_trigger(hass, config, action, trigger_info)
async def async_get_triggers(

View File

@ -3,10 +3,6 @@ from __future__ import annotations
import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
from homeassistant.components.homeassistant.triggers import state as state_trigger
from homeassistant.const import (
@ -19,6 +15,7 @@ from homeassistant.const import (
)
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_registry
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
from . import DOMAIN, STATE_CLEANING, STATE_DOCKED
@ -74,8 +71,8 @@ async def async_get_trigger_capabilities(
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
automation_info: AutomationTriggerInfo,
action: TriggerActionType,
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
"""Attach a trigger."""
if config[CONF_TYPE] == "cleaning":
@ -92,5 +89,5 @@ async def async_attach_trigger(
state_config[CONF_FOR] = config[CONF_FOR]
state_config = await state_trigger.async_validate_trigger_config(hass, state_config)
return await state_trigger.async_attach_trigger(
hass, state_config, action, automation_info, platform_type="device"
hass, state_config, action, trigger_info, platform_type="device"
)

View File

@ -6,13 +6,10 @@ from dataclasses import dataclass
from aiohttp import hdrs
import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import CONF_PLATFORM, CONF_WEBHOOK_ID
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
from . import DOMAIN, async_register, async_unregister
@ -35,7 +32,7 @@ WEBHOOK_TRIGGERS = f"{DOMAIN}_triggers"
class TriggerInstance:
"""Attached trigger settings."""
automation_info: AutomationTriggerInfo
trigger_info: TriggerInfo
job: HassJob
@ -55,15 +52,15 @@ async def _handle_webhook(hass, webhook_id, request):
WEBHOOK_TRIGGERS, {}
)
for trigger in triggers[webhook_id]:
result = {**base_result, **trigger.automation_info["trigger_data"]}
result = {**base_result, **trigger.trigger_info["trigger_data"]}
hass.async_run_hass_job(trigger.job, {"trigger": result})
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
automation_info: AutomationTriggerInfo,
action: TriggerActionType,
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
"""Trigger based on incoming webhooks."""
webhook_id: str = config[CONF_WEBHOOK_ID]
@ -76,14 +73,14 @@ async def async_attach_trigger(
if webhook_id not in triggers:
async_register(
hass,
automation_info["domain"],
automation_info["name"],
trigger_info["domain"],
trigger_info["name"],
webhook_id,
_handle_webhook,
)
triggers[webhook_id] = []
trigger_instance = TriggerInstance(automation_info, job)
trigger_instance = TriggerInstance(trigger_info, job)
triggers[webhook_id].append(trigger_instance)
@callback

View File

@ -3,10 +3,6 @@ import logging
import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import (
ATTR_FRIENDLY_NAME,
CONF_ENTITY_ID,
@ -22,6 +18,7 @@ from homeassistant.helpers import (
location,
)
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
# mypy: allow-incomplete-defs, allow-untyped-defs
@ -62,13 +59,13 @@ 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 = "zone",
) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration."""
trigger_data = automation_info["trigger_data"]
trigger_data = trigger_info["trigger_data"]
entity_id: list[str] = config[CONF_ENTITY_ID]
zone_entity_id = config.get(CONF_ZONE)
event = config.get(CONF_EVENT)
@ -91,7 +88,7 @@ async def async_attach_trigger(
if not (zone_state := hass.states.get(zone_entity_id)):
_LOGGER.warning(
"Automation '%s' is referencing non-existing zone '%s' in a zone trigger",
automation_info["name"],
trigger_info["name"],
zone_entity_id,
)
return