2017-04-30 05:04:49 +00:00
|
|
|
"""The exceptions used by Home Assistant."""
|
2018-11-21 11:26:08 +00:00
|
|
|
from typing import Optional, Tuple, TYPE_CHECKING
|
2018-05-12 21:44:53 +00:00
|
|
|
import jinja2
|
2015-08-30 01:11:24 +00:00
|
|
|
|
2018-11-21 11:26:08 +00:00
|
|
|
# pylint: disable=using-constant-test
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
# pylint: disable=unused-import
|
|
|
|
from .core import Context # noqa
|
|
|
|
|
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."""
|
|
|
|
|
2018-05-12 21:44:53 +00:00
|
|
|
def __init__(self, exception: jinja2.TemplateError) -> None:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Init the error."""
|
2019-07-31 19:25:30 +00:00
|
|
|
super().__init__("{}: {}".format(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,
|
|
|
|
permission: Optional[Tuple[str]] = None,
|
|
|
|
) -> None:
|
2018-11-21 11:26:08 +00:00
|
|
|
"""Unauthorized error."""
|
|
|
|
super().__init__(self.__class__.__name__)
|
|
|
|
self.context = context
|
|
|
|
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-07-31 19:25:30 +00:00
|
|
|
super().__init__(self, "Service {}.{} not found".format(domain, service))
|
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."""
|
|
|
|
return "Unable to find service {}/{}".format(self.domain, self.service)
|