Skip to content

Commit 7ec8678

Browse files
authored
Fix authentication error handling and trigger reauth flow (#385)
Closes #159
1 parent 2051fca commit 7ec8678

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

custom_components/bhyve/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
7474

7575
try:
7676
if await client.login() is False:
77+
_LOGGER.warning("Invalid credentials for %s", entry.data[CONF_USERNAME])
7778
msg = "Invalid credentials"
7879
raise ConfigEntryAuthFailed(msg)
7980
except AuthenticationError as err:
81+
_LOGGER.warning("Authentication failed for %s", entry.data[CONF_USERNAME])
8082
raise ConfigEntryAuthFailed(err) from err
8183
except BHyveError as err:
8284
raise ConfigEntryNotReady(err) from err
@@ -177,6 +179,12 @@ async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
177179

178180
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
179181
"""Unload a config entry."""
182+
data = hass.data[DOMAIN].get(entry.entry_id)
183+
if data:
184+
client = data.get("client")
185+
if client:
186+
await client.stop()
187+
180188
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
181189
hass.data[DOMAIN].pop(entry.entry_id)
182190

custom_components/bhyve/coordinator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from datetime import timedelta
88
from typing import TYPE_CHECKING, Any
99

10+
from homeassistant.exceptions import ConfigEntryAuthFailed
1011
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
1112

1213
from .const import (
@@ -122,8 +123,8 @@ async def _async_update_data(self) -> dict[str, Any]:
122123
return data # noqa: TRY300
123124

124125
except AuthenticationError as err:
125-
msg = "Authentication failed"
126-
raise UpdateFailed(msg) from err
126+
_LOGGER.warning("Authentication failed, triggering reauth: %s", err)
127+
raise ConfigEntryAuthFailed from err
127128
except BHyveError as err:
128129
msg = f"Error communicating with API: {err}"
129130
raise UpdateFailed(msg) from err

custom_components/bhyve/pybhyve/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ async def _request(
8282
try:
8383
resp.raise_for_status()
8484
return await resp.json(content_type=None)
85+
except ClientResponseError as err:
86+
if err.status in (401, 403):
87+
_LOGGER.warning("Authentication error from %s: %s", url, err.status)
88+
raise AuthenticationError from err
89+
msg = f"Error requesting data from {url}: {err}"
90+
raise RequestError(msg) from err
8591
except Exception as err:
8692
msg = f"Error requesting data from {url}: {err}"
8793
raise RequestError(msg) from err
@@ -168,7 +174,7 @@ async def login(self) -> bool:
168174
self._token = response["orbit_session_token"]
169175

170176
except ClientResponseError as response_err:
171-
if response_err.status == 400: # noqa: PLR2004
177+
if response_err.status in (401, 403):
172178
raise AuthenticationError from response_err
173179
raise RequestError from response_err
174180
except Exception as err:

0 commit comments

Comments
 (0)