Use TriggerActionType [core, d-h] (#76804)

This commit is contained in:
Marc Mueller 2022-08-15 17:39:14 +02:00 committed by GitHub
parent 1557a7c36d
commit e39672078d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 51 deletions

View File

@ -3,14 +3,11 @@ from __future__ import annotations
import voluptuous as vol import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.components.homeassistant.triggers import state as state_trigger from homeassistant.components.homeassistant.triggers import state as state_trigger
from homeassistant.const import CONF_ENTITY_ID, CONF_FOR, CONF_PLATFORM, CONF_TYPE from homeassistant.const import CONF_ENTITY_ID, CONF_FOR, CONF_PLATFORM, CONF_TYPE
from homeassistant.core import CALLBACK_TYPE, HomeAssistant from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_registry as er from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from . import DEVICE_TRIGGER_BASE_SCHEMA from . import DEVICE_TRIGGER_BASE_SCHEMA
@ -38,8 +35,8 @@ TRIGGER_SCHEMA = DEVICE_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,
) -> CALLBACK_TYPE: ) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration.""" """Listen for state changes based on configuration."""
to_state = None to_state = None
@ -53,7 +50,7 @@ async def async_attach_trigger(
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.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 (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
@ -23,6 +19,7 @@ from homeassistant.helpers import (
config_validation as cv, config_validation as cv,
entity_registry as er, entity_registry as er,
) )
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
from homeassistant.helpers.typing import ConfigType, TemplateVarsType from homeassistant.helpers.typing import ConfigType, TemplateVarsType
from . import DEVICE_TRIGGER_BASE_SCHEMA, entity from . import DEVICE_TRIGGER_BASE_SCHEMA, entity
@ -155,12 +152,12 @@ def async_condition_from_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,
) -> CALLBACK_TYPE: ) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration.""" """Listen for state changes based on configuration."""
if config[CONF_TYPE] not in [CONF_TURNED_ON, CONF_TURNED_OFF]: if config[CONF_TYPE] not in [CONF_TURNED_ON, CONF_TURNED_OFF]:
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] == CONF_TURNED_ON: if config[CONF_TYPE] == CONF_TURNED_ON:
to_state = "on" to_state = "on"
@ -176,7 +173,7 @@ async def async_attach_trigger(
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

@ -5,12 +5,9 @@ from typing import Any, Protocol, cast
import voluptuous as vol import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
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 ( from . import (
@ -40,8 +37,8 @@ class DeviceAutomationTriggerProtocol(Protocol):
self, self,
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."""
@ -74,11 +71,11 @@ 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,
) -> CALLBACK_TYPE: ) -> CALLBACK_TYPE:
"""Listen for trigger.""" """Listen for trigger."""
platform = await async_get_device_automation_platform( platform = await async_get_device_automation_platform(
hass, config[CONF_DOMAIN], DeviceAutomationType.TRIGGER hass, config[CONF_DOMAIN], DeviceAutomationType.TRIGGER
) )
return await platform.async_attach_trigger(hass, config, action, automation_info) return await platform.async_attach_trigger(hass, config, action, trigger_info)

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
@ -37,10 +34,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:
"""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
)

View File

@ -3,15 +3,12 @@ import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.components.automation import (
AutomationActionType,
AutomationTriggerInfo,
)
from homeassistant.const import CONF_EVENT, CONF_PLATFORM, CONF_SOURCE, CONF_ZONE from homeassistant.const import CONF_EVENT, CONF_PLATFORM, CONF_SOURCE, CONF_ZONE
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
from homeassistant.helpers import condition, config_validation as cv from homeassistant.helpers import condition, config_validation as cv
from homeassistant.helpers.config_validation import entity_domain from homeassistant.helpers.config_validation import entity_domain
from homeassistant.helpers.event import TrackStates, async_track_state_change_filtered from homeassistant.helpers.event import TrackStates, async_track_state_change_filtered
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
@ -44,11 +41,11 @@ def source_match(state, source):
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"]
source: str = config[CONF_SOURCE].lower() source: str = config[CONF_SOURCE].lower()
zone_entity_id = config.get(CONF_ZONE) zone_entity_id = config.get(CONF_ZONE)
trigger_event = config.get(CONF_EVENT) trigger_event = config.get(CONF_EVENT)
@ -66,7 +63,7 @@ async def async_attach_trigger(
if (zone_state := hass.states.get(zone_entity_id)) is None: if (zone_state := hass.states.get(zone_entity_id)) is None:
_LOGGER.warning( _LOGGER.warning(
"Unable to execute automation %s: Zone %s not found", "Unable to execute automation %s: Zone %s not found",
automation_info["name"], trigger_info["name"],
zone_entity_id, zone_entity_id,
) )
return return

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,
toggle_entity, toggle_entity,
@ -27,6 +23,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
@ -82,8 +79,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."""
if config[CONF_TYPE] == "target_humidity_changed": if config[CONF_TYPE] == "target_humidity_changed":
@ -106,12 +103,10 @@ async def async_attach_trigger(
) )
) )
return await numeric_state_trigger.async_attach_trigger( return await numeric_state_trigger.async_attach_trigger(
hass, numeric_state_config, action, automation_info, platform_type="device" hass, numeric_state_config, action, trigger_info, platform_type="device"
) )
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_trigger_capabilities( async def async_get_trigger_capabilities(