Skip to content

Commit

Permalink
Translate exception messages in myUplink (home-assistant#131626)
Browse files Browse the repository at this point in the history
* Translate exceptions

* Add one more translation

* Adding more translations

* Make message easier to understand for end-user

* Clarify message

* Address review comments
  • Loading branch information
astrandb authored Dec 16, 2024
1 parent 14f4f8a commit 5adb7f4
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
20 changes: 16 additions & 4 deletions homeassistant/components/myuplink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions homeassistant/components/myuplink/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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

Expand Down Expand Up @@ -137,7 +137,13 @@ async def async_set_native_value(self, value: float) -> None:
)
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()
4 changes: 1 addition & 3 deletions homeassistant/components/myuplink/quality_scale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion homeassistant/components/myuplink/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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

Expand Down Expand Up @@ -86,7 +87,13 @@ async def async_select_option(self, option: str) -> None:
)
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()
20 changes: 20 additions & 0 deletions homeassistant/components/myuplink/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}."
}
}
}
8 changes: 6 additions & 2 deletions homeassistant/components/myuplink/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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

Expand Down Expand Up @@ -129,7 +129,11 @@ async def _async_turn_switch(self, mode: int) -> None:
)
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()

0 comments on commit 5adb7f4

Please sign in to comment.