mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Add version to templates (#78484)
This commit is contained in:
parent
de7e12eeaf
commit
8dbe293ae2
@ -23,6 +23,7 @@ from typing import Any, NoReturn, TypeVar, cast, overload
|
||||
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
|
||||
@ -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:
|
||||
@ -2001,6 +2007,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
||||
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
|
||||
@ -2033,6 +2040,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
||||
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
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user