mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Adds states and state_attr as a filter, adds is_state and is_state_attr as a test. (#79473)
This commit is contained in:
parent
d3ada34498
commit
f73fc9e355
@ -25,7 +25,7 @@ import weakref
|
|||||||
|
|
||||||
from awesomeversion import AwesomeVersion
|
from awesomeversion import AwesomeVersion
|
||||||
import jinja2
|
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.sandbox import ImmutableSandboxedEnvironment
|
||||||
from jinja2.utils import Namespace
|
from jinja2.utils import Namespace
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -2153,9 +2153,13 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
|||||||
self.filters["closest"] = pass_context(hassfunction(closest_filter))
|
self.filters["closest"] = pass_context(hassfunction(closest_filter))
|
||||||
self.globals["distance"] = hassfunction(distance)
|
self.globals["distance"] = hassfunction(distance)
|
||||||
self.globals["is_state"] = hassfunction(is_state)
|
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.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.globals["state_attr"] = hassfunction(state_attr)
|
||||||
|
self.filters["state_attr"] = self.globals["state_attr"]
|
||||||
self.globals["states"] = AllStates(hass)
|
self.globals["states"] = AllStates(hass)
|
||||||
|
self.filters["states"] = self.globals["states"]
|
||||||
self.globals["utcnow"] = hassfunction(utcnow)
|
self.globals["utcnow"] = hassfunction(utcnow)
|
||||||
self.globals["now"] = hassfunction(now)
|
self.globals["now"] = hassfunction(now)
|
||||||
|
|
||||||
|
@ -1348,6 +1348,22 @@ def test_is_state(hass):
|
|||||||
)
|
)
|
||||||
assert tpl.async_render() is False
|
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):
|
def test_is_state_attr(hass):
|
||||||
"""Test is_state_attr method."""
|
"""Test is_state_attr method."""
|
||||||
@ -1368,10 +1384,28 @@ def test_is_state_attr(hass):
|
|||||||
)
|
)
|
||||||
assert tpl.async_render() is False
|
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):
|
def test_state_attr(hass):
|
||||||
"""Test state_attr method."""
|
"""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(
|
tpl = template.Template(
|
||||||
"""
|
"""
|
||||||
{% if state_attr("test.object", "mode") == "on" %}yes{% else %}no{% endif %}
|
{% 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
|
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):
|
def test_states_function(hass):
|
||||||
"""Test using states as a function."""
|
"""Test using states as a function."""
|
||||||
@ -1398,6 +1448,22 @@ def test_states_function(hass):
|
|||||||
tpl2 = template.Template('{{ states("test.object2") }}', hass)
|
tpl2 = template.Template('{{ states("test.object2") }}', hass)
|
||||||
assert tpl2.async_render() == "unknown"
|
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(
|
@patch(
|
||||||
"homeassistant.helpers.template.TemplateEnvironment.is_safe_callable",
|
"homeassistant.helpers.template.TemplateEnvironment.is_safe_callable",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user