From 4459dce73ae2a4ce4bf0178d7e945913938e85d5 Mon Sep 17 00:00:00 2001 From: Artur Pragacz <49985303+arturpragacz@users.noreply.github.com> Date: Sat, 14 Mar 2026 00:58:19 +0100 Subject: [PATCH] Reorder code to group intent errors (#165431) --- homeassistant/helpers/intent.py | 138 ++++++++++++++++---------------- 1 file changed, 69 insertions(+), 69 deletions(-) diff --git a/homeassistant/helpers/intent.py b/homeassistant/helpers/intent.py index b6d6cd5ad95..d85f505c0e5 100644 --- a/homeassistant/helpers/intent.py +++ b/homeassistant/helpers/intent.py @@ -184,6 +184,52 @@ class IntentUnexpectedError(IntentError): """Unexpected error while handling intent.""" +class MatchFailedError(IntentError): + """Error when target matching fails.""" + + def __init__( + self, + result: MatchTargetsResult, + constraints: MatchTargetsConstraints, + preferences: MatchTargetsPreferences | None = None, + ) -> None: + """Initialize error.""" + super().__init__() + + self.result = result + self.constraints = constraints + self.preferences = preferences + + def __str__(self) -> str: + """Return string representation.""" + return f"" + + +class NoStatesMatchedError(MatchFailedError): + """Error when no states match the intent's constraints.""" + + def __init__( + self, + reason: MatchFailedReason, + name: str | None = None, + area: str | None = None, + floor: str | None = None, + domains: set[str] | None = None, + device_classes: set[str] | None = None, + ) -> None: + """Initialize error.""" + super().__init__( + result=MatchTargetsResult(False, reason), + constraints=MatchTargetsConstraints( + name=name, + area_name=area, + floor_name=floor, + domains=domains, + device_classes=device_classes, + ), + ) + + class MatchFailedReason(Enum): """Possible reasons for match failure in async_match_targets.""" @@ -232,6 +278,29 @@ class MatchFailedReason(Enum): ) +@dataclass +class MatchTargetsResult: + """Result from async_match_targets.""" + + is_match: bool + """True if one or more entities matched.""" + + no_match_reason: MatchFailedReason | None = None + """Reason for failed match when is_match = False.""" + + states: list[State] = field(default_factory=list) + """List of matched entity states.""" + + no_match_name: str | None = None + """Name of invalid area/floor or duplicate name when match fails for those reasons.""" + + areas: list[ar.AreaEntry] = field(default_factory=list) + """Areas that were targeted.""" + + floors: list[fr.FloorEntry] = field(default_factory=list) + """Floors that were targeted.""" + + @dataclass class MatchTargetsConstraints: """Constraints for async_match_targets.""" @@ -292,75 +361,6 @@ class MatchTargetsPreferences: """Id of floor to use when deduplicating names.""" -@dataclass -class MatchTargetsResult: - """Result from async_match_targets.""" - - is_match: bool - """True if one or more entities matched.""" - - no_match_reason: MatchFailedReason | None = None - """Reason for failed match when is_match = False.""" - - states: list[State] = field(default_factory=list) - """List of matched entity states.""" - - no_match_name: str | None = None - """Name of invalid area/floor or duplicate name when match fails for those reasons.""" - - areas: list[ar.AreaEntry] = field(default_factory=list) - """Areas that were targeted.""" - - floors: list[fr.FloorEntry] = field(default_factory=list) - """Floors that were targeted.""" - - -class MatchFailedError(IntentError): - """Error when target matching fails.""" - - def __init__( - self, - result: MatchTargetsResult, - constraints: MatchTargetsConstraints, - preferences: MatchTargetsPreferences | None = None, - ) -> None: - """Initialize error.""" - super().__init__() - - self.result = result - self.constraints = constraints - self.preferences = preferences - - def __str__(self) -> str: - """Return string representation.""" - return f"" - - -class NoStatesMatchedError(MatchFailedError): - """Error when no states match the intent's constraints.""" - - def __init__( - self, - reason: MatchFailedReason, - name: str | None = None, - area: str | None = None, - floor: str | None = None, - domains: set[str] | None = None, - device_classes: set[str] | None = None, - ) -> None: - """Initialize error.""" - super().__init__( - result=MatchTargetsResult(False, reason), - constraints=MatchTargetsConstraints( - name=name, - area_name=area, - floor_name=floor, - domains=domains, - device_classes=device_classes, - ), - ) - - @dataclass class MatchTargetsCandidate: """Candidate for async_match_targets."""