Skip to content

Commit

Permalink
0.4.8-a1
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-ha authored Nov 15, 2024
1 parent 3683b89 commit 6c71c4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 22 additions & 10 deletions custom_components/mg_saic/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ async def async_setup(self):
async def _async_update_data(self):
"""Fetch data from the API."""
retries = 0
data = {}
while retries < RETRY_LIMIT:
data = {}
try:
# Fetch charging info
charging_info = await self.client.get_charging_info()
Expand Down Expand Up @@ -137,24 +137,36 @@ async def _async_update_data(self):
LOGGER.warning("Data invalid or generic: %s", e)
retries += 1
delay = min(retries * RETRY_BACKOFF_FACTOR, MAX_RETRY_DELAY)
LOGGER.info("Retrying in %s seconds...", delay)
LOGGER.info(
"Retrying in %s seconds... (Attempt %d/%d)",
delay,
retries,
RETRY_LIMIT,
)
await asyncio.sleep(delay)

except Exception as e:
LOGGER.error("Error fetching data: %s", e)
retries += 1
delay = min(retries * RETRY_BACKOFF_FACTOR, MAX_RETRY_DELAY)
LOGGER.info("Retrying in %s seconds...", delay)
LOGGER.info(
"Retrying in %s seconds... (Attempt %d/%d)",
delay,
retries,
RETRY_LIMIT,
)
await asyncio.sleep(delay)

# After retries exhausted
LOGGER.error("Failed to fetch data after retries.")
if self.data:
LOGGER.info("Using previous data.")
return self.data
else:
LOGGER.error("No previous data available. Returning empty data.")
return {}
LOGGER.error("Failed to fetch data after %d retries.", RETRY_LIMIT)
# Proceed with partial or empty data
data = self.data or {}
data.setdefault("charging", None)
data.setdefault("status", None)
data.setdefault("info", None)
# Set is_charging to False as we don't have valid data
self.is_charging = False
return data

def _is_generic_response(self, status):
"""Check if the vehicle status response is generic."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mg_saic/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"pycryptodome",
"saic-ismart-client-ng==0.5.2"
],
"version": "0.4.7"
"version": "0.4.8-a1"
}

0 comments on commit 6c71c4c

Please sign in to comment.