Adds states and state_attr as a filter, adds is_state and is_state_attr as a test. (#79473)

This commit is contained in:
Petro31 2022-10-25 13:49:51 -04:00 committed by GitHub
parent d3ada34498
commit f73fc9e355
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 2 deletions

View File

@ -25,7 +25,7 @@ import weakref
from awesomeversion import AwesomeVersion
import jinja2
from jinja2 import pass_context, pass_environment
from jinja2 import pass_context, pass_environment, pass_eval_context
from jinja2.sandbox import ImmutableSandboxedEnvironment
from jinja2.utils import Namespace
import voluptuous as vol
@ -2153,9 +2153,13 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
self.filters["closest"] = pass_context(hassfunction(closest_filter))
self.globals["distance"] = hassfunction(distance)
self.globals["is_state"] = hassfunction(is_state)
self.tests["is_state"] = pass_eval_context(self.globals["is_state"])
self.globals["is_state_attr"] = hassfunction(is_state_attr)
self.tests["is_state_attr"] = pass_eval_context(self.globals["is_state_attr"])
self.globals["state_attr"] = hassfunction(state_attr)
self.filters["state_attr"] = self.globals["state_attr"]
self.globals["states"] = AllStates(hass)
self.filters["states"] = self.globals["states"]
self.globals["utcnow"] = hassfunction(utcnow)
self.globals["now"] = hassfunction(now)

View File

@ -1348,6 +1348,22 @@ def test_is_state(hass):
)
assert tpl.async_render() is False
tpl = template.Template(
"""
{% if "test.object" is is_state("available") %}yes{% else %}no{% endif %}
""",
hass,
)
assert tpl.async_render() == "yes"
tpl = template.Template(
"""
{{ ['test.object'] | select("is_state", "available") | first | default }}
""",
hass,
)
assert tpl.async_render() == "test.object"
def test_is_state_attr(hass):
"""Test is_state_attr method."""
@ -1368,10 +1384,28 @@ def test_is_state_attr(hass):
)
assert tpl.async_render() is False
tpl = template.Template(
"""
{% if "test.object" is is_state_attr("mode", "on") %}yes{% else %}no{% endif %}
""",
hass,
)
assert tpl.async_render() == "yes"
tpl = template.Template(
"""
{{ ['test.object'] | select("is_state_attr", "mode", "on") | first | default }}
""",
hass,
)
assert tpl.async_render() == "test.object"
def test_state_attr(hass):
"""Test state_attr method."""
hass.states.async_set("test.object", "available", {"mode": "on"})
hass.states.async_set(
"test.object", "available", {"effect": "action", "mode": "on"}
)
tpl = template.Template(
"""
{% if state_attr("test.object", "mode") == "on" %}yes{% else %}no{% endif %}
@ -1388,6 +1422,22 @@ def test_state_attr(hass):
)
assert tpl.async_render() is True
tpl = template.Template(
"""
{% if "test.object" | state_attr("mode") == "on" %}yes{% else %}no{% endif %}
""",
hass,
)
assert tpl.async_render() == "yes"
tpl = template.Template(
"""
{{ ['test.object'] | map("state_attr", "effect") | first | default }}
""",
hass,
)
assert tpl.async_render() == "action"
def test_states_function(hass):
"""Test using states as a function."""
@ -1398,6 +1448,22 @@ def test_states_function(hass):
tpl2 = template.Template('{{ states("test.object2") }}', hass)
assert tpl2.async_render() == "unknown"
tpl = template.Template(
"""
{% if "test.object" | states == "available" %}yes{% else %}no{% endif %}
""",
hass,
)
assert tpl.async_render() == "yes"
tpl = template.Template(
"""
{{ ['test.object'] | map("states") | first | default }}
""",
hass,
)
assert tpl.async_render() == "available"
@patch(
"homeassistant.helpers.template.TemplateEnvironment.is_safe_callable",