From 1e7fc27bb8f2b466334d9ef137a8178e34d4b331 Mon Sep 17 00:00:00 2001 From: Duco Sebel <74970928+DCSBL@users.noreply.github.com> Date: Sun, 5 Jan 2025 15:56:25 +0100 Subject: [PATCH] Adjust has_v2_api so test does not trigger 'Was never awaited' error (#472) --- homewizard_energy/__init__.py | 8 ++++---- tests/test_helpers.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homewizard_energy/__init__.py b/homewizard_energy/__init__.py index 3fb34f5..5053bfb 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" - async with websession.get( - url, ssl=False, raise_for_status=False, timeout=15 - ) as res: - return res.status == 401 + res = await websession.get(url, ssl=False, raise_for_status=False, timeout=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 diff --git a/tests/test_helpers.py b/tests/test_helpers.py index f733b4a..079c5a0 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -47,7 +47,7 @@ async def test_has_v2_api_false(aresponses): async def test_has_v2_api_exception(): """Test if has_v2_api returns False when an exception occurs.""" session = AsyncMock() - session.request = AsyncMock(side_effect=asyncio.TimeoutError()) + session.get = AsyncMock(side_effect=asyncio.TimeoutError) result = await has_v2_api("example.com", session) assert result is False