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