Extract variable rendering (#39934)

This commit is contained in:
Paulus Schoutsen committed 2020-09-11 12:15:28 +00:00
1 parent b107e87d38
commit 8ef04268be
8 files changed
+193 -25

No files matched your search

+20 -1
View File
@@ -1136,7 +1136,7 @@ async def test_logbook_humanify_automation_triggered_event(hass):
assert event2["entity_id"] == "automation.bye"
async def test_automation_variables(hass):
async def test_automation_variables(hass, caplog):
"""Test automation variables."""
calls = async_mock_service(hass, "test", "automation")
@@ -1172,6 +1172,15 @@ async def test_automation_variables(hass):
"service": "test.automation",
},
},
{
"variables": {
"test_var": "{{ trigger.event.data.break + 1 }}",
},
"trigger": {"platform": "event", "event_type": "test_event_3"},
"action": {
"service": "test.automation",
},
},
]
},
)
@@ -1188,3 +1197,13 @@ async def test_automation_variables(hass):
hass.bus.async_fire("test_event_2", {"pass_condition": True})
await hass.async_block_till_done()
assert len(calls) == 2
assert "Error rendering variables" not in caplog.text
hass.bus.async_fire("test_event_3")
await hass.async_block_till_done()
assert len(calls) == 2
assert "Error rendering variables" in caplog.text
hass.bus.async_fire("test_event_3", {"break": 0})
await hass.async_block_till_done()
assert len(calls) == 3
+26 -1
View File
@@ -17,6 +17,7 @@ from homeassistant.const import (
)
from homeassistant.core import Context, callback, split_entity_id
from homeassistant.exceptions import ServiceNotFound
from homeassistant.helpers import template
from homeassistant.helpers.event import async_track_state_change
from homeassistant.helpers.service import async_get_all_descriptions
from homeassistant.loader import bind_hass
@@ -617,7 +618,7 @@ async def test_concurrent_script(hass, concurrently):
assert not script.is_on(hass, "script.script2")
async def test_script_variables(hass):
async def test_script_variables(hass, caplog):
"""Test defining scripts."""
assert await async_setup_component(
hass,
@@ -652,6 +653,19 @@ async def test_script_variables(hass):
},
],
},
"script3": {
"variables": {
"test_var": "{{ break + 1 }}",
},
"sequence": [
{
"service": "test.script",
"data": {
"value": "{{ test_var }}",
},
},
],
},
}
},
)
@@ -681,3 +695,14 @@ async def test_script_variables(hass):
assert len(mock_calls) == 3
assert mock_calls[2].data["value"] == "from_service"
assert "Error rendering variables" not in caplog.text
with pytest.raises(template.TemplateError):
await hass.services.async_call("script", "script3", blocking=True)
assert "Error rendering variables" in caplog.text
assert len(mock_calls) == 3
await hass.services.async_call("script", "script3", {"break": 0}, blocking=True)
assert len(mock_calls) == 4
assert mock_calls[3].data["value"] == "1"
+60
View File
@@ -0,0 +1,60 @@
"""Test script variables."""
import pytest
from homeassistant.helpers import config_validation as cv, template
async def test_static_vars():
"""Test static vars."""
orig = {"hello": "world"}
var = cv.SCRIPT_VARIABLES_SCHEMA(orig)
rendered = var.async_render(None, None)
assert rendered is not orig
assert rendered == orig
async def test_static_vars_run_args():
"""Test static vars."""
orig = {"hello": "world"}
orig_copy = dict(orig)
var = cv.SCRIPT_VARIABLES_SCHEMA(orig)
rendered = var.async_render(None, {"hello": "override", "run": "var"})
assert rendered == {"hello": "override", "run": "var"}
# Make sure we don't change original vars
assert orig == orig_copy
async def test_template_vars(hass):
"""Test template vars."""
var = cv.SCRIPT_VARIABLES_SCHEMA({"hello": "{{ 1 + 1 }}"})
rendered = var.async_render(hass, None)
assert rendered == {"hello": "2"}
async def test_template_vars_run_args(hass):
"""Test template vars."""
var = cv.SCRIPT_VARIABLES_SCHEMA(
{
"something": "{{ run_var_ex + 1 }}",
"something_2": "{{ run_var_ex + 1 }}",
}
)
rendered = var.async_render(
hass,
{
"run_var_ex": 5,
"something_2": 1,
},
)
assert rendered == {
"run_var_ex": 5,
"something": "6",
"something_2": 1,
}
async def test_template_vars_error(hass):
"""Test template vars."""
var = cv.SCRIPT_VARIABLES_SCHEMA({"hello": "{{ canont.work }}"})
with pytest.raises(template.TemplateError):
var.async_render(hass, None)