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 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 (
@ -24,6 +20,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 from . import DOMAIN
@ -80,8 +77,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] == "jammed": if config[CONF_TYPE] == "jammed":
@ -104,5 +101,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

@ -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 ( from homeassistant.components.device_automation import (
DEVICE_TRIGGER_BASE_SCHEMA, DEVICE_TRIGGER_BASE_SCHEMA,
entity, entity,
@ -28,6 +24,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 .const import DOMAIN from .const import DOMAIN
@ -94,12 +91,12 @@ 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] not in TRIGGER_TYPES: 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": if config[CONF_TYPE] == "buffering":
to_state = STATE_BUFFERING to_state = STATE_BUFFERING
elif config[CONF_TYPE] == "idle": elif config[CONF_TYPE] == "idle":
@ -122,5 +119,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

@ -8,10 +8,6 @@ from typing import cast
import attr import attr
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.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -26,6 +22,7 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import debug_info, trigger as mqtt_trigger from . import debug_info, trigger as mqtt_trigger
@ -93,8 +90,8 @@ LOG_NAME = "Device trigger"
class TriggerInstance: class TriggerInstance:
"""Attached trigger settings.""" """Attached trigger settings."""
action: AutomationActionType = attr.ib() action: TriggerActionType = attr.ib()
automation_info: AutomationTriggerInfo = attr.ib() trigger_info: TriggerInfo = attr.ib()
trigger: Trigger = attr.ib() trigger: Trigger = attr.ib()
remove: CALLBACK_TYPE | None = attr.ib(default=None) remove: CALLBACK_TYPE | None = attr.ib(default=None)
@ -118,7 +115,7 @@ class TriggerInstance:
self.trigger.hass, self.trigger.hass,
mqtt_config, mqtt_config,
self.action, self.action,
self.automation_info, self.trigger_info,
) )
@ -138,10 +135,10 @@ class Trigger:
trigger_instances: list[TriggerInstance] = attr.ib(factory=list) trigger_instances: list[TriggerInstance] = attr.ib(factory=list)
async def add_trigger( async def add_trigger(
self, action: AutomationActionType, automation_info: AutomationTriggerInfo self, action: TriggerActionType, trigger_info: TriggerInfo
) -> Callable: ) -> Callable:
"""Add MQTT trigger.""" """Add MQTT trigger."""
instance = TriggerInstance(action, automation_info, self) instance = TriggerInstance(action, trigger_info, self)
self.trigger_instances.append(instance) self.trigger_instances.append(instance)
if self.topic is not None: if self.topic is not None:
@ -323,8 +320,8 @@ async def async_get_triggers(
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."""
hass.data.setdefault(DEVICE_TRIGGERS, {}) hass.data.setdefault(DEVICE_TRIGGERS, {})
@ -344,5 +341,5 @@ async def async_attach_trigger(
value_template=None, value_template=None,
) )
return await hass.data[DEVICE_TRIGGERS][discovery_id].add_trigger( 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 import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import CONF_PAYLOAD, CONF_PLATFORM, CONF_VALUE_TEMPLATE from homeassistant.const import CONF_PAYLOAD, 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
from homeassistant.helpers.json import json_loads from homeassistant.helpers.json import json_loads
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from .. import mqtt from .. import mqtt
@ -39,11 +36,11 @@ _LOGGER = logging.getLogger(__name__)
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."""
trigger_data = automation_info["trigger_data"] trigger_data = trigger_info["trigger_data"]
topic = config[CONF_TOPIC] topic = config[CONF_TOPIC]
wanted_payload = config.get(CONF_PAYLOAD) wanted_payload = config.get(CONF_PAYLOAD)
value_template = config.get(CONF_VALUE_TEMPLATE) value_template = config.get(CONF_VALUE_TEMPLATE)
@ -51,8 +48,8 @@ async def async_attach_trigger(
qos = config[CONF_QOS] qos = config[CONF_QOS]
job = HassJob(action) job = HassJob(action)
variables = None variables = None
if automation_info: if trigger_info:
variables = automation_info.get("variables") variables = trigger_info.get("variables")
template.attach(hass, wanted_payload) template.attach(hass, wanted_payload)
if wanted_payload: if wanted_payload: