Resolve traceback error when using variables in template triggers (#77287)
Co-authored-by: Erik <erik@montnemery.com>
This commit is contained in:
parent
ee32e0eb3f
commit
ba6a81c565
2 changed files with 112 additions and 15 deletions
|
@ -1,12 +1,13 @@
|
|||
"""The tests for the trigger helper."""
|
||||
from unittest.mock import MagicMock, call, patch
|
||||
from unittest.mock import ANY, MagicMock, call, patch
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||
from homeassistant.helpers.trigger import (
|
||||
_async_get_trigger_platform,
|
||||
async_initialize_triggers,
|
||||
async_validate_trigger_config,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
@ -137,3 +138,62 @@ async def test_trigger_alias(
|
|||
"Automation trigger 'My event' triggered by event 'trigger_event'"
|
||||
in caplog.text
|
||||
)
|
||||
|
||||
|
||||
async def test_async_initialize_triggers(
|
||||
hass: HomeAssistant, calls: list[ServiceCall], caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test async_initialize_triggers with different action types."""
|
||||
|
||||
log_cb = MagicMock()
|
||||
|
||||
action_calls = []
|
||||
|
||||
trigger_config = await async_validate_trigger_config(
|
||||
hass,
|
||||
[
|
||||
{
|
||||
"platform": "event",
|
||||
"event_type": ["trigger_event"],
|
||||
"variables": {
|
||||
"name": "Paulus",
|
||||
"via_event": "{{ trigger.event.event_type }}",
|
||||
},
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
async def async_action(*args):
|
||||
action_calls.append([*args])
|
||||
|
||||
@callback
|
||||
def cb_action(*args):
|
||||
action_calls.append([*args])
|
||||
|
||||
def non_cb_action(*args):
|
||||
action_calls.append([*args])
|
||||
|
||||
for action in (async_action, cb_action, non_cb_action):
|
||||
action_calls = []
|
||||
|
||||
unsub = await async_initialize_triggers(
|
||||
hass,
|
||||
trigger_config,
|
||||
action,
|
||||
"test",
|
||||
"",
|
||||
log_cb,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.bus.async_fire("trigger_event")
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(action_calls) == 1
|
||||
assert action_calls[0][0]["name"] == "Paulus"
|
||||
assert action_calls[0][0]["via_event"] == "trigger_event"
|
||||
log_cb.assert_called_once_with(ANY, "Initialized trigger")
|
||||
|
||||
log_cb.reset_mock()
|
||||
unsub()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue