Rendering complex template objects to leave non-template values alone (#29353)
This commit is contained in:
parent
b1b784484e
commit
eef91f843d
4 changed files with 18 additions and 4 deletions
|
@ -489,8 +489,9 @@ def template_complex(value):
|
||||||
for key, element in return_value.items():
|
for key, element in return_value.items():
|
||||||
return_value[key] = template_complex(element)
|
return_value[key] = template_complex(element)
|
||||||
return return_value
|
return return_value
|
||||||
|
if isinstance(value, str):
|
||||||
return template(value)
|
return template(value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
def datetime(value):
|
def datetime(value):
|
||||||
|
|
|
@ -69,7 +69,9 @@ def render_complex(value, variables=None):
|
||||||
return [render_complex(item, variables) for item in value]
|
return [render_complex(item, variables) for item in value]
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
return {key: render_complex(item, variables) for key, item in value.items()}
|
return {key: render_complex(item, variables) for key, item in value.items()}
|
||||||
return value.async_render(variables)
|
if isinstance(value, Template):
|
||||||
|
return value.async_render(variables)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
def extract_entities(
|
def extract_entities(
|
||||||
|
|
|
@ -395,7 +395,7 @@ def test_template_complex():
|
||||||
"""Test template_complex validator."""
|
"""Test template_complex validator."""
|
||||||
schema = vol.Schema(cv.template_complex)
|
schema = vol.Schema(cv.template_complex)
|
||||||
|
|
||||||
for value in (None, "{{ partial_print }", "{% if True %}Hello"):
|
for value in ("{{ partial_print }", "{% if True %}Hello"):
|
||||||
with pytest.raises(vol.MultipleInvalid):
|
with pytest.raises(vol.MultipleInvalid):
|
||||||
schema(value)
|
schema(value)
|
||||||
|
|
||||||
|
@ -420,6 +420,10 @@ def test_template_complex():
|
||||||
["{{ beer }}", 1],
|
["{{ beer }}", 1],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Ensure we don't mutate non-string types that cannot be templates.
|
||||||
|
for value in (1, True, None):
|
||||||
|
assert schema(value) == value
|
||||||
|
|
||||||
|
|
||||||
def test_time_zone():
|
def test_time_zone():
|
||||||
"""Test time zone validation."""
|
"""Test time zone validation."""
|
||||||
|
|
|
@ -1796,3 +1796,10 @@ def test_length_of_states(hass):
|
||||||
|
|
||||||
tpl = template.Template("{{ states.sensor | length }}", hass)
|
tpl = template.Template("{{ states.sensor | length }}", hass)
|
||||||
assert tpl.async_render() == "2"
|
assert tpl.async_render() == "2"
|
||||||
|
|
||||||
|
|
||||||
|
def test_render_complex_handling_non_template_values(hass):
|
||||||
|
"""Test that we can render non-template fields."""
|
||||||
|
assert template.render_complex(
|
||||||
|
{True: 1, False: template.Template("{{ hello }}", hass)}, {"hello": 2}
|
||||||
|
) == {True: 1, False: "2"}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue