mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Add alias support to all triggers (#77184)
This commit is contained in:
parent
29b502bb0a
commit
9843753f30
@ -443,9 +443,13 @@ class AutomationEntity(ToggleEntity, RestoreEntity):
|
||||
This method is a coroutine.
|
||||
"""
|
||||
reason = ""
|
||||
if "trigger" in run_variables and "description" in run_variables["trigger"]:
|
||||
reason = f' by {run_variables["trigger"]["description"]}'
|
||||
self._logger.debug("Automation triggered%s", reason)
|
||||
alias = ""
|
||||
if "trigger" in run_variables:
|
||||
if "description" in run_variables["trigger"]:
|
||||
reason = f' by {run_variables["trigger"]["description"]}'
|
||||
if "alias" in run_variables["trigger"]:
|
||||
alias = f' trigger \'{run_variables["trigger"]["alias"]}\''
|
||||
self._logger.debug("Automation%s triggered%s", alias, reason)
|
||||
|
||||
# Create a new context referring to the old context.
|
||||
parent_id = None if context is None else context.id
|
||||
|
@ -1425,6 +1425,7 @@ CONDITION_ACTION_SCHEMA: vol.Schema = vol.Schema(
|
||||
|
||||
TRIGGER_BASE_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_ALIAS): str,
|
||||
vol.Required(CONF_PLATFORM): str,
|
||||
vol.Optional(CONF_ID): str,
|
||||
vol.Optional(CONF_VARIABLES): SCRIPT_VARIABLES_SCHEMA,
|
||||
|
@ -9,7 +9,13 @@ from typing import TYPE_CHECKING, Any, Protocol, TypedDict
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import CONF_ENABLED, CONF_ID, CONF_PLATFORM, CONF_VARIABLES
|
||||
from homeassistant.const import (
|
||||
CONF_ALIAS,
|
||||
CONF_ENABLED,
|
||||
CONF_ID,
|
||||
CONF_PLATFORM,
|
||||
CONF_VARIABLES,
|
||||
)
|
||||
from homeassistant.core import CALLBACK_TYPE, Context, HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.loader import IntegrationNotFound, async_get_integration
|
||||
@ -43,6 +49,7 @@ class TriggerData(TypedDict):
|
||||
|
||||
id: str
|
||||
idx: str
|
||||
alias: str | None
|
||||
|
||||
|
||||
class TriggerInfo(TypedDict):
|
||||
@ -130,7 +137,8 @@ async def async_initialize_triggers(
|
||||
platform = await _async_get_trigger_platform(hass, conf)
|
||||
trigger_id = conf.get(CONF_ID, f"{idx}")
|
||||
trigger_idx = f"{idx}"
|
||||
trigger_data = TriggerData(id=trigger_id, idx=trigger_idx)
|
||||
trigger_alias = conf.get(CONF_ALIAS)
|
||||
trigger_data = TriggerData(id=trigger_id, idx=trigger_idx, alias=trigger_alias)
|
||||
info = TriggerInfo(
|
||||
domain=domain,
|
||||
name=name,
|
||||
|
@ -103,3 +103,37 @@ async def test_if_disabled_trigger_not_firing(
|
||||
hass.bus.async_fire("enabled_trigger_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_trigger_alias(
|
||||
hass: HomeAssistant, calls: list[ServiceCall], caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test triggers support aliases."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"automation",
|
||||
{
|
||||
"automation": {
|
||||
"trigger": [
|
||||
{
|
||||
"alias": "My event",
|
||||
"platform": "event",
|
||||
"event_type": "trigger_event",
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {"alias": "{{ trigger.alias }}"},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.bus.async_fire("trigger_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["alias"] == "My event"
|
||||
assert (
|
||||
"Automation trigger 'My event' triggered by event 'trigger_event'"
|
||||
in caplog.text
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user