Skip to content

Add apply to make macros/callables useful in filters and tests #144227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
Open
7 changes: 7 additions & 0 deletions homeassistant/helpers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,11 @@ def add(value, amount, default=_SENTINEL):
return default


def apply(value, fn, *args, **kwargs):
"""Call the given callable with the provided arguments and keyword arguments."""
return fn(value, *args, **kwargs)


def logarithm(value, base=math.e, default=_SENTINEL):
"""Filter and function to get logarithm of the value with a specific base."""
try:
Expand Down Expand Up @@ -3110,6 +3115,7 @@ def __init__(

self.filters["acos"] = arc_cosine
self.filters["add"] = add
self.filters["apply"] = apply
self.filters["as_datetime"] = as_datetime
self.filters["as_local"] = dt_util.as_local
self.filters["as_timedelta"] = as_timedelta
Expand Down Expand Up @@ -3169,6 +3175,7 @@ def __init__(
self.filters["unpack"] = struct_unpack
self.filters["version"] = version

self.tests["apply"] = apply
self.tests["contains"] = contains
self.tests["datetime"] = _is_datetime
self.tests["is_number"] = is_number
Expand Down
56 changes: 56 additions & 0 deletions tests/helpers/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,62 @@ def test_add(hass: HomeAssistant) -> None:
assert render(hass, "{{ 'no_number' | add(10, default=1) }}") == 1


def test_apply(hass: HomeAssistant) -> None:
"""Test apply."""
assert template.Template(
"""
{%- macro add_foo(arg) -%}
{{arg}}foo
{%- endmacro -%}
{{ ["a", "b", "c"] | map('apply', add_foo) | list }}
""",
hass,
).async_render() == ["afoo", "bfoo", "cfoo"]

assert template.Template(
"""
{{ ['1', '2', '3', '4', '5'] | map('apply', int) | list }}
""",
hass,
).async_render() == [1, 2, 3, 4, 5]


def test_apply_macro_with_arguments(hass: HomeAssistant) -> None:
"""Test apply macro with positional, named, and mixed arguments."""
# Test macro with positional arguments
assert template.Template(
"""
{%- macro greet(name, greeting) -%}
{{ greeting }}, {{ name }}!
{%- endmacro %}
{{ ["Alice", "Bob"] | map('apply', greet, "Hello") | list }}
""",
hass,
).async_render() == ["Hello, Alice!", "Hello, Bob!"]

# Test macro with named arguments
assert template.Template(
"""
{%- macro greet(name, greeting="Hi") -%}
{{ greeting }}, {{ name }}!
{%- endmacro %}
{{ ["Alice", "Bob"] | map('apply', greet, greeting="Hello") | list }}
""",
hass,
).async_render() == ["Hello, Alice!", "Hello, Bob!"]

# Test macro with mixed positional and named arguments
assert template.Template(
"""
{%- macro greet(name, separator, greeting="Hi") -%}
{{ greeting }}{{separator}} {{ name }}!
{%- endmacro %}
{{ ["Alice", "Bob"] | map('apply', greet, "," , greeting="Hey") | list }}
""",
hass,
).async_render() == ["Hey, Alice!", "Hey, Bob!"]


def test_logarithm(hass: HomeAssistant) -> None:
"""Test logarithm."""
tests = [
Expand Down