diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 7421e95f293..e177b76faf3 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -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 diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 2ed4bc7abab..4c2fed60bb4 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -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, diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index a48ccbbb7b9..9fde56ec7aa 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -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, diff --git a/tests/helpers/test_trigger.py b/tests/helpers/test_trigger.py index 9c59b00c843..7cee307f3ec 100644 --- a/tests/helpers/test_trigger.py +++ b/tests/helpers/test_trigger.py @@ -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 + )