1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Use device area id in intent matching (#86678)

* Use device area id when matching

* Normalize whitespace in response

* Add extra test entity
This commit is contained in:
Michael Hansen
2023-01-26 09:48:49 -06:00
committed by GitHub
parent 38203003d2
commit adeaf746ec
3 changed files with 71 additions and 11 deletions

View File

@@ -20,7 +20,7 @@ from homeassistant.core import Context, HomeAssistant, State, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.loader import bind_hass
from . import area_registry, config_validation as cv, entity_registry
from . import area_registry, config_validation as cv, device_registry, entity_registry
_LOGGER = logging.getLogger(__name__)
_SlotsType = dict[str, Any]
@@ -159,6 +159,7 @@ def async_match_states(
states: Iterable[State] | None = None,
entities: entity_registry.EntityRegistry | None = None,
areas: area_registry.AreaRegistry | None = None,
devices: device_registry.DeviceRegistry | None = None,
) -> Iterable[State]:
"""Find states that match the constraints."""
if states is None:
@@ -206,11 +207,28 @@ def async_match_states(
assert area is not None, f"No area named {area_name}"
if area is not None:
if devices is None:
devices = device_registry.async_get(hass)
entity_area_ids: dict[str, str | None] = {}
for _state, entity in states_and_entities:
if entity is None:
continue
if entity.area_id:
# Use entity's area id first
entity_area_ids[entity.id] = entity.area_id
elif entity.device_id:
# Fall back to device area if not set on entity
device = devices.async_get(entity.device_id)
if device is not None:
entity_area_ids[entity.id] = device.area_id
# Filter by area
states_and_entities = [
(state, entity)
for state, entity in states_and_entities
if (entity is not None) and (entity.area_id == area.id)
if (entity is not None) and (entity_area_ids.get(entity.id) == area.id)
]
if name is not None: