Skip to content

Commit

Permalink
Add list areas function to template (home-assistant#88441)
Browse files Browse the repository at this point in the history
  • Loading branch information
rokam authored Mar 9, 2023
1 parent 48fca3b commit eed16dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions homeassistant/helpers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,12 @@ def is_device_attr(
return bool(device_attr(hass, device_or_entity_id, attr_name) == attr_value)


def areas(hass: HomeAssistant) -> Iterable[str | None]:
"""Return all areas."""
area_reg = area_registry.async_get(hass)
return [area.id for area in area_reg.async_list_areas()]


def area_id(hass: HomeAssistant, lookup_value: str) -> str | None:
"""Get the area ID from an area name, device id, or entity id."""
area_reg = area_registry.async_get(hass)
Expand Down Expand Up @@ -2183,6 +2189,9 @@ def wrapper(_: Any, *args: _P.args, **kwargs: _P.kwargs) -> _R:
self.globals["device_id"] = hassfunction(device_id)
self.filters["device_id"] = pass_context(self.globals["device_id"])

self.globals["areas"] = hassfunction(areas)
self.filters["areas"] = pass_context(self.globals["areas"])

self.globals["area_id"] = hassfunction(area_id)
self.filters["area_id"] = pass_context(self.globals["area_id"])

Expand Down
20 changes: 20 additions & 0 deletions tests/helpers/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2851,6 +2851,26 @@ async def test_device_attr(
assert info.rate_limit is None


async def test_areas(hass: HomeAssistant, area_registry: ar.AreaRegistry) -> None:
"""Test areas function."""
# Test no areas
info = render_to_info(hass, "{{ areas() }}")
assert_result_info(info, [])
assert info.rate_limit is None

# Test one area
area1 = area_registry.async_get_or_create("area1")
info = render_to_info(hass, "{{ areas() }}")
assert_result_info(info, [area1.id])
assert info.rate_limit is None

# Test multiple areas
area2 = area_registry.async_get_or_create("area2")
info = render_to_info(hass, "{{ areas() }}")
assert_result_info(info, [area1.id, area2.id])
assert info.rate_limit is None


async def test_area_id(
hass: HomeAssistant,
area_registry: ar.AreaRegistry,
Expand Down

0 comments on commit eed16dc

Please sign in to comment.