mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Use TriggerActionType [core, homeassistant] (#76805)
This commit is contained in:
parent
19cf6089d6
commit
7360cce1c2
@ -1,15 +1,12 @@
|
|||||||
"""Home Assistant trigger dispatcher."""
|
"""Home Assistant trigger dispatcher."""
|
||||||
import importlib
|
import importlib
|
||||||
|
|
||||||
from homeassistant.components.automation import (
|
|
||||||
AutomationActionType,
|
|
||||||
AutomationTriggerInfo,
|
|
||||||
)
|
|
||||||
from homeassistant.components.device_automation.trigger import (
|
from homeassistant.components.device_automation.trigger import (
|
||||||
DeviceAutomationTriggerProtocol,
|
DeviceAutomationTriggerProtocol,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_PLATFORM
|
from homeassistant.const import CONF_PLATFORM
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
@ -31,9 +28,9 @@ 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:
|
||||||
"""Attach trigger of specified platform."""
|
"""Attach trigger of specified platform."""
|
||||||
platform = _get_trigger_platform(config)
|
platform = _get_trigger_platform(config)
|
||||||
return await platform.async_attach_trigger(hass, config, action, automation_info)
|
return await platform.async_attach_trigger(hass, config, action, trigger_info)
|
||||||
|
@ -5,13 +5,10 @@ from typing import Any
|
|||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.automation import (
|
|
||||||
AutomationActionType,
|
|
||||||
AutomationTriggerInfo,
|
|
||||||
)
|
|
||||||
from homeassistant.const import CONF_EVENT_DATA, CONF_PLATFORM
|
from homeassistant.const import CONF_EVENT_DATA, CONF_PLATFORM
|
||||||
from homeassistant.core import CALLBACK_TYPE, Event, HassJob, HomeAssistant, callback
|
from homeassistant.core import CALLBACK_TYPE, Event, HassJob, HomeAssistant, callback
|
||||||
from homeassistant.helpers import config_validation as cv, template
|
from homeassistant.helpers import config_validation as cv, template
|
||||||
|
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
CONF_EVENT_TYPE = "event_type"
|
CONF_EVENT_TYPE = "event_type"
|
||||||
@ -37,14 +34,14 @@ def _schema_value(value: Any) -> Any:
|
|||||||
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 = "event",
|
platform_type: str = "event",
|
||||||
) -> CALLBACK_TYPE:
|
) -> CALLBACK_TYPE:
|
||||||
"""Listen for events based on configuration."""
|
"""Listen for events based on configuration."""
|
||||||
trigger_data = automation_info["trigger_data"]
|
trigger_data = trigger_info["trigger_data"]
|
||||||
variables = automation_info["variables"]
|
variables = trigger_info["variables"]
|
||||||
|
|
||||||
template.attach(hass, config[CONF_EVENT_TYPE])
|
template.attach(hass, config[CONF_EVENT_TYPE])
|
||||||
event_types = template.render_complex(
|
event_types = template.render_complex(
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
"""Offer Home Assistant core automation rules."""
|
"""Offer Home Assistant core automation rules."""
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.automation import (
|
|
||||||
AutomationActionType,
|
|
||||||
AutomationTriggerInfo,
|
|
||||||
)
|
|
||||||
from homeassistant.const import CONF_EVENT, CONF_PLATFORM, EVENT_HOMEASSISTANT_STOP
|
from homeassistant.const import CONF_EVENT, CONF_PLATFORM, EVENT_HOMEASSISTANT_STOP
|
||||||
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
|
from homeassistant.helpers import config_validation as cv
|
||||||
|
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
# mypy: allow-untyped-defs
|
# mypy: allow-untyped-defs
|
||||||
@ -26,11 +23,11 @@ TRIGGER_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,
|
||||||
) -> CALLBACK_TYPE:
|
) -> CALLBACK_TYPE:
|
||||||
"""Listen for events based on configuration."""
|
"""Listen for events based on configuration."""
|
||||||
trigger_data = automation_info["trigger_data"]
|
trigger_data = trigger_info["trigger_data"]
|
||||||
event = config.get(CONF_EVENT)
|
event = config.get(CONF_EVENT)
|
||||||
job = HassJob(action)
|
job = HassJob(action)
|
||||||
|
|
||||||
@ -56,7 +53,7 @@ async def async_attach_trigger(
|
|||||||
|
|
||||||
# Automation are enabled while hass is starting up, fire right away
|
# Automation are enabled while hass is starting up, fire right away
|
||||||
# Check state because a config reload shouldn't trigger it.
|
# Check state because a config reload shouldn't trigger it.
|
||||||
if automation_info["home_assistant_start"]:
|
if trigger_info["home_assistant_start"]:
|
||||||
hass.async_run_hass_job(
|
hass.async_run_hass_job(
|
||||||
job,
|
job,
|
||||||
{
|
{
|
||||||
|
@ -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 (
|
from homeassistant.const import (
|
||||||
CONF_ABOVE,
|
CONF_ABOVE,
|
||||||
CONF_ATTRIBUTE,
|
CONF_ATTRIBUTE,
|
||||||
@ -28,6 +24,7 @@ from homeassistant.helpers.event import (
|
|||||||
async_track_same_state,
|
async_track_same_state,
|
||||||
async_track_state_change_event,
|
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-calls, allow-untyped-defs
|
# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs
|
||||||
@ -87,8 +84,8 @@ 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 = "numeric_state",
|
platform_type: str = "numeric_state",
|
||||||
) -> CALLBACK_TYPE:
|
) -> CALLBACK_TYPE:
|
||||||
@ -105,8 +102,8 @@ async def async_attach_trigger(
|
|||||||
attribute = config.get(CONF_ATTRIBUTE)
|
attribute = config.get(CONF_ATTRIBUTE)
|
||||||
job = HassJob(action)
|
job = HassJob(action)
|
||||||
|
|
||||||
trigger_data = automation_info["trigger_data"]
|
trigger_data = trigger_info["trigger_data"]
|
||||||
_variables = automation_info["variables"] or {}
|
_variables = trigger_info["variables"] or {}
|
||||||
|
|
||||||
if value_template is not None:
|
if value_template is not None:
|
||||||
value_template.hass = hass
|
value_template.hass = hass
|
||||||
@ -139,7 +136,7 @@ async def async_attach_trigger(
|
|||||||
except exceptions.ConditionError as ex:
|
except exceptions.ConditionError as ex:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Error initializing '%s' trigger: %s",
|
"Error initializing '%s' trigger: %s",
|
||||||
automation_info["name"],
|
trigger_info["name"],
|
||||||
ex,
|
ex,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -185,7 +182,7 @@ async def async_attach_trigger(
|
|||||||
try:
|
try:
|
||||||
matching = check_numeric_state(entity_id, from_s, to_s)
|
matching = check_numeric_state(entity_id, from_s, to_s)
|
||||||
except exceptions.ConditionError as ex:
|
except exceptions.ConditionError as ex:
|
||||||
_LOGGER.warning("Error in '%s' trigger: %s", automation_info["name"], ex)
|
_LOGGER.warning("Error in '%s' trigger: %s", trigger_info["name"], ex)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not matching:
|
if not matching:
|
||||||
@ -201,7 +198,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",
|
"Error rendering '%s' for template: %s",
|
||||||
automation_info["name"],
|
trigger_info["name"],
|
||||||
ex,
|
ex,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
@ -7,10 +7,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_ATTRIBUTE, CONF_FOR, CONF_PLATFORM, MATCH_ALL
|
from homeassistant.const import CONF_ATTRIBUTE, CONF_FOR, CONF_PLATFORM, MATCH_ALL
|
||||||
from homeassistant.core import (
|
from homeassistant.core import (
|
||||||
CALLBACK_TYPE,
|
CALLBACK_TYPE,
|
||||||
@ -30,6 +26,7 @@ from homeassistant.helpers.event import (
|
|||||||
async_track_state_change_event,
|
async_track_state_change_event,
|
||||||
process_state_match,
|
process_state_match,
|
||||||
)
|
)
|
||||||
|
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-calls, allow-untyped-defs
|
# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs
|
||||||
@ -97,8 +94,8 @@ 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 = "state",
|
platform_type: str = "state",
|
||||||
) -> CALLBACK_TYPE:
|
) -> CALLBACK_TYPE:
|
||||||
@ -131,8 +128,8 @@ async def async_attach_trigger(
|
|||||||
attribute = config.get(CONF_ATTRIBUTE)
|
attribute = config.get(CONF_ATTRIBUTE)
|
||||||
job = HassJob(action)
|
job = HassJob(action)
|
||||||
|
|
||||||
trigger_data = automation_info["trigger_data"]
|
trigger_data = trigger_info["trigger_data"]
|
||||||
_variables = automation_info["variables"] or {}
|
_variables = trigger_info["variables"] or {}
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def state_automation_listener(event: Event):
|
def state_automation_listener(event: Event):
|
||||||
@ -193,7 +190,7 @@ async def async_attach_trigger(
|
|||||||
call_action()
|
call_action()
|
||||||
return
|
return
|
||||||
|
|
||||||
trigger_info = {
|
data = {
|
||||||
"trigger": {
|
"trigger": {
|
||||||
"platform": "state",
|
"platform": "state",
|
||||||
"entity_id": entity,
|
"entity_id": entity,
|
||||||
@ -201,7 +198,7 @@ async def async_attach_trigger(
|
|||||||
"to_state": to_s,
|
"to_state": to_s,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
variables = {**_variables, **trigger_info}
|
variables = {**_variables, **data}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
period[entity] = cv.positive_time_period(
|
period[entity] = cv.positive_time_period(
|
||||||
@ -209,7 +206,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
|
||||||
|
|
||||||
|
@ -5,10 +5,6 @@ from functools import partial
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import sensor
|
from homeassistant.components import sensor
|
||||||
from homeassistant.components.automation import (
|
|
||||||
AutomationActionType,
|
|
||||||
AutomationTriggerInfo,
|
|
||||||
)
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_DEVICE_CLASS,
|
ATTR_DEVICE_CLASS,
|
||||||
CONF_AT,
|
CONF_AT,
|
||||||
@ -23,6 +19,7 @@ from homeassistant.helpers.event import (
|
|||||||
async_track_state_change_event,
|
async_track_state_change_event,
|
||||||
async_track_time_change,
|
async_track_time_change,
|
||||||
)
|
)
|
||||||
|
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
@ -45,11 +42,11 @@ TRIGGER_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,
|
||||||
) -> 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"]
|
||||||
entities: dict[str, CALLBACK_TYPE] = {}
|
entities: dict[str, CALLBACK_TYPE] = {}
|
||||||
removes = []
|
removes = []
|
||||||
job = HassJob(action)
|
job = HassJob(action)
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
"""Offer time listening automation rules."""
|
"""Offer time listening automation rules."""
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.automation import (
|
|
||||||
AutomationActionType,
|
|
||||||
AutomationTriggerInfo,
|
|
||||||
)
|
|
||||||
from homeassistant.const import CONF_PLATFORM
|
from homeassistant.const import CONF_PLATFORM
|
||||||
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
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.event import async_track_time_change
|
from homeassistant.helpers.event import async_track_time_change
|
||||||
|
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
|
||||||
@ -63,11 +60,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."""
|
||||||
trigger_data = automation_info["trigger_data"]
|
trigger_data = trigger_info["trigger_data"]
|
||||||
hours = config.get(CONF_HOURS)
|
hours = config.get(CONF_HOURS)
|
||||||
minutes = config.get(CONF_MINUTES)
|
minutes = config.get(CONF_MINUTES)
|
||||||
seconds = config.get(CONF_SECONDS)
|
seconds = config.get(CONF_SECONDS)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user