Use TriggerActionType [core, l-m] (#76806)

This commit is contained in:
Marc Mueller 2022-08-15 17:39:55 +02:00 committed by GitHub
parent e39672078d
commit af002d9dc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 45 deletions

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 (
@ -24,6 +20,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
@ -80,8 +77,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] == "jammed":
@ -104,5 +101,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

@ -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,
entity,
@ -28,6 +24,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 .const import DOMAIN
@ -94,12 +91,12 @@ 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] not in TRIGGER_TYPES:
return await entity.async_attach_trigger(hass, config, action, automation_info)
return await entity.async_attach_trigger(hass, config, action, trigger_info)
if config[CONF_TYPE] == "buffering":
to_state = STATE_BUFFERING
elif config[CONF_TYPE] == "idle":
@ -122,5 +119,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

@ -8,10 +8,6 @@ from typing import cast
import attr
import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -26,6 +22,7 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
from . import debug_info, trigger as mqtt_trigger
@ -93,8 +90,8 @@ LOG_NAME = "Device trigger"
class TriggerInstance:
"""Attached trigger settings."""
action: AutomationActionType = attr.ib()
automation_info: AutomationTriggerInfo = attr.ib()
action: TriggerActionType = attr.ib()
trigger_info: TriggerInfo = attr.ib()
trigger: Trigger = attr.ib()
remove: CALLBACK_TYPE | None = attr.ib(default=None)
@ -118,7 +115,7 @@ class TriggerInstance:
self.trigger.hass,
mqtt_config,
self.action,
self.automation_info,
self.trigger_info,
)
@ -138,10 +135,10 @@ class Trigger:
trigger_instances: list[TriggerInstance] = attr.ib(factory=list)
async def add_trigger(
self, action: AutomationActionType, automation_info: AutomationTriggerInfo
self, action: TriggerActionType, trigger_info: TriggerInfo
) -> Callable:
"""Add MQTT trigger."""
instance = TriggerInstance(action, automation_info, self)
instance = TriggerInstance(action, trigger_info, self)
self.trigger_instances.append(instance)
if self.topic is not None:
@ -323,8 +320,8 @@ async def async_get_triggers(
async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
automation_info: AutomationTriggerInfo,
action: TriggerActionType,
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
"""Attach a trigger."""
hass.data.setdefault(DEVICE_TRIGGERS, {})
@ -344,5 +341,5 @@ async def async_attach_trigger(
value_template=None,
)
return await hass.data[DEVICE_TRIGGERS][discovery_id].add_trigger(
action, automation_info
action, trigger_info
)

View File

@ -4,14 +4,11 @@ import logging
import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import CONF_PAYLOAD, CONF_PLATFORM, CONF_VALUE_TEMPLATE
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
from homeassistant.helpers import config_validation as cv, template
from homeassistant.helpers.json import json_loads
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType
from .. import mqtt
@ -39,11 +36,11 @@ _LOGGER = logging.getLogger(__name__)
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"]
topic = config[CONF_TOPIC]
wanted_payload = config.get(CONF_PAYLOAD)
value_template = config.get(CONF_VALUE_TEMPLATE)
@ -51,8 +48,8 @@ async def async_attach_trigger(
qos = config[CONF_QOS]
job = HassJob(action)
variables = None
if automation_info:
variables = automation_info.get("variables")
if trigger_info:
variables = trigger_info.get("variables")
template.attach(hass, wanted_payload)
if wanted_payload: