From ffa2fa0bcfc4d53aab6d54608aad748ddb7edf9b Mon Sep 17 00:00:00 2001 From: Jordan H Date: Fri, 29 Sep 2023 20:31:45 +0000 Subject: [PATCH] use contextlib to surpress expected exceptions --- custom_components/family_safety/config_flow.py | 8 ++------ custom_components/family_safety/coordinator.py | 7 +++++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/custom_components/family_safety/config_flow.py b/custom_components/family_safety/config_flow.py index 83990f0..9192178 100644 --- a/custom_components/family_safety/config_flow.py +++ b/custom_components/family_safety/config_flow.py @@ -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 ) @@ -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", diff --git a/custom_components/family_safety/coordinator.py b/custom_components/family_safety/coordinator.py index 66fcc72..c91b2c3 100644 --- a/custom_components/family_safety/coordinator.py +++ b/custom_components/family_safety/coordinator.py @@ -1,5 +1,6 @@ """Family Safety data hub.""" +import contextlib import logging from datetime import timedelta @@ -7,6 +8,7 @@ from homeassistant.core import HomeAssistant from pyfamilysafety import FamilySafety +from pyfamilysafety.exceptions import AggregatorException from homeassistant.helpers.update_coordinator import ( DataUpdateCoordinator, UpdateFailed @@ -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