mirror of
https://github.com/home-assistant/core.git
synced 2025-11-09 02:49:40 +00:00
Add ServiceValidationError and translation support (#102592)
* Add ServiceValidationError * Add translation support * Extend translation support to HomeAssistantError * Add translation support for ServiceNotFound exc * Frontend translation & translation_key from caller * Improve fallback message * Set websocket_api as default translation_domain * Add MQTT ServiceValidationError exception * Follow up comments * Revert removing gueard on translation_key * Revert test changes to fix CI test * Follow up comments * Fix CI test * Follow up * Improve language * Follow up comment
This commit is contained in:
@@ -12,6 +12,23 @@ if TYPE_CHECKING:
|
||||
class HomeAssistantError(Exception):
|
||||
"""General Home Assistant exception occurred."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*args: object,
|
||||
translation_domain: str | None = None,
|
||||
translation_key: str | None = None,
|
||||
translation_placeholders: dict[str, str] | None = None,
|
||||
) -> None:
|
||||
"""Initialize exception."""
|
||||
super().__init__(*args)
|
||||
self.translation_domain = translation_domain
|
||||
self.translation_key = translation_key
|
||||
self.translation_placeholders = translation_placeholders
|
||||
|
||||
|
||||
class ServiceValidationError(HomeAssistantError):
|
||||
"""A validation exception occurred when calling a service."""
|
||||
|
||||
|
||||
class InvalidEntityFormatError(HomeAssistantError):
|
||||
"""When an invalid formatted entity is encountered."""
|
||||
@@ -165,13 +182,19 @@ class ServiceNotFound(HomeAssistantError):
|
||||
|
||||
def __init__(self, domain: str, service: str) -> None:
|
||||
"""Initialize error."""
|
||||
super().__init__(self, f"Service {domain}.{service} not found")
|
||||
super().__init__(
|
||||
self,
|
||||
f"Service {domain}.{service} not found.",
|
||||
translation_domain="homeassistant",
|
||||
translation_key="service_not_found",
|
||||
translation_placeholders={"domain": domain, "service": service},
|
||||
)
|
||||
self.domain = domain
|
||||
self.service = service
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""Return string representation."""
|
||||
return f"Unable to find service {self.domain}.{self.service}"
|
||||
return f"Service {self.domain}.{self.service} not found."
|
||||
|
||||
|
||||
class MaxLengthExceeded(HomeAssistantError):
|
||||
|
||||
Reference in New Issue
Block a user