Skip to content

Commit

Permalink
fix: token exception returns str instead of dict
Browse files Browse the repository at this point in the history
also add translation in spanish
  • Loading branch information
duhow committed Feb 8, 2024
1 parent ddeab92 commit 17bac09
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
21 changes: 20 additions & 1 deletion custom_components/aigues_barcelona/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,21 @@ async def validate_credentials(

except Exception:
_LOGGER.debug(f"Last data: {api.last_response}")
if not api.last_response:
return False

if (
api.last_response
isinstance(api.last_response, dict)
and api.last_response.get("path") == "recaptchaClientResponse"
):
raise RecaptchaAppeared

if (
isinstance(api.last_response, str)
and api.last_response == "JWT Token Revoked"
):
raise TokenExpired

return False


Expand Down Expand Up @@ -168,6 +178,11 @@ async def async_step_user(
self._abort_if_unique_id_configured()
except NotImplementedError:
errors["base"] = "not_implemented"
except TokenExpired:
errors["base"] = "token_expired"
return self.async_show_form(
step_id="token", data_schema=TOKEN_SCHEMA, errors=errors
)
except RecaptchaAppeared:
# Ask for OAuth Token to login.
return self.async_show_form(step_id="token", data_schema=TOKEN_SCHEMA)
Expand Down Expand Up @@ -198,6 +213,10 @@ class RecaptchaAppeared(HomeAssistantError):
issued."""


class TokenExpired(HomeAssistantError):
"""Error to indicate the OAuth token has expired."""


class InvalidAuth(HomeAssistantError):
"""Error to indicate credentials are invalid."""

Expand Down
3 changes: 2 additions & 1 deletion custom_components/aigues_barcelona/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
},
"error": {
"already_configured": "Error: Account is already configured",
"invalid_auth": "Invalid credentials"
"invalid_auth": "Invalid credentials",
"token_expired": "OAuth Token has expired, please issue a new one"
},
"step": {
"user": {
Expand Down
36 changes: 36 additions & 0 deletions custom_components/aigues_barcelona/translations/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"config": {
"abort": {
"already_configured": "Abort: Account is already configured",
"invalid_auth": "Invalid credentials"
},
"error": {
"already_configured": "Error: Account is already configured",
"invalid_auth": "Invalid credentials",
"token_expired": "El Token OAuth ha caducado, por favor genera uno nuevo"
},
"step": {
"user": {
"data": {
"username": "Usuario (DNI)",
"password": "Contrase\u00f1a"
},
"title": "Configurar integraci\u00f3n"
},
"token": {
"data": {
"token": "OAuth Token"
},
"title": "Introduce el Token OAuth",
"description": "Debido a que Aigues de Barcelona utiliza Recaptcha para iniciar sesi\u00f3n, no se puede iniciar sesi\u00f3n autom\u00e1ticamente desde Home Assistant, as\u00ed que tienes que iniciar sesi\u00f3n desde la web, y proporcionar el token.\nCopia y pega el token aqu\u00ed (empieza por ey....)"
},
"reauth_confirm": {
"data": {
"token": "OAuth Token"
},
"title": "Relogin - Introduce el Token OAuth",
"description": "Debido a que Aigues de Barcelona utiliza Recaptcha para iniciar sesi\u00f3n, no se puede iniciar sesi\u00f3n autom\u00e1ticamente desde Home Assistant, as\u00ed que tienes que iniciar sesi\u00f3n desde la web, y proporcionar el token.\nCopia y pega el token aqu\u00ed (empieza por ey....)"
}
}
}
}

0 comments on commit 17bac09

Please sign in to comment.