Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement handling of aiobiketrax exceptions #35

Merged
merged 2 commits into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions custom_components/biketrax/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Any

import voluptuous as vol
from aiobiketrax import Account
from aiobiketrax import Account, exceptions
from homeassistant import config_entries
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
Expand Down Expand Up @@ -37,10 +37,23 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
)

try:
await account.update_devices()
except:
try:
await account.update_devices()
except Exception as e: # pylint: disable=broad-except
_LOGGER.debug(
"Exception while validating input for '%s'.",
data["username"],
exc_info=e,
)
raise
except exceptions.ConnectionError:
raise CannotConnect
except exceptions.AuthenticationError:
raise InvalidAuth

if not account.devices:
raise NoDevice

return {"title": f"BikeTrax {data['username']}"}


Expand All @@ -66,6 +79,8 @@ async def async_step_user(
errors["base"] = "cannot_connect"
except InvalidAuth:
errors["base"] = "invalid_auth"
except NoDevice:
errors["base"] = "no_device"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
Expand Down Expand Up @@ -131,3 +146,7 @@ class CannotConnect(HomeAssistantError):

class InvalidAuth(HomeAssistantError):
"""Error to indicate there is invalid auth."""


class NoDevice(HomeAssistantError):
"""Error to indicate there are no devices."""
20 changes: 13 additions & 7 deletions custom_components/biketrax/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from datetime import timedelta

from aiobiketrax import Account
from aiobiketrax import Account, exceptions
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
Expand Down Expand Up @@ -49,8 +49,10 @@ async def _async_update_data(self) -> None:

for device in self.account.devices:
await device.update_position()
except (HTTPError, TimeoutException) as err:
raise UpdateFailed(f"Error communicating with BikeTrax API: {err}") from err
except exceptions.BikeTraxError as err:
raise UpdateFailed(
f"A BikeTrax error occurred while updating the devices: {err}"
) from err

def start_background_task(self):
"""Start the websocket task."""
Expand Down Expand Up @@ -83,8 +85,10 @@ async def _async_update_data(self) -> None:
try:
for device in self.account.devices:
await device.update_trips()
except (HTTPError, TimeoutException) as err:
raise UpdateFailed(f"Error communicating with BikeTrax API: {err}") from err
except exceptions.BikeTraxError as err:
raise UpdateFailed(
f"A BikeTrax error occurred while updating the trips: {err}"
) from err


class SubscriptionDataUpdateCoordinator(BikeTraxDataUpdateCoordinator):
Expand All @@ -109,5 +113,7 @@ async def _async_update_data(self) -> None:
try:
for device in self.account.devices:
await device.update_subscription()
except (HTTPError, TimeoutException) as err:
raise UpdateFailed(f"Error communicating with BikeTrax API: {err}") from err
except exceptions.BikeTraxError as err:
raise UpdateFailed(
f"A BikeTrax error occurred while updating the subscription data: {err}"
) from err
6 changes: 4 additions & 2 deletions custom_components/biketrax/translations/de.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"config": {
"abort": {
"already_configured": "Gerät wurde bereits konfiguriert"
"already_configured": "Ger\u00e4t wurde bereits konfiguriert"
},
"error": {
"cannot_connect": "Verbindung fehlgeschlagen",
"invalid_auth": "Ung\u00fcltige Authentifizierung"
"invalid_auth": "Ung\u00fcltige Authentifizierung",
"no_device": "Kein Ger\u00e4t mit Ihrem Konto verkn\u00fcpft",
"unknown": "Unerwarteter Fehler"
},
"step": {
"user": {
Expand Down
1 change: 1 addition & 0 deletions custom_components/biketrax/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"error": {
"cannot_connect": "Failed to connect",
"invalid_auth": "Invalid authentication",
"no_device": "No device connected to your account",
"unknown": "Unexpected error"
},
"step": {
Expand Down
4 changes: 3 additions & 1 deletion custom_components/biketrax/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
},
"error": {
"cannot_connect": "Kan geen verbinding maken",
"invalid_auth": "Ongeldige authenticatie"
"invalid_auth": "Ongeldige authenticatie",
"no_device": "Er is geen apparat aan het account gekoppeld",
"unknown": "Onbekende fout"
},
"step": {
"user": {
Expand Down