Translate exception messages in myUplink (#131626)

* Translate exceptions

* Add one more translation

* Adding more translations

* Make message easier to understand for end-user

* Clarify message

* Address review comments
This commit is contained in:
Åke Strandberg 2024-12-16 15:42:15 +01:00 committed by GitHub
parent 14f4f8aeb5
commit 5adb7f4542
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 59 additions and 12 deletions

View File

@ -55,13 +55,25 @@ async def async_setup_entry(
await auth.async_get_access_token()
except ClientResponseError as err:
if err.status in {HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN}:
raise ConfigEntryAuthFailed from err
raise ConfigEntryNotReady from err
raise ConfigEntryAuthFailed(
translation_domain=DOMAIN,
translation_key="config_entry_auth_failed",
) from err
raise ConfigEntryNotReady(
translation_domain=DOMAIN,
translation_key="config_entry_not_ready",
) from err
except ClientError as err:
raise ConfigEntryNotReady from err
raise ConfigEntryNotReady(
translation_domain=DOMAIN,
translation_key="config_entry_not_ready",
) from err
if set(config_entry.data["token"]["scope"].split(" ")) != set(OAUTH2_SCOPES):
raise ConfigEntryAuthFailed("Incorrect OAuth2 scope")
raise ConfigEntryAuthFailed(
translation_domain=DOMAIN,
translation_key="incorrect_oauth2_scope",
)
# Setup MyUplinkAPI and coordinator for data fetch
api = MyUplinkAPI(auth)

View File

@ -10,7 +10,7 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import MyUplinkConfigEntry, MyUplinkDataCoordinator
from .const import F_SERIES
from .const import DOMAIN, F_SERIES
from .entity import MyUplinkEntity
from .helpers import find_matching_platform, skip_entity, transform_model_series
@ -137,7 +137,13 @@ class MyUplinkNumber(MyUplinkEntity, NumberEntity):
)
except ClientError as err:
raise HomeAssistantError(
f"Failed to set new value {value} for {self.point_id}/{self.entity_id}"
translation_domain=DOMAIN,
translation_key="set_number_error",
translation_placeholders={
"entity": self.entity_id,
"point": self.point_id,
"value": str(value),
},
) from err
await self.coordinator.async_request_refresh()

View File

@ -78,9 +78,7 @@ rules:
It is not feasible to use the API names as translation keys as they can change between
firmware and API upgrades and the number of appliance models and firmware releases are huge.
Entity names translations are therefore not implemented for the time being.
exception-translations:
status: todo
comment: PR pending review \#191937
exception-translations: done
icon-translations: done
reconfiguration-flow: done
repair-issues:

View File

@ -12,6 +12,7 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import MyUplinkConfigEntry, MyUplinkDataCoordinator
from .const import DOMAIN
from .entity import MyUplinkEntity
from .helpers import find_matching_platform, skip_entity
@ -86,7 +87,13 @@ class MyUplinkSelect(MyUplinkEntity, SelectEntity):
)
except ClientError as err:
raise HomeAssistantError(
f"Failed to set new option {self.options_rev[option]} for {self.point_id}/{self.entity_id}"
translation_domain=DOMAIN,
translation_key="set_select_error",
translation_placeholders={
"entity": self.entity_id,
"option": self.options_rev[option],
"point": self.point_id,
},
) from err
await self.coordinator.async_request_refresh()

View File

@ -42,5 +42,25 @@
"name": "Status"
}
}
},
"exceptions": {
"config_entry_auth_failed": {
"message": "Error while logging in to the API. Please check your credentials."
},
"config_entry_not_ready": {
"message": "Error while loading the integration."
},
"incorrect_oauth2_scope": {
"message": "Stored permissions are invalid. Please login again to update permissions."
},
"set_number_error": {
"message": "Failed to set new value {value} for {point}/{entity}."
},
"set_select_error": {
"message": "Failed to set new option {option} for {point}/{entity}."
},
"set_switch_error": {
"message": "Failed to set state for {entity}."
}
}
}

View File

@ -12,7 +12,7 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import MyUplinkConfigEntry, MyUplinkDataCoordinator
from .const import F_SERIES
from .const import DOMAIN, F_SERIES
from .entity import MyUplinkEntity
from .helpers import find_matching_platform, skip_entity, transform_model_series
@ -129,7 +129,11 @@ class MyUplinkDevicePointSwitch(MyUplinkEntity, SwitchEntity):
)
except aiohttp.ClientError as err:
raise HomeAssistantError(
f"Failed to set state for {self.entity_id}"
translation_domain=DOMAIN,
translation_key="set_switch_error",
translation_placeholders={
"entity": self.entity_id,
},
) from err
await self.coordinator.async_request_refresh()