From 0457bb2717c6ef61be1c975cb98a96f596fb474a Mon Sep 17 00:00:00 2001 From: David Poll Date: Mon, 13 Mar 2023 10:20:33 -0700 Subject: [PATCH] Add is_hidden_entity test for Jinja templates (#89011) --- homeassistant/helpers/template.py | 10 ++++++++++ tests/helpers/test_template.py | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 1c5d15801f8..8f68c7af378 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -1442,6 +1442,13 @@ def distance(hass, *args): ) +def is_hidden_entity(hass: HomeAssistant, entity_id: str) -> bool: + """Test if an entity is hidden.""" + entity_reg = entity_registry.async_get(hass) + entry = entity_reg.async_get(entity_id) + return entry is not None and entry.hidden + + def is_state(hass: HomeAssistant, entity_id: str, state: str | list[str]) -> bool: """Test if a state is a specific value.""" state_obj = _get_state(hass, entity_id) @@ -2266,6 +2273,9 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment): self.globals["area_devices"] = hassfunction(area_devices) self.filters["area_devices"] = pass_context(self.globals["area_devices"]) + self.globals["is_hidden_entity"] = hassfunction(is_hidden_entity) + self.tests["is_hidden_entity"] = pass_context(self.globals["is_hidden_entity"]) + self.globals["integration_entities"] = hassfunction(integration_entities) self.filters["integration_entities"] = pass_context( self.globals["integration_entities"] diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 740040835c6..750602c9d6c 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -1443,6 +1443,26 @@ def test_if_state_exists(hass: HomeAssistant) -> None: assert tpl.async_render() == "exists" +def test_is_hidden_entity( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: + """Test is_hidden_entity method.""" + hidden_entity = entity_registry.async_get_or_create( + "sensor", "mock", "hidden", hidden_by=er.RegistryEntryHider.USER + ) + visible_entity = entity_registry.async_get_or_create("sensor", "mock", "visible") + assert template.Template( + f"{{{{ is_hidden_entity('{hidden_entity.entity_id}') }}}}", + hass, + ).async_render() + + assert not template.Template( + f"{{{{ is_hidden_entity('{visible_entity.entity_id}') }}}}", + hass, + ).async_render() + + def test_is_state(hass: HomeAssistant) -> None: """Test is_state method.""" hass.states.async_set("test.object", "available")