Add support for expanding zones in templates (#69793)

pull/69853/head
Franck Nijhof 2022-04-11 12:59:45 +02:00 committed by GitHub
parent b325c112b4
commit 6524dd8fb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 3 deletions

View File

@ -33,6 +33,7 @@ from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_LATITUDE,
ATTR_LONGITUDE,
ATTR_PERSONS,
ATTR_UNIT_OF_MEASUREMENT,
LENGTH_METERS,
STATE_UNKNOWN,
@ -76,6 +77,7 @@ _IS_NUMERIC = re.compile(r"^[+-]?(?!0\d)\d*(?:\.\d*)?$")
_RESERVED_NAMES = {"contextfunction", "evalcontextfunction", "environmentfunction"}
_GROUP_DOMAIN_PREFIX = "group."
_ZONE_DOMAIN_PREFIX = "zone."
_COLLECTABLE_STATE_ATTRIBUTES = {
"state",
@ -886,7 +888,7 @@ def result_as_boolean(template_result: Any | None) -> bool:
def expand(hass: HomeAssistant, *args: Any) -> Iterable[State]:
"""Expand out any groups into entity states."""
"""Expand out any groups and zones into entity states."""
# circular import.
from . import entity as entity_helper # pylint: disable=import-outside-toplevel
@ -912,9 +914,11 @@ def expand(hass: HomeAssistant, *args: Any) -> Iterable[State]:
and source["domain"] == "group"
):
# Collect state will be called in here since it's wrapped
group_entities = entity.attributes.get(ATTR_ENTITY_ID)
if group_entities:
if group_entities := entity.attributes.get(ATTR_ENTITY_ID):
search += group_entities
elif entity_id.startswith(_ZONE_DOMAIN_PREFIX):
if zone_entities := entity.attributes.get(ATTR_PERSONS):
search += zone_entities
else:
_collect_state(hass, entity_id)
found[entity_id] = entity

View File

@ -2105,6 +2105,58 @@ async def test_expand(hass):
["light.grouped", "light.first", "light.second"],
)
assert await async_setup_component(
hass,
"zone",
{
"zone": {
"name": "Test",
"latitude": 32.880837,
"longitude": -117.237561,
"radius": 250,
"passive": False,
}
},
)
info = render_to_info(
hass, "{{ expand('zone.test') | map(attribute='entity_id') | join(', ') }}"
)
assert_result_info(
info,
"",
["zone.test"],
)
hass.states.async_set(
"person.person1",
"test",
)
await hass.async_block_till_done()
info = render_to_info(
hass, "{{ expand('zone.test') | map(attribute='entity_id') | join(', ') }}"
)
assert_result_info(
info,
"person.person1",
["zone.test", "person.person1"],
)
hass.states.async_set(
"person.person2",
"test",
)
await hass.async_block_till_done()
info = render_to_info(
hass, "{{ expand('zone.test') | map(attribute='entity_id') | join(', ') }}"
)
assert_result_info(
info,
"person.person1, person.person2",
["zone.test", "person.person1", "person.person2"],
)
async def test_device_entities(hass):
"""Test device_entities function."""