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

View File

@ -3,13 +3,10 @@ from __future__ import annotations
import voluptuous as vol import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.components.device_automation import toggle_entity from homeassistant.components.device_automation import toggle_entity
from homeassistant.const import CONF_DOMAIN from homeassistant.const import CONF_DOMAIN
from homeassistant.core import CALLBACK_TYPE, HomeAssistant from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import DOMAIN from . import DOMAIN
@ -23,13 +20,11 @@ TRIGGER_SCHEMA = vol.All(
async def async_attach_trigger( async def async_attach_trigger(
hass: HomeAssistant, hass: HomeAssistant,
config: ConfigType, config: ConfigType,
action: AutomationActionType, action: TriggerActionType,
automation_info: AutomationTriggerInfo, trigger_info: TriggerInfo,
) -> CALLBACK_TYPE: ) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration.""" """Listen for state changes based on configuration."""
return await toggle_entity.async_attach_trigger( return await toggle_entity.async_attach_trigger(hass, config, action, trigger_info)
hass, config, action, automation_info
)
async def async_get_triggers( async def async_get_triggers(

View File

@ -3,10 +3,6 @@ from __future__ import annotations
import voluptuous as vol import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
from homeassistant.components.homeassistant.triggers import state as state_trigger from homeassistant.components.homeassistant.triggers import state as state_trigger
from homeassistant.const import ( from homeassistant.const import (
@ -19,6 +15,7 @@ from homeassistant.const import (
) )
from homeassistant.core import CALLBACK_TYPE, HomeAssistant from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_registry from homeassistant.helpers import config_validation as cv, entity_registry
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import DOMAIN, STATE_CLEANING, STATE_DOCKED from . import DOMAIN, STATE_CLEANING, STATE_DOCKED
@ -74,8 +71,8 @@ async def async_get_trigger_capabilities(
async def async_attach_trigger( async def async_attach_trigger(
hass: HomeAssistant, hass: HomeAssistant,
config: ConfigType, config: ConfigType,
action: AutomationActionType, action: TriggerActionType,
automation_info: AutomationTriggerInfo, trigger_info: TriggerInfo,
) -> CALLBACK_TYPE: ) -> CALLBACK_TYPE:
"""Attach a trigger.""" """Attach a trigger."""
if config[CONF_TYPE] == "cleaning": if config[CONF_TYPE] == "cleaning":
@ -92,5 +89,5 @@ async def async_attach_trigger(
state_config[CONF_FOR] = config[CONF_FOR] state_config[CONF_FOR] = config[CONF_FOR]
state_config = await state_trigger.async_validate_trigger_config(hass, state_config) state_config = await state_trigger.async_validate_trigger_config(hass, state_config)
return await state_trigger.async_attach_trigger( 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 from aiohttp import hdrs
import voluptuous as vol import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import CONF_PLATFORM, CONF_WEBHOOK_ID from homeassistant.const import CONF_PLATFORM, CONF_WEBHOOK_ID
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import DOMAIN, async_register, async_unregister from . import DOMAIN, async_register, async_unregister
@ -35,7 +32,7 @@ WEBHOOK_TRIGGERS = f"{DOMAIN}_triggers"
class TriggerInstance: class TriggerInstance:
"""Attached trigger settings.""" """Attached trigger settings."""
automation_info: AutomationTriggerInfo trigger_info: TriggerInfo
job: HassJob job: HassJob
@ -55,15 +52,15 @@ async def _handle_webhook(hass, webhook_id, request):
WEBHOOK_TRIGGERS, {} WEBHOOK_TRIGGERS, {}
) )
for trigger in triggers[webhook_id]: 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}) hass.async_run_hass_job(trigger.job, {"trigger": result})
async def async_attach_trigger( async def async_attach_trigger(
hass: HomeAssistant, hass: HomeAssistant,
config: ConfigType, config: ConfigType,
action: AutomationActionType, action: TriggerActionType,
automation_info: AutomationTriggerInfo, trigger_info: TriggerInfo,
) -> CALLBACK_TYPE: ) -> CALLBACK_TYPE:
"""Trigger based on incoming webhooks.""" """Trigger based on incoming webhooks."""
webhook_id: str = config[CONF_WEBHOOK_ID] webhook_id: str = config[CONF_WEBHOOK_ID]
@ -76,14 +73,14 @@ async def async_attach_trigger(
if webhook_id not in triggers: if webhook_id not in triggers:
async_register( async_register(
hass, hass,
automation_info["domain"], trigger_info["domain"],
automation_info["name"], trigger_info["name"],
webhook_id, webhook_id,
_handle_webhook, _handle_webhook,
) )
triggers[webhook_id] = [] triggers[webhook_id] = []
trigger_instance = TriggerInstance(automation_info, job) trigger_instance = TriggerInstance(trigger_info, job)
triggers[webhook_id].append(trigger_instance) triggers[webhook_id].append(trigger_instance)
@callback @callback

View File

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