Allow skip parsing template result (#42401)

This commit is contained in:
Franck Nijhof
2020-10-26 16:01:09 +01:00
committed by GitHub
parent 80e8068b46
commit 45aba9bdf2
10 changed files with 98 additions and 24 deletions
+1 -1
View File
@@ -174,7 +174,7 @@ async def test_do_no_run_forever(hass, caplog):
assert await async_setup_component(
hass,
shell_command.DOMAIN,
{shell_command.DOMAIN: {"test_service": "sleep 10000s"}},
{shell_command.DOMAIN: {"test_service": "sleep 10000"}},
)
await hass.async_block_till_done()
+23
View File
@@ -915,3 +915,26 @@ async def test_condition_template_error(hass, caplog):
assert caplog.records[0].message.startswith(
"Error during template condition: UndefinedError:"
)
async def test_condition_template_invalid_results(hass):
"""Test template condition render false with invalid results."""
test = await condition.async_from_config(
hass, {"condition": "template", "value_template": "{{ 'string' }}"}
)
assert not test(hass)
test = await condition.async_from_config(
hass, {"condition": "template", "value_template": "{{ 10.1 }}"}
)
assert not test(hass)
test = await condition.async_from_config(
hass, {"condition": "template", "value_template": "{{ 42 }}"}
)
assert not test(hass)
test = await condition.async_from_config(
hass, {"condition": "template", "value_template": "{{ [1, 2, 3] }}"}
)
assert not test(hass)
+22
View File
@@ -2653,6 +2653,28 @@ async def test_legacy_templates(hass):
)
async def test_no_result_parsing(hass):
"""Test if templates results are not parsed."""
hass.states.async_set("sensor.temperature", "12")
assert (
template.Template("{{ states.sensor.temperature.state }}", hass).async_render(
parse_result=False
)
== "12"
)
assert (
template.Template("{{ false }}", hass).async_render(parse_result=False)
== "False"
)
assert (
template.Template("{{ [1, 2, 3] }}", hass).async_render(parse_result=False)
== "[1, 2, 3]"
)
async def test_is_static_still_ast_evals(hass):
"""Test is_static still convers to native type."""
tpl = template.Template("[1, 2]", hass)