Add as_function to allow macros to return values (#142033)

This commit is contained in:
David Poll
2025-05-26 11:22:45 -07:00
committed by GitHub
parent 27b0488f05
commit 670e8dd434
2 changed files with 43 additions and 0 deletions

View File

@@ -828,6 +828,23 @@ def test_apply_macro_with_arguments(hass: HomeAssistant) -> None:
).async_render() == ["Hey, Alice!", "Hey, Bob!"]
def test_as_function(hass: HomeAssistant) -> None:
"""Test as_function."""
assert (
template.Template(
"""
{%- macro macro_double(num, returns) -%}
{%- do returns(num * 2) -%}
{%- endmacro -%}
{%- set double = macro_double | as_function -%}
{{ double(5) }}
""",
hass,
).async_render()
== 10
)
def test_logarithm(hass: HomeAssistant) -> None:
"""Test logarithm."""
tests = [