mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Add is_hidden_entity test for Jinja templates (#89011)
This commit is contained in:
parent
02389960ce
commit
0457bb2717
@ -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:
|
def is_state(hass: HomeAssistant, entity_id: str, state: str | list[str]) -> bool:
|
||||||
"""Test if a state is a specific value."""
|
"""Test if a state is a specific value."""
|
||||||
state_obj = _get_state(hass, entity_id)
|
state_obj = _get_state(hass, entity_id)
|
||||||
@ -2266,6 +2273,9 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
|||||||
self.globals["area_devices"] = hassfunction(area_devices)
|
self.globals["area_devices"] = hassfunction(area_devices)
|
||||||
self.filters["area_devices"] = pass_context(self.globals["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.globals["integration_entities"] = hassfunction(integration_entities)
|
||||||
self.filters["integration_entities"] = pass_context(
|
self.filters["integration_entities"] = pass_context(
|
||||||
self.globals["integration_entities"]
|
self.globals["integration_entities"]
|
||||||
|
@ -1443,6 +1443,26 @@ def test_if_state_exists(hass: HomeAssistant) -> None:
|
|||||||
assert tpl.async_render() == "exists"
|
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:
|
def test_is_state(hass: HomeAssistant) -> None:
|
||||||
"""Test is_state method."""
|
"""Test is_state method."""
|
||||||
hass.states.async_set("test.object", "available")
|
hass.states.async_set("test.object", "available")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user