1 """The exceptions used by Home Assistant."""
3 from __future__
import annotations
5 from collections.abc
import Callable, Generator, Sequence
6 from dataclasses
import dataclass
7 from typing
import TYPE_CHECKING, Any
9 from .util.event_type
import EventType
12 from .core
import Context
15 _function_cache: dict[str, Callable[[str, str, dict[str, str] |
None], str]] = {}
19 Callable[[str, str, dict[str, str] |
None], str]
21 """Return a method that can fetch a translated exception message.
23 Defaults to English, requires translations to already be cached.
27 from .helpers.translation
import (
28 async_get_exception_message
as async_get_exception_message_import,
31 return async_get_exception_message_import
35 """General Home Assistant exception occurred."""
37 _message: str |
None =
None
38 generate_message: bool =
False
43 translation_domain: str |
None =
None,
44 translation_key: str |
None =
None,
45 translation_placeholders: dict[str, str] |
None =
None,
47 """Initialize exception."""
48 if not args
and translation_key
and translation_domain:
50 args = (translation_key,)
58 """Return exception message.
60 If no message was passed to `__init__`, the exception message is generated from
61 the translation_key. The message will be in English, regardless of the configured
76 if "async_get_exception_message" not in _function_cache:
77 _function_cache[
"async_get_exception_message"] = (
81 self.
_message_message = _function_cache[
"async_get_exception_message"](
88 """A validation exception occurred when validating the configuration."""
92 message_translation_key: str,
93 exceptions: list[Exception],
94 translation_domain: str |
None =
None,
95 translation_placeholders: dict[str, str] |
None =
None,
97 """Initialize exception."""
99 *(message_translation_key, exceptions),
100 translation_domain=translation_domain,
101 translation_key=message_translation_key,
102 translation_placeholders=translation_placeholders,
108 """A validation exception occurred when calling a service."""
111 class InvalidEntityFormatError(HomeAssistantError):
112 """When an invalid formatted entity is encountered."""
116 """When no entity is specified."""
120 """Error during template rendering."""
122 def __init__(self, exception: Exception | str) ->
None:
123 """Init the error."""
124 if isinstance(exception, str):
127 super().
__init__(f
"{exception.__class__.__name__}: {exception}")
130 @dataclass(slots=True)
132 """Error during condition evaluation."""
137 def _indent(indent: int, message: str) -> str:
138 """Return indentation."""
139 return " " * indent + message
141 def output(self, indent: int) -> Generator[str]:
142 """Yield an indented representation."""
143 raise NotImplementedError
146 """Return string representation."""
147 return "\n".join(
list(self.
outputoutput(indent=0)))
150 @dataclass(slots=True)
152 """Condition error message."""
157 def output(self, indent: int) -> Generator[str]:
158 """Yield an indented representation."""
159 yield self.
_indent_indent(indent, f
"In '{self.type}' condition: {self.message}")
162 @dataclass(slots=True)
164 """Condition error with index."""
171 error: ConditionError
173 def output(self, indent: int) -> Generator[str]:
174 """Yield an indented representation."""
177 indent, f
"In '{self.type}' (item {self.index+1} of {self.total}):"
180 yield self.
_indent_indent(indent, f
"In '{self.type}':")
182 yield from self.error.
output(indent + 1)
185 @dataclass(slots=True)
187 """Condition error with subconditions."""
190 errors: Sequence[ConditionError]
192 def output(self, indent: int) -> Generator[str]:
193 """Yield an indented representation."""
194 for item
in self.errors:
195 yield from item.output(indent)
199 """Base class for platform and config entry exceptions."""
202 """Return a human readable error."""
203 return super().
__str__()
or str(self.__cause__)
207 """Error to indicate that platform is not ready."""
210 class ConfigEntryError(IntegrationError):
211 """Error to indicate that config entry setup has failed."""
215 """Error to indicate that config entry is not ready."""
219 """Error to indicate that config entry could not authenticate."""
223 """When an invalid state is encountered."""
227 """When an action is unauthorized."""
231 context: Context |
None =
None,
232 user_id: str |
None =
None,
233 entity_id: str |
None =
None,
234 config_entry_id: str |
None =
None,
235 perm_category: str |
None =
None,
236 permission: str |
None =
None,
238 """Unauthorized error."""
239 super().
__init__(self.__class__.__name__)
242 if user_id
is None and context
is not None:
243 user_id = context.user_id
255 """When call is made with user ID that doesn't exist."""
258 class ServiceNotFound(ServiceValidationError):
259 """Raised when a service is not found."""
261 def __init__(self, domain: str, service: str) ->
None:
262 """Initialize error."""
264 translation_domain=
"homeassistant",
265 translation_key=
"service_not_found",
266 translation_placeholders={
"domain": domain,
"service": service},
274 """Raised when a property value has exceeded the max character length."""
277 self, value: EventType[Any] | str, property_name: str, max_length: int
279 """Initialize error."""
283 translation_domain=
"homeassistant",
284 translation_key=
"max_length_exceeded",
285 translation_placeholders={
287 "property_name": property_name,
288 "max_length":
str(max_length),
298 """Raised when dependencies cannot be setup."""
300 def __init__(self, failed_dependencies: list[str]) ->
None:
301 """Initialize error."""
303 f
"Could not setup dependencies: {', '.join(failed_dependencies)}",
Generator[str] output(self, int indent)
Generator[str] output(self, int indent)
Generator[str] output(self, int indent)
str _indent(int indent, str message)
Generator[str] output(self, int indent)
None __init__(self, str message_translation_key, list[Exception] exceptions, str|None translation_domain=None, dict[str, str]|None translation_placeholders=None)
None __init__(self, list[str] failed_dependencies)
None __init__(self, *object args, str|None translation_domain=None, str|None translation_key=None, dict[str, str]|None translation_placeholders=None)
None __init__(self, EventType[Any]|str value, str property_name, int max_length)
None __init__(self, str domain, str service)
None __init__(self, Exception|str exception)
None __init__(self, Context|None context=None, str|None user_id=None, str|None entity_id=None, str|None config_entry_id=None, str|None perm_category=None, str|None permission=None)
( Callable[[str, str, dict[str, str]|None], str]) import_async_get_exception_message()