Add is_hidden_entity test for Jinja templates (#89011)

This commit is contained in:
David Poll 2023-03-13 10:20:33 -07:00 committed by GitHub
parent 02389960ce
commit 0457bb2717
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View file

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

View file

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