mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Fix area_id and area_name template functions (#55470)
This commit is contained in:
parent
1c01ff401f
commit
1d1b5ab345
@ -957,6 +957,7 @@ def area_id(hass: HomeAssistant, lookup_value: str) -> str | None:
|
|||||||
return area.id
|
return area.id
|
||||||
|
|
||||||
ent_reg = entity_registry.async_get(hass)
|
ent_reg = entity_registry.async_get(hass)
|
||||||
|
dev_reg = device_registry.async_get(hass)
|
||||||
# Import here, not at top-level to avoid circular import
|
# Import here, not at top-level to avoid circular import
|
||||||
from homeassistant.helpers import ( # pylint: disable=import-outside-toplevel
|
from homeassistant.helpers import ( # pylint: disable=import-outside-toplevel
|
||||||
config_validation as cv,
|
config_validation as cv,
|
||||||
@ -968,10 +969,14 @@ def area_id(hass: HomeAssistant, lookup_value: str) -> str | None:
|
|||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if entity := ent_reg.async_get(lookup_value):
|
if entity := ent_reg.async_get(lookup_value):
|
||||||
return entity.area_id
|
# If entity has an area ID, return that
|
||||||
|
if entity.area_id:
|
||||||
|
return entity.area_id
|
||||||
|
# If entity has a device ID, return the area ID for the device
|
||||||
|
if entity.device_id and (device := dev_reg.async_get(entity.device_id)):
|
||||||
|
return device.area_id
|
||||||
|
|
||||||
# Check if this could be a device ID (hex string)
|
# Check if this could be a device ID
|
||||||
dev_reg = device_registry.async_get(hass)
|
|
||||||
if device := dev_reg.async_get(lookup_value):
|
if device := dev_reg.async_get(lookup_value):
|
||||||
return device.area_id
|
return device.area_id
|
||||||
|
|
||||||
@ -992,6 +997,7 @@ def area_name(hass: HomeAssistant, lookup_value: str) -> str | None:
|
|||||||
if area:
|
if area:
|
||||||
return area.name
|
return area.name
|
||||||
|
|
||||||
|
dev_reg = device_registry.async_get(hass)
|
||||||
ent_reg = entity_registry.async_get(hass)
|
ent_reg = entity_registry.async_get(hass)
|
||||||
# Import here, not at top-level to avoid circular import
|
# Import here, not at top-level to avoid circular import
|
||||||
from homeassistant.helpers import ( # pylint: disable=import-outside-toplevel
|
from homeassistant.helpers import ( # pylint: disable=import-outside-toplevel
|
||||||
@ -1004,11 +1010,18 @@ def area_name(hass: HomeAssistant, lookup_value: str) -> str | None:
|
|||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if entity := ent_reg.async_get(lookup_value):
|
if entity := ent_reg.async_get(lookup_value):
|
||||||
|
# If entity has an area ID, get the area name for that
|
||||||
if entity.area_id:
|
if entity.area_id:
|
||||||
return _get_area_name(area_reg, entity.area_id)
|
return _get_area_name(area_reg, entity.area_id)
|
||||||
return None
|
# If entity has a device ID and the device exists with an area ID, get the
|
||||||
|
# area name for that
|
||||||
|
if (
|
||||||
|
entity.device_id
|
||||||
|
and (device := dev_reg.async_get(entity.device_id))
|
||||||
|
and device.area_id
|
||||||
|
):
|
||||||
|
return _get_area_name(area_reg, device.area_id)
|
||||||
|
|
||||||
dev_reg = device_registry.async_get(hass)
|
|
||||||
if (device := dev_reg.async_get(lookup_value)) and device.area_id:
|
if (device := dev_reg.async_get(lookup_value)) and device.area_id:
|
||||||
return _get_area_name(area_reg, device.area_id)
|
return _get_area_name(area_reg, device.area_id)
|
||||||
|
|
||||||
|
@ -1828,6 +1828,16 @@ async def test_area_id(hass):
|
|||||||
assert_result_info(info, area_entry_entity_id.id)
|
assert_result_info(info, area_entry_entity_id.id)
|
||||||
assert info.rate_limit is None
|
assert info.rate_limit is None
|
||||||
|
|
||||||
|
# Make sure that when entity doesn't have an area but its device does, that's what
|
||||||
|
# gets returned
|
||||||
|
entity_entry = entity_registry.async_update_entity(
|
||||||
|
entity_entry.entity_id, area_id=area_entry_entity_id.id
|
||||||
|
)
|
||||||
|
|
||||||
|
info = render_to_info(hass, f"{{{{ area_id('{entity_entry.entity_id}') }}}}")
|
||||||
|
assert_result_info(info, area_entry_entity_id.id)
|
||||||
|
assert info.rate_limit is None
|
||||||
|
|
||||||
|
|
||||||
async def test_area_name(hass):
|
async def test_area_name(hass):
|
||||||
"""Test area_name function."""
|
"""Test area_name function."""
|
||||||
@ -1897,6 +1907,16 @@ async def test_area_name(hass):
|
|||||||
assert_result_info(info, area_entry.name)
|
assert_result_info(info, area_entry.name)
|
||||||
assert info.rate_limit is None
|
assert info.rate_limit is None
|
||||||
|
|
||||||
|
# Make sure that when entity doesn't have an area but its device does, that's what
|
||||||
|
# gets returned
|
||||||
|
entity_entry = entity_registry.async_update_entity(
|
||||||
|
entity_entry.entity_id, area_id=None
|
||||||
|
)
|
||||||
|
|
||||||
|
info = render_to_info(hass, f"{{{{ area_name('{entity_entry.entity_id}') }}}}")
|
||||||
|
assert_result_info(info, area_entry.name)
|
||||||
|
assert info.rate_limit is None
|
||||||
|
|
||||||
|
|
||||||
def test_closest_function_to_coord(hass):
|
def test_closest_function_to_coord(hass):
|
||||||
"""Test closest function to coord."""
|
"""Test closest function to coord."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user