Skip to content

Commit 6c54b18

Browse files
committed
Bug fix for reauthentication #28
1 parent 8e78c46 commit 6c54b18

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

custom_components/wemportal/coordinator.py

+24-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
DataUpdateCoordinator,
99
UpdateFailed,
1010
)
11-
from .exceptions import WemPortalError
11+
from .exceptions import ServerError, WemPortalError
1212
from .const import _LOGGER, DEFAULT_TIMEOUT
1313
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
1414
from .wemportalapi import WemPortalApi
@@ -41,16 +41,26 @@ async def _async_update_data(self):
4141
try:
4242
return await self.hass.async_add_executor_job(self.api.fetch_data)
4343
except WemPortalError as exc:
44-
_LOGGER.error("Error fetching data from wemportal", exc_info=exc)
45-
_LOGGER.error("Creating new wemportal api instance")
46-
# TODO: This is a temporary solution and should be removed when api cause from #28 is resolved
47-
try:
48-
new_api = WemPortalApi(
49-
self.config_entry.data.get(CONF_USERNAME),
50-
self.config_entry.data.get(CONF_PASSWORD),
51-
self.config_entry.options,
52-
)
53-
self.api = new_api
54-
except Exception:
55-
pass
56-
raise UpdateFailed from exc
44+
45+
if isinstance(exc.__cause__, ServerError):
46+
_LOGGER.error("Creating new wemportal api instance")
47+
# TODO: This is a temporary solution and should be removed when api cause from #28 is resolved
48+
try:
49+
new_api = WemPortalApi(
50+
self.config_entry.data.get(CONF_USERNAME),
51+
self.config_entry.data.get(CONF_PASSWORD),
52+
self.config_entry.options,
53+
)
54+
self.api = new_api
55+
except Exception as exc2:
56+
raise UpdateFailed from exc2
57+
try:
58+
return await self.hass.async_add_executor_job(self.api.fetch_data)
59+
except WemPortalError as exc2:
60+
_LOGGER.error("Error fetching data from wemportal", exc_info=exc)
61+
raise UpdateFailed from exc2
62+
else:
63+
_LOGGER.error("Error fetching data from wemportal", exc_info=exc)
64+
raise UpdateFailed from exc
65+
66+

custom_components/wemportal/wemportalapi.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ def api_login(self):
195195
except reqs.exceptions.HTTPError as exc:
196196
self.valid_login = False
197197
response_status, response_message = self.get_response_details(response)
198-
if response.status_code == 400:
198+
if response is None:
199+
raise UnknownAuthError(
200+
f"Authentication Error: Encountered an unknown authentication error."
201+
) from exc
202+
elif response.status_code == 400:
199203
raise AuthError(
200204
f"Authentication Error: Check if your login credentials are correct. Received response code: {response.status_code}, response: {response.content}. Server returned internal status code: {response_status} and message: {response_message}"
201205
) from exc
@@ -213,13 +217,14 @@ def api_login(self):
213217
def get_response_details(self, response: reqs.Response):
214218
server_status = ""
215219
server_message = ""
216-
try:
217-
response_data = response.json()
218-
# Status we get back from server
219-
server_status = response_data["Status"]
220-
server_message = response_data["Message"]
221-
except KeyError:
222-
pass
220+
if response:
221+
try:
222+
response_data = response.json()
223+
# Status we get back from server
224+
server_status = response_data["Status"]
225+
server_message = response_data["Message"]
226+
except KeyError:
227+
pass
223228
return server_status, server_message
224229

225230
def make_api_call(
@@ -238,7 +243,7 @@ def make_api_call(
238243
)
239244
response.raise_for_status()
240245
except Exception as exc:
241-
if response.status_code == 401 and not login_retry:
246+
if response and response.status_code == 401 and not login_retry:
242247
self.api_login()
243248
headers = headers or self.headers
244249
time.sleep(delay)

0 commit comments

Comments
 (0)