Skip to content
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

Add version to templates #78484

Merged
merged 3 commits into from
Sep 15, 2022
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
8 changes: 8 additions & 0 deletions homeassistant/helpers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from urllib.parse import urlencode as urllib_urlencode
import weakref

from awesomeversion import AwesomeVersion
import jinja2
from jinja2 import pass_context, pass_environment
from jinja2.sandbox import ImmutableSandboxedEnvironment
Expand Down Expand Up @@ -1529,6 +1530,11 @@ def arc_tangent2(*args, default=_SENTINEL):
return default


def version(value):
"""Filter and function to get version object of the value."""
return AwesomeVersion(value)


def square_root(value, default=_SENTINEL):
"""Filter and function to get square root of the value."""
try:
Expand Down Expand Up @@ -2001,6 +2007,7 @@ def __init__(self, hass, limited=False, strict=False):
self.filters["slugify"] = slugify
self.filters["iif"] = iif
self.filters["bool"] = forgiving_boolean
self.filters["version"] = version
self.globals["log"] = logarithm
self.globals["sin"] = sine
self.globals["cos"] = cosine
Expand Down Expand Up @@ -2033,6 +2040,7 @@ def __init__(self, hass, limited=False, strict=False):
self.globals["slugify"] = slugify
self.globals["iif"] = iif
self.globals["bool"] = forgiving_boolean
self.globals["version"] = version
self.tests["is_number"] = is_number
self.tests["match"] = regex_match
self.tests["search"] = regex_search
Expand Down
39 changes: 39 additions & 0 deletions tests/helpers/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,45 @@ def test_timedelta(mock_is_safe, hass):
assert result == "15 days"


def test_version(hass):
"""Test version filter and function."""
filter_result = template.Template(
"{{ '2099.9.9' | version}}",
hass,
).async_render()
function_result = template.Template(
"{{ version('2099.9.9')}}",
hass,
).async_render()
assert filter_result == function_result == "2099.9.9"

filter_result = template.Template(
"{{ '2099.9.9' | version < '2099.9.10' }}",
hass,
).async_render()
function_result = template.Template(
"{{ version('2099.9.9') < '2099.9.10' }}",
hass,
).async_render()
assert filter_result == function_result is True

filter_result = template.Template(
"{{ '2099.9.9' | version == '2099.9.9' }}",
hass,
).async_render()
function_result = template.Template(
"{{ version('2099.9.9') == '2099.9.9' }}",
hass,
).async_render()
assert filter_result == function_result is True

with pytest.raises(TemplateError):
template.Template(
"{{ version(None) < '2099.9.10' }}",
hass,
).async_render()


def test_regex_match(hass):
"""Test regex_match method."""
tpl = template.Template(
Expand Down