From 6c71c4cce1bd2852f3763295d5c075067a854d82 Mon Sep 17 00:00:00 2001 From: Alvaro Duarte <59612788+ad-ha@users.noreply.github.com> Date: Sat, 16 Nov 2024 00:32:07 +0100 Subject: [PATCH] 0.4.8-a1 --- custom_components/mg_saic/coordinator.py | 32 ++++++++++++++++-------- custom_components/mg_saic/manifest.json | 2 +- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/custom_components/mg_saic/coordinator.py b/custom_components/mg_saic/coordinator.py index b07f207..f0779ff 100644 --- a/custom_components/mg_saic/coordinator.py +++ b/custom_components/mg_saic/coordinator.py @@ -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() @@ -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.""" diff --git a/custom_components/mg_saic/manifest.json b/custom_components/mg_saic/manifest.json index 75a2edc..75e1f84 100644 --- a/custom_components/mg_saic/manifest.json +++ b/custom_components/mg_saic/manifest.json @@ -12,5 +12,5 @@ "pycryptodome", "saic-ismart-client-ng==0.5.2" ], - "version": "0.4.7" + "version": "0.4.8-a1" }