Support templating MQTT triggers (#45614)
* Add support for limited templates (no HASS access) * Pass variables to automation triggers * Support templates in MQTT triggers * Spelling * Handle trigger referenced by variables * Raise on unsupported function in limited templates * Validate MQTT trigger schema in MQTT device trigger * Add trigger_variables to automation config schema * Don't print stacktrace when setting up trigger throws * Make pylint happy * Add trigger_variables to variables * Add debug prints, document limited template * Add tests * Validate MQTT trigger topic early when possible * Improve valid_subscribe_topic_template
This commit is contained in:
parent
b9b1caf4d7
commit
047f16772f
12 changed files with 262 additions and 15 deletions
|
@ -1237,6 +1237,94 @@ async def test_automation_variables(hass, caplog):
|
|||
assert len(calls) == 3
|
||||
|
||||
|
||||
async def test_automation_trigger_variables(hass, caplog):
|
||||
"""Test automation trigger variables."""
|
||||
calls = async_mock_service(hass, "test", "automation")
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"variables": {
|
||||
"event_type": "{{ trigger.event.event_type }}",
|
||||
},
|
||||
"trigger_variables": {
|
||||
"test_var": "defined_in_config",
|
||||
},
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data": {
|
||||
"value": "{{ test_var }}",
|
||||
"event_type": "{{ event_type }}",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"variables": {
|
||||
"event_type": "{{ trigger.event.event_type }}",
|
||||
"test_var": "overridden_in_config",
|
||||
},
|
||||
"trigger_variables": {
|
||||
"test_var": "defined_in_config",
|
||||
},
|
||||
"trigger": {"platform": "event", "event_type": "test_event_2"},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data": {
|
||||
"value": "{{ test_var }}",
|
||||
"event_type": "{{ event_type }}",
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["value"] == "defined_in_config"
|
||||
assert calls[0].data["event_type"] == "test_event"
|
||||
|
||||
hass.bus.async_fire("test_event_2")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
assert calls[1].data["value"] == "overridden_in_config"
|
||||
assert calls[1].data["event_type"] == "test_event_2"
|
||||
|
||||
assert "Error rendering variables" not in caplog.text
|
||||
|
||||
|
||||
async def test_automation_bad_trigger_variables(hass, caplog):
|
||||
"""Test automation trigger variables accessing hass is rejected."""
|
||||
calls = async_mock_service(hass, "test", "automation")
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"trigger_variables": {
|
||||
"test_var": "{{ states('foo.bar') }}",
|
||||
},
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
hass.bus.async_fire("test_event")
|
||||
assert "Use of 'states' is not supported in limited templates" in caplog.text
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_blueprint_automation(hass, calls):
|
||||
"""Test blueprint automation."""
|
||||
assert await async_setup_component(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue