diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 0db755e5115..dfab80e5223 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -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) diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index e42f001818b..63a6154a190 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -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",