Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/helpers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,7 @@ def __init__(self, hass, limited=False, strict=False):
self.template_cache: weakref.WeakValueDictionary[
str | jinja2.nodes.Template, CodeType | str | None
] = weakref.WeakValueDictionary()
self.add_extension("jinja2.ext.loopcontrols")
self.filters["round"] = forgiving_round
self.filters["multiply"] = multiply
self.filters["log"] = logarithm
Expand Down
20 changes: 20 additions & 0 deletions tests/helpers/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,26 @@ def test_iterating_domain_states(hass: HomeAssistant) -> None:
)


def test_loop_controls(hass: HomeAssistant) -> None:
"""Test that loop controls are enabled."""
assert (
template.Template(
"""
{%- for v in range(10) %}
{%- if v == 1 -%}
{%- continue -%}
{%- elif v == 3 -%}
{%- break -%}
{%- endif -%}
{{ v }}
{%- endfor -%}
""",
hass,
).async_render()
== "02"
)


def test_float_function(hass: HomeAssistant) -> None:
"""Test float function."""
hass.states.async_set("sensor.temperature", "12")
Expand Down