2017-04-30 07:04:49 +02:00
|
|
|
"""The exceptions used by Home Assistant."""
|
2020-06-06 21:34:56 +03:00
|
|
|
from typing import TYPE_CHECKING, Optional
|
2019-12-09 16:42:10 +01:00
|
|
|
|
2018-11-21 12:26:08 +01:00
|
|
|
if TYPE_CHECKING:
|
2019-11-26 08:40:08 +02:00
|
|
|
from .core import Context # noqa: F401 pylint: disable=unused-import
|
2018-11-21 12:26:08 +01:00
|
|
|
|
2015-08-29 22:34:35 -04:00
|
|
|
|
2015-08-29 21:11:24 -04:00
|
|
|
class HomeAssistantError(Exception):
|
2016-03-08 00:06:04 +01:00
|
|
|
"""General Home Assistant exception occurred."""
|
|
|
|
|
2015-08-29 21:11:24 -04:00
|
|
|
|
|
|
|
class InvalidEntityFormatError(HomeAssistantError):
|
2016-03-08 00:06:04 +01:00
|
|
|
"""When an invalid formatted entity is encountered."""
|
|
|
|
|
2015-08-29 21:11:24 -04:00
|
|
|
|
|
|
|
class NoEntitySpecifiedError(HomeAssistantError):
|
2016-03-08 00:06:04 +01:00
|
|
|
"""When no entity is specified."""
|
|
|
|
|
2015-12-11 19:07:03 -08:00
|
|
|
|
|
|
|
class TemplateError(HomeAssistantError):
|
2016-03-08 00:06:04 +01:00
|
|
|
"""Error during template rendering."""
|
|
|
|
|
2020-10-12 09:38:24 -05:00
|
|
|
def __init__(self, exception: Exception) -> None:
|
2017-04-30 07:04:49 +02:00
|
|
|
"""Init the error."""
|
2019-08-23 18:53:33 +02:00
|
|
|
super().__init__(f"{exception.__class__.__name__}: {exception}")
|
2017-06-26 09:41:48 -07:00
|
|
|
|
|
|
|
|
|
|
|
class PlatformNotReady(HomeAssistantError):
|
|
|
|
"""Error to indicate that platform is not ready."""
|
|
|
|
|
2017-10-25 18:05:30 +02:00
|
|
|
|
2018-10-04 15:53:50 +02:00
|
|
|
class ConfigEntryNotReady(HomeAssistantError):
|
|
|
|
"""Error to indicate that config entry is not ready."""
|
|
|
|
|
|
|
|
|
2017-10-25 18:05:30 +02:00
|
|
|
class InvalidStateError(HomeAssistantError):
|
|
|
|
"""When an invalid state is encountered."""
|
|
|
|
|
2018-11-21 12:26:08 +01:00
|
|
|
|
|
|
|
class Unauthorized(HomeAssistantError):
|
|
|
|
"""When an action is unauthorized."""
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
context: Optional["Context"] = None,
|
|
|
|
user_id: Optional[str] = None,
|
|
|
|
entity_id: Optional[str] = None,
|
|
|
|
config_entry_id: Optional[str] = None,
|
|
|
|
perm_category: Optional[str] = None,
|
2020-06-06 21:34:56 +03:00
|
|
|
permission: Optional[str] = None,
|
2019-07-31 12:25:30 -07:00
|
|
|
) -> None:
|
2018-11-21 12:26:08 +01:00
|
|
|
"""Unauthorized error."""
|
|
|
|
super().__init__(self.__class__.__name__)
|
|
|
|
self.context = context
|
2020-08-19 14:57:38 +02:00
|
|
|
|
|
|
|
if user_id is None and context is not None:
|
|
|
|
user_id = context.user_id
|
|
|
|
|
2018-11-21 12:26:08 +01:00
|
|
|
self.user_id = user_id
|
|
|
|
self.entity_id = entity_id
|
2018-12-13 15:30:20 +01:00
|
|
|
self.config_entry_id = config_entry_id
|
|
|
|
# Not all actions have an ID (like adding config entry)
|
|
|
|
# We then use this fallback to know what category was unauth
|
|
|
|
self.perm_category = perm_category
|
2018-11-21 12:26:08 +01:00
|
|
|
self.permission = permission
|
|
|
|
|
|
|
|
|
|
|
|
class UnknownUser(Unauthorized):
|
|
|
|
"""When call is made with user ID that doesn't exist."""
|
2018-11-30 21:28:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ServiceNotFound(HomeAssistantError):
|
|
|
|
"""Raised when a service is not found."""
|
|
|
|
|
|
|
|
def __init__(self, domain: str, service: str) -> None:
|
|
|
|
"""Initialize error."""
|
2019-08-23 18:53:33 +02:00
|
|
|
super().__init__(self, f"Service {domain}.{service} not found")
|
2018-11-30 21:28:35 +01:00
|
|
|
self.domain = domain
|
|
|
|
self.service = service
|
2019-05-14 07:09:11 +02:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
"""Return string representation."""
|
2020-12-02 10:32:25 +01:00
|
|
|
return f"Unable to find service {self.domain}.{self.service}"
|