From e329d5047f846a3405a8205c6a202f89bb426e56 Mon Sep 17 00:00:00 2001 From: Petro Date: Sun, 2 Oct 2022 14:13:31 +0000 Subject: [PATCH] add new jinja test and filters --- homeassistant/helpers/template.py | 6 ++- tests/helpers/test_template.py | 68 ++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 083d0e530aa7c1..e68b54eeb95acb 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -25,7 +25,7 @@ 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 @@ -2143,9 +2143,13 @@ def warn_unsupported(*args, **kwargs): 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 9c9a1e42a98747..b6c54d8ab8879a 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -1332,6 +1332,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.""" @@ -1352,10 +1368,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 %} @@ -1372,6 +1406,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.""" @@ -1382,6 +1432,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",