Allow device_attr and is_device_attr to be used as a filter and a test (respectively) (#81924)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
On Freund 2022-11-24 09:31:17 +02:00 committed by GitHub
parent 8a8732f0bc
commit 9dd1b85cbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -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"])

View File

@ -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."""