Skip to content

Commit 463b091

Browse files
committed
code review changes
1 parent d0a71a2 commit 463b091

3 files changed

Lines changed: 31 additions & 16 deletions

File tree

‎custom_components/sigenergy_cloud/api.py‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -429,19 +429,26 @@ async def get_custom_energy_stats(
429429

430430
last_error: SigenCloudApiError | None = None
431431
for params in deduped_candidates:
432-
try:
433-
return await self._request(
434-
"GET",
435-
ENDPOINT_ENERGY_STATS_CUSTOM,
436-
params=params,
437-
)
438-
except SigenCloudApiError as err:
439-
last_error = err
440-
_LOGGER.debug(
441-
"Custom energy stats failed method=GET params=%s: %s",
442-
params,
443-
err,
444-
)
432+
# For transient backend failures, retry once with the same request
433+
# before moving to compatibility variants.
434+
for attempt in (1, 2):
435+
try:
436+
return await self._request(
437+
"GET",
438+
ENDPOINT_ENERGY_STATS_CUSTOM,
439+
params=params,
440+
)
441+
except SigenCloudApiError as err:
442+
last_error = err
443+
_LOGGER.debug(
444+
"Custom energy stats failed method=GET attempt=%s params=%s: %s",
445+
attempt,
446+
params,
447+
err,
448+
)
449+
if attempt == 1 and "HTTP 500" in str(err):
450+
continue
451+
break
445452

446453
if last_error is not None:
447454
last_message = str(last_error)

‎test_auth_live.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import json
2121
import logging
2222
import os
23+
import re
2324
import sys
2425

2526
import aiohttp
@@ -313,7 +314,13 @@ async def run() -> None:
313314
lowered = message.lower()
314315
if (
315316
"unsupported for this account/region" in lowered
316-
or ("http 500" in lowered and ("system error" in lowered or '"code":1' in lowered))
317+
or (
318+
"http 500" in lowered
319+
and (
320+
"system error" in lowered
321+
or re.search(r'"code"\s*:\s*1\b', lowered) is not None
322+
)
323+
)
317324
):
318325
print("UNAVAILABLE")
319326
print(" Endpoint appears unsupported for this account/region")

‎tests/test_api.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ async def test_get_custom_energy_stats(self, mock_session):
390390
}
391391

392392
@pytest.mark.asyncio
393-
async def test_get_custom_energy_stats_fallback_params(self, mock_session):
393+
async def test_get_custom_energy_stats_retries_on_500(self, mock_session):
394394
client = await self._make_authed_client(mock_session)
395395

396396
fail_resp = make_mock_response(
@@ -419,9 +419,10 @@ async def test_get_custom_energy_stats_fallback_params(self, mock_session):
419419
first_call = mock_session.request.call_args_list[0]
420420
second_call = mock_session.request.call_args_list[1]
421421
assert first_call[0][0] == "GET"
422+
assert second_call[0][0] == "GET"
423+
assert first_call[1]["params"] == second_call[1]["params"]
422424
assert first_call[1]["params"]["stationId"] == "STATION001"
423425
assert first_call[1]["params"]["resourceIds"] == "energy_card"
424-
assert second_call[0][0] == "GET"
425426

426427
@pytest.mark.asyncio
427428
async def test_set_operational_mode(self, mock_session):

0 commit comments

Comments
 (0)