Skip to content

Commit

Permalink
use contextlib to surpress expected exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
pantherale0 committed Sep 29, 2023
1 parent 4c1f15b commit ffa2fa0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
8 changes: 2 additions & 6 deletions custom_components/family_safety/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,11 @@ async def async_step_accounts(

if user_input is not None:
tracked_user_ids = []
try:
with contextlib.suppress(IndexError):
for user in user_input.get("accounts", []):
tracked_user_ids.append(
_get_account_id(user, self.family_safety.accounts)
)
except IndexError:
pass
return await self._async_create_entry(
accounts=tracked_user_ids
)
Expand All @@ -242,11 +240,9 @@ async def async_step_accounts(
if tracked_accounts is None:
tracked_accounts = []
for account in tracked_accounts:
try:
with contextlib.suppress(IndexError):
acc = self.family_safety.get_account(account)
default_tracked_accounts.append(f"{acc.first_name} {acc.surname}")
except IndexError:
pass

return self.async_show_form(
step_id="accounts",
Expand Down
7 changes: 5 additions & 2 deletions custom_components/family_safety/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Family Safety data hub."""

import contextlib
import logging
from datetime import timedelta

import async_timeout

from homeassistant.core import HomeAssistant
from pyfamilysafety import FamilySafety
from pyfamilysafety.exceptions import AggregatorException
from homeassistant.helpers.update_coordinator import (
DataUpdateCoordinator,
UpdateFailed
Expand Down Expand Up @@ -35,7 +37,8 @@ def __init__(self,
async def _async_update_data(self):
"""Fetch and update data from the API."""
try:
async with async_timeout.timeout(50):
return await self.api.update()
async with async_timeout.timeout(59):
with contextlib.suppress(AggregatorException):
return await self.api.update()
except Exception as err:
raise UpdateFailed(f"Error communicating with API {err}") from err

0 comments on commit ffa2fa0

Please sign in to comment.