From 9dd1b85cbb20fe9b0f125a513b3fceb78242df55 Mon Sep 17 00:00:00 2001 From: On Freund Date: Thu, 24 Nov 2022 09:31:17 +0200 Subject: [PATCH] Allow `device_attr` and `is_device_attr` to be used as a filter and a test (respectively) (#81924) Co-authored-by: Franck Nijhof --- homeassistant/helpers/template.py | 3 +++ tests/helpers/test_template.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index fe073dcb742..3d74024cb00 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -2094,7 +2094,10 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment): self.filters["device_entities"] = pass_context(self.globals["device_entities"]) self.globals["device_attr"] = hassfunction(device_attr) + self.filters["device_attr"] = pass_context(self.globals["device_attr"]) + self.globals["is_device_attr"] = hassfunction(is_device_attr) + self.tests["is_device_attr"] = pass_eval_context(self.globals["is_device_attr"]) self.globals["config_entry_id"] = hassfunction(config_entry_id) self.filters["config_entry_id"] = pass_context(self.globals["config_entry_id"]) diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index cd73e663b71..07463787a8b 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -2738,6 +2738,21 @@ async def test_device_attr(hass: HomeAssistant) -> None: assert_result_info(info, True) assert info.rate_limit is None + # Test filter syntax (device_attr) + info = render_to_info( + hass, f"{{{{ '{entity_entry.entity_id}' | device_attr('model') }}}}" + ) + assert_result_info(info, "test") + assert info.rate_limit is None + + # Test test syntax (is_device_attr) + info = render_to_info( + hass, + f"{{{{ ['{device_entry.id}'] | select('is_device_attr', 'model', 'test') | list }}}}", + ) + assert_result_info(info, [device_entry.id]) + assert info.rate_limit is None + async def test_area_id(hass: HomeAssistant) -> None: """Test area_id function."""