From aa14b236c6840a39584a59a07943099fe9c89472 Mon Sep 17 00:00:00 2001 From: Kyle Date: Thu, 15 Oct 2026 00:18:28 +0200 Subject: [PATCH 1/2] Add timeout handling to API request Refactor API call to include timeout handling. --- homewizard_energy/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homewizard_energy/__init__.py b/homewizard_energy/__init__.py index 5053bfbf..34c94d96 100644 --- a/homewizard_energy/__init__.py +++ b/homewizard_energy/__init__.py @@ -27,10 +27,10 @@ async def has_v2_api(host: str, websession: ClientSession | None = None) -> bool # v2 api is https only and returns a 401 Unauthorized when no key provided, # no connection can be made if the device is not v2 url = f"https://{host}/api" - res = await websession.get(url, ssl=False, raise_for_status=False, timeout=5) - res.close() - - return res.status == 401 + async with asyncio.timeout(5.0): + res = await websession.get(url, ssl=False, raise_for_status=False) + res.close() + return res.status == 401 except Exception: # pylint: disable=broad-except # all other status/exceptions means the device is not v2 or not reachable at this time return False From 858c8880e9eb5a6668dbe8b6b8cf5362a95cd207 Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 24 Nov 2026 22:18:05 +0100 Subject: [PATCH 2/2] Add ClientTimeout to API request Updated the API request to use ClientTimeout for better timeout handling. --- homewizard_energy/__init__.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/homewizard_energy/__init__.py b/homewizard_energy/__init__.py index 34c94d96..a34344e1 100644 --- a/homewizard_energy/__init__.py +++ b/homewizard_energy/__init__.py @@ -1,6 +1,6 @@ """HomeWizard Energy API library.""" -from aiohttp import ClientSession +from aiohttp import ClientSession, ClientTimeout from .errors import DisabledError, InvalidStateError, RequestError, UnsupportedError from .homewizard_energy import HomeWizardEnergy @@ -27,10 +27,15 @@ async def has_v2_api(host: str, websession: ClientSession | None = None) -> bool # v2 api is https only and returns a 401 Unauthorized when no key provided, # no connection can be made if the device is not v2 url = f"https://{host}/api" - async with asyncio.timeout(5.0): - res = await websession.get(url, ssl=False, raise_for_status=False) - res.close() - return res.status == 401 + res = await websession.get( + url, + ssl=False, + raise_for_status=False, + timeout=ClientTimeout(total=5), + ) + res.close() + + return res.status == 401 except Exception: # pylint: disable=broad-except # all other status/exceptions means the device is not v2 or not reachable at this time return False