Skip to content

Commit

Permalink
Better handling of timeout errors (JohNan#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohNan authored Mar 21, 2023
1 parent c6d25b3 commit 35848ce
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 7 deletions.
8 changes: 6 additions & 2 deletions custom_components/wellbeing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ def __init__(self, hass: HomeAssistant, client: WellbeingApiClient, update_inter
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=update_interval)

async def async_login(self) -> bool:
"""Login to Verisure."""
"""Login to Wellbeing."""
try:
await self.api.async_login()
except Exception as ex:
_LOGGER.error("Could not log in to WellBeing, %s", ex)
_LOGGER.error(
"Could not log in to WellBeing, %s. Will try again after %d",
ex,
self.update_interval.seconds
)
return False

return True
Expand Down
9 changes: 4 additions & 5 deletions custom_components/wellbeing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,17 +404,14 @@ async def api_wrapper(self, method: str, url: str, data: dict = {}, headers: dic
if method == "get":
response = await self._session.get(url, headers=headers)
return await response.json()

elif method == "put":
response = await self._session.put(url, headers=headers, json=data)
return await response.json()

elif method == "patch":
await self._session.patch(url, headers=headers, json=data)

elif method == "post":
response = await self._session.post(url, headers=headers, json=data)
return await response.json()
else:
raise Exception("Unsupported http method '%s'" % method)

except asyncio.TimeoutError as exception:
_LOGGER.error(
Expand All @@ -437,3 +434,5 @@ async def api_wrapper(self, method: str, url: str, data: dict = {}, headers: dic
)
except Exception as exception: # pylint: disable=broad-except
_LOGGER.error("Something really wrong happened! - %s", exception)

return {}
30 changes: 30 additions & 0 deletions custom_components/wellbeing/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
"""Adds config flow for Wellbeing."""
from typing import Mapping, Any

import voluptuous as vol
import logging

import homeassistant.helpers.config_validation as cv
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from .api import WellbeingApiClient
from .const import CONF_PASSWORD, CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
from .const import CONF_USERNAME
from .const import DOMAIN

_LOGGER: logging.Logger = logging.getLogger(__package__)

class WellbeingFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for wellbeing."""
Expand Down Expand Up @@ -40,6 +45,31 @@ async def async_step_user(self, user_input=None):

return await self._show_config_form(user_input)

async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
"""Handle configuration by re-auth."""
return await self.async_step_reauth_validate()

async def async_step_reauth_validate(self, user_input=None):
"""Handle reauth and validation."""
errors = {}
if user_input is not None:
return await self._test_credentials(
user_input[CONF_USERNAME], user_input[CONF_PASSWORD]
)

return self.async_show_form(
step_id="reauth_validate",
data_schema=vol.Schema(
{
vol.Required(CONF_PASSWORD): str,
}
),
errors=errors,
description_placeholders={
CONF_USERNAME: user_input[CONF_USERNAME],
},
)

@staticmethod
@callback
def async_get_options_flow(config_entry):
Expand Down
7 changes: 7 additions & 0 deletions custom_components/wellbeing/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
"username": "Username",
"password": "Password"
}
},
"reauth_validate": {
"data": {
"password": "Password"
},
"description": "Enter the password for {username}.",
"title": "Reauthenticate an August account"
}
},
"error": {
Expand Down
7 changes: 7 additions & 0 deletions custom_components/wellbeing/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
"username": "Identifiant",
"password": "Mot de Passe"
}
},
"reauth_validate": {
"data": {
"password": "Password"
},
"description": "Enter the password for {username}.",
"title": "Reauthenticate an August account"
}
},
"error": {
Expand Down
7 changes: 7 additions & 0 deletions custom_components/wellbeing/translations/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
"username": "Brukernavn",
"password": "Passord"
}
},
"reauth_validate": {
"data": {
"password": "Password"
},
"description": "Enter the password for {username}.",
"title": "Reauthenticate an August account"
}
},
"error": {
Expand Down
7 changes: 7 additions & 0 deletions custom_components/wellbeing/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
"username": "Nazwa użytkownika",
"password": "Hasło"
}
},
"reauth_validate": {
"data": {
"password": "Password"
},
"description": "Enter the password for {username}.",
"title": "Reauthenticate an August account"
}
},
"error": {
Expand Down

0 comments on commit 35848ce

Please sign in to comment.