Skip to content

Commit

Permalink
Better connection error handling for remote loader (#2360)
Browse files Browse the repository at this point in the history
* fix: better connection error handling for remote loader

* fix: better connection error handling for remote loader
  • Loading branch information
bramstroker committed Jul 6, 2024
1 parent adb1700 commit 5d83645
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions custom_components/powercalc/power_profile/loader/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,13 @@ async def download_with_retry(self, callback: Callable[[], Coroutine[Any, Any, N
try:
return await callback()
except (ClientError, ProfileDownloadError) as e:
_LOGGER.error(e, exc_info=e)
_LOGGER.debug(e)
retry_count += 1
if retry_count == max_retries:
raise ProfileDownloadError(f"Failed to download even after {max_retries} retries, falling back to local copy") from e

await asyncio.sleep(self.retry_timeout)
_LOGGER.warning("Failed to download, retrying... (Attempt %d of %d)", retry_count, max_retries)
_LOGGER.warning("Failed to download, retrying... (Attempt %d of %d)", retry_count + 1, max_retries)
return None # pragma: no cover

async def download_profile(self, manufacturer: str, model: str, storage_path: str) -> None:
Expand Down
8 changes: 4 additions & 4 deletions tests/power_profile/loader/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ async def test_fallback_to_local_library(hass: HomeAssistant, mock_aioresponse:
Test that the local library is used when the remote library is not available.
When unavailable, it should retry 3 times before falling back to the local library.
"""
caplog.set_level(logging.ERROR)
caplog.set_level(logging.WARNING)
mock_aioresponse.get(
ENDPOINT_LIBRARY,
status=404,
Expand All @@ -298,7 +298,7 @@ async def test_fallback_to_local_library(hass: HomeAssistant, mock_aioresponse:
await loader.initialize()

assert "signify" in loader.manufacturer_models
assert len(caplog.records) == 3
assert len(caplog.records) == 2


async def test_fallback_to_local_library_on_client_connection_error(
Expand All @@ -310,7 +310,7 @@ async def test_fallback_to_local_library_on_client_connection_error(
Test that the local library is used when powercalc.lauwbier.nl is not available.
See: https://github.com/bramstroker/homeassistant-powercalc/issues/2277
"""
caplog.set_level(logging.ERROR)
caplog.set_level(logging.WARNING)
mock_aioresponse.get(
ENDPOINT_LIBRARY,
status=200,
Expand All @@ -323,7 +323,7 @@ async def test_fallback_to_local_library_on_client_connection_error(
await loader.initialize()

assert "signify" in loader.manufacturer_models
assert len(caplog.records) == 3
assert len(caplog.records) == 2


async def test_fallback_to_local_profile(
Expand Down

0 comments on commit 5d83645

Please sign in to comment.