Add translation to Tessie exceptions (#106525)

* Fix HomeAssistantError

* Add error translations

* Add cable error

* remove underscore from fallback

* Add more exceptions

* Try again for name

* Improve messages

* Address review feedback
This commit is contained in:
Brett Adams 2024-02-22 05:58:38 +10:00 committed by GitHub
parent a5ab2dfb62
commit ff0ba5361f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 2 deletions

View File

@ -53,7 +53,7 @@ class TessieEntity(CoordinatorEntity[TessieStateUpdateCoordinator]):
return self.coordinator.data.get(key or self.key, default)
async def run(
self, func: Callable[..., Awaitable[dict[str, bool | str]]], **kargs: Any
self, func: Callable[..., Awaitable[dict[str, Any]]], **kargs: Any
) -> None:
"""Run a tessie_api function and handle exceptions."""
try:
@ -66,8 +66,13 @@ class TessieEntity(CoordinatorEntity[TessieStateUpdateCoordinator]):
except ClientResponseError as e:
raise HomeAssistantError from e
if response["result"] is False:
name: str = getattr(self, "name", self.entity_id)
reason: str = response.get("reason", "unknown")
raise HomeAssistantError(
response.get("reason", "An unknown issue occurred")
reason.replace("_", " "),
translation_domain=DOMAIN,
translation_key=reason.replace(" ", "_"),
translation_placeholders={"name": name},
)
def set(self, *args: Any) -> None:

View File

@ -389,6 +389,24 @@
}
},
"exceptions": {
"unknown": {
"message": "An unknown issue occured changing {name}."
},
"not_supported": {
"message": "{name} is not supported."
},
"cable_connected": {
"message": "Charge cable is connected."
},
"already_active": {
"message": "{name} is already active."
},
"already_inactive": {
"message": "{name} is already inactive."
},
"incorrect_pin": {
"message": "Incorrect pin for {name}."
},
"no_cable": {
"message": "Insert cable to lock"
}