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

Use exposed error messages in Assist (#127503)

* Use exposed error messages

* Report expose errors

* Remove comment

* Relative import

* Rework expose check logic

* Delay creation of all names list, and skip config/hidden entities

* Clean up commented code and type issue

* Fix test

* Move assistant check
This commit is contained in:
Michael Hansen
2024-10-11 12:06:03 -05:00
committed by GitHub
parent ba6bcf86ca
commit 67e0ccf677
5 changed files with 780 additions and 93 deletions

View File

@@ -351,6 +351,7 @@ class MatchTargetsCandidate:
"""Candidate for async_match_targets."""
state: State
is_exposed: bool
entity: entity_registry.RegistryEntry | None = None
area: area_registry.AreaEntry | None = None
floor: floor_registry.FloorEntry | None = None
@@ -514,29 +515,31 @@ def async_match_targets( # noqa: C901
if not states:
return MatchTargetsResult(False, MatchFailedReason.DOMAIN)
if constraints.assistant:
# Filter by exposure
states = [
s
for s in states
if async_should_expose(hass, constraints.assistant, s.entity_id)
]
if not states:
return MatchTargetsResult(False, MatchFailedReason.ASSISTANT)
candidates = [
MatchTargetsCandidate(
state=state,
is_exposed=(
async_should_expose(hass, constraints.assistant, state.entity_id)
if constraints.assistant
else True
),
)
for state in states
]
if constraints.domains and (not filtered_by_domain):
# Filter by domain (if we didn't already do it)
states = [s for s in states if s.domain in constraints.domains]
if not states:
candidates = [c for c in candidates if c.state.domain in constraints.domains]
if not candidates:
return MatchTargetsResult(False, MatchFailedReason.DOMAIN)
if constraints.states:
# Filter by state
states = [s for s in states if s.state in constraints.states]
if not states:
candidates = [c for c in candidates if c.state.state in constraints.states]
if not candidates:
return MatchTargetsResult(False, MatchFailedReason.STATE)
# Exit early so we can avoid registry lookups
# Try to exit early so we can avoid registry lookups
if not (
constraints.name
or constraints.features
@@ -544,11 +547,18 @@ def async_match_targets( # noqa: C901
or constraints.area_name
or constraints.floor_name
):
return MatchTargetsResult(True, states=states)
if constraints.assistant:
# Check exposure
candidates = [c for c in candidates if c.is_exposed]
if not candidates:
return MatchTargetsResult(False, MatchFailedReason.ASSISTANT)
return MatchTargetsResult(True, states=[c.state for c in candidates])
# We need entity registry entries now
er = entity_registry.async_get(hass)
candidates = [MatchTargetsCandidate(s, er.async_get(s.entity_id)) for s in states]
for candidate in candidates:
candidate.entity = er.async_get(candidate.state.entity_id)
if constraints.name:
# Filter by entity name or alias
@@ -637,6 +647,12 @@ def async_match_targets( # noqa: C901
False, MatchFailedReason.AREA, areas=targeted_areas
)
if constraints.assistant:
# Check exposure
candidates = [c for c in candidates if c.is_exposed]
if not candidates:
return MatchTargetsResult(False, MatchFailedReason.ASSISTANT)
if constraints.name and (not constraints.allow_duplicate_names):
# Check for duplicates
if not areas_added: