Skip to content

Commit

Permalink
Merge pull request #602 from astrandb/TimeoutRertry
Browse files Browse the repository at this point in the history
Retry data fetch on timeout before flagging failure
  • Loading branch information
astrandb authored Dec 14, 2024
2 parents 7557c9e + 732c4d6 commit de246ce
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions custom_components/miele/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN][entry.entry_id] = {}
hass.data[DOMAIN]["id_log"] = []
hass.data[DOMAIN][entry.entry_id]["retries_401"] = 0
hass.data[DOMAIN][entry.entry_id]["timeouts"] = 0
hass.data[DOMAIN][entry.entry_id]["listener"] = None
hass.data[DOMAIN][entry.entry_id][API] = AsyncConfigEntryAuth(
aiohttp_client.async_get_clientsession(hass), session
Expand Down Expand Up @@ -288,30 +289,44 @@ async def get_coordinator(

async def async_fetch():
miele_api = hass.data[DOMAIN][entry.entry_id][API]
try:
async with asyncio.timeout(API_READ_TIMEOUT):
res = await miele_api.request(
"GET",
f"/devices?language={hass.config.language}",
agent_suffix=f"Miele for Home Assistant/{VERSION}",
)
if res.status == 401:
hass.data[DOMAIN][entry.entry_id]["retries_401"] += 1
if hass.data[DOMAIN][entry.entry_id]["retries_401"] == 5:
raise ConfigEntryAuthFailed(
"Authentication failure when fetching data"
while True:
try:
async with asyncio.timeout(API_READ_TIMEOUT):
res = await miele_api.request(
"GET",
f"/devices?language={hass.config.language}",
agent_suffix=f"Miele for Home Assistant/{VERSION}",
)
if res.status == 401:
hass.data[DOMAIN][entry.entry_id]["retries_401"] += 1
if hass.data[DOMAIN][entry.entry_id]["retries_401"] == 5:
raise ConfigEntryAuthFailed(
"Authentication failure when fetching data"
)
raise UpdateFailed(
f"HTTP status 401: Retry {hass.data[DOMAIN][entry.entry_id]['retries_401']}"
)
raise UpdateFailed(
f"HTTP status 401: Retry {hass.data[DOMAIN][entry.entry_id]['retries_401']}"
if res.status != 200:
raise UpdateFailed(
f"HTTP Status {res.status}: fetching {DOMAIN} data"
)
result = await res.json()
break
except JSONDecodeError as error:
_LOGGER.error("Could not decode json from coordinator fetch")
raise UpdateFailed(error) from error
except TimeoutError as error:
hass.data[DOMAIN][entry.entry_id]["timeouts"] += 1
_LOGGER.debug(
"Timeout #%s fetching data from API",
hass.data[DOMAIN][entry.entry_id]["timeouts"],
)
if res.status != 200:
raise UpdateFailed(f"HTTP Status {res.status}: fetching {DOMAIN} data")
result = await res.json()
except JSONDecodeError as error:
_LOGGER.error("Could not decode json from coordinator fetch")
raise UpdateFailed(error) from error
if hass.data[DOMAIN][entry.entry_id]["timeouts"] >= 3:
raise TimeoutError(error) from error
await asyncio.sleep(10)

hass.data[DOMAIN][entry.entry_id]["retries_401"] = 0
hass.data[DOMAIN][entry.entry_id]["timeouts"] = 0
flat_result: dict = {}
# result["1223001"] = TEST_DATA_1
# result["1223003"] = TEST_DATA_3
Expand Down

0 comments on commit de246ce

Please sign in to comment.