Add version to templates (#78484)
This commit is contained in:
parent
de7e12eeaf
commit
8dbe293ae2
2 changed files with 47 additions and 0 deletions
|
@ -23,6 +23,7 @@ from typing import Any, NoReturn, TypeVar, cast, overload
|
||||||
from urllib.parse import urlencode as urllib_urlencode
|
from urllib.parse import urlencode as urllib_urlencode
|
||||||
import weakref
|
import weakref
|
||||||
|
|
||||||
|
from awesomeversion import AwesomeVersion
|
||||||
import jinja2
|
import jinja2
|
||||||
from jinja2 import pass_context, pass_environment
|
from jinja2 import pass_context, pass_environment
|
||||||
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
||||||
|
@ -1529,6 +1530,11 @@ def arc_tangent2(*args, default=_SENTINEL):
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
|
def version(value):
|
||||||
|
"""Filter and function to get version object of the value."""
|
||||||
|
return AwesomeVersion(value)
|
||||||
|
|
||||||
|
|
||||||
def square_root(value, default=_SENTINEL):
|
def square_root(value, default=_SENTINEL):
|
||||||
"""Filter and function to get square root of the value."""
|
"""Filter and function to get square root of the value."""
|
||||||
try:
|
try:
|
||||||
|
@ -2001,6 +2007,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
||||||
self.filters["slugify"] = slugify
|
self.filters["slugify"] = slugify
|
||||||
self.filters["iif"] = iif
|
self.filters["iif"] = iif
|
||||||
self.filters["bool"] = forgiving_boolean
|
self.filters["bool"] = forgiving_boolean
|
||||||
|
self.filters["version"] = version
|
||||||
self.globals["log"] = logarithm
|
self.globals["log"] = logarithm
|
||||||
self.globals["sin"] = sine
|
self.globals["sin"] = sine
|
||||||
self.globals["cos"] = cosine
|
self.globals["cos"] = cosine
|
||||||
|
@ -2033,6 +2040,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
||||||
self.globals["slugify"] = slugify
|
self.globals["slugify"] = slugify
|
||||||
self.globals["iif"] = iif
|
self.globals["iif"] = iif
|
||||||
self.globals["bool"] = forgiving_boolean
|
self.globals["bool"] = forgiving_boolean
|
||||||
|
self.globals["version"] = version
|
||||||
self.tests["is_number"] = is_number
|
self.tests["is_number"] = is_number
|
||||||
self.tests["match"] = regex_match
|
self.tests["match"] = regex_match
|
||||||
self.tests["search"] = regex_search
|
self.tests["search"] = regex_search
|
||||||
|
|
|
@ -1566,6 +1566,45 @@ def test_timedelta(mock_is_safe, hass):
|
||||||
assert result == "15 days"
|
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):
|
def test_regex_match(hass):
|
||||||
"""Test regex_match method."""
|
"""Test regex_match method."""
|
||||||
tpl = template.Template(
|
tpl = template.Template(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue