From 50bae83c2aaaaf560e80f4b0ec91e9ef3865010c Mon Sep 17 00:00:00 2001 From: Ivan Kruglov Date: Tue, 9 Apr 2024 10:50:16 +0200 Subject: [PATCH 1/2] alfen.py: improve logging --- custom_components/alfen_wallbox/alfen.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/custom_components/alfen_wallbox/alfen.py b/custom_components/alfen_wallbox/alfen.py index 8153d2a..6289e7f 100644 --- a/custom_components/alfen_wallbox/alfen.py +++ b/custom_components/alfen_wallbox/alfen.py @@ -243,10 +243,9 @@ async def _update_value(self, api_param, value, allowed_login=True) -> ClientRes async def _get_value(self, api_param): """Get a value from the API.""" - response = await self._get(url=self.__get_url( - f"{PROP}?{ID}={api_param}")) - - _LOGGER.debug(f"Status Response {response}") + cmd = f"{PROP}?{ID}={api_param}" + response = await self._get(url=self.__get_url(cmd)) + _LOGGER.debug(f"Status Response {cmd}: {response}") if response is not None: if self.properties is None: @@ -265,9 +264,10 @@ async def _get_all_properties_value(self): nextRequest = True offset = 0 while (nextRequest): - response = await self._get(url=self.__get_url( - f"{PROP}?{CAT}={cat}&{OFFSET}={offset}")) - _LOGGER.debug(f"Status Response {response}") + cmd = f"{PROP}?{CAT}={cat}&{OFFSET}={offset}" + response = await self._get(url=self.__get_url(cmd)) + _LOGGER.debug(f"Status Response {cmd}: {response}") + if response is not None: properties += response[PROPERTIES] nextRequest = response[TOTAL] > ( From be6438d3c80e55b3b6397e3b3936fc42a60e2b58 Mon Sep 17 00:00:00 2001 From: Ivan Kruglov Date: Tue, 9 Apr 2024 10:51:01 +0200 Subject: [PATCH 2/2] alfen.py: prevent infinite loop in _get_all_properties_value() --- custom_components/alfen_wallbox/alfen.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/custom_components/alfen_wallbox/alfen.py b/custom_components/alfen_wallbox/alfen.py index 6289e7f..b27260a 100644 --- a/custom_components/alfen_wallbox/alfen.py +++ b/custom_components/alfen_wallbox/alfen.py @@ -263,16 +263,26 @@ async def _get_all_properties_value(self): for cat in (CAT_GENERIC, CAT_GENERIC2, CAT_METER1, CAT_STATES, CAT_TEMP, CAT_OCPP, CAT_METER4, CAT_MBUS_TCP, CAT_COMM, CAT_DISPLAY, CAT_METER2): nextRequest = True offset = 0 + attempt = 0 while (nextRequest): + attempt += 1 cmd = f"{PROP}?{CAT}={cat}&{OFFSET}={offset}" response = await self._get(url=self.__get_url(cmd)) _LOGGER.debug(f"Status Response {cmd}: {response}") if response is not None: + attempt = 0 properties += response[PROPERTIES] nextRequest = response[TOTAL] > ( offset + len(response[PROPERTIES])) offset += len(response[PROPERTIES]) + elif attempt >= 3: + # This only possible in case of series of timeouts or unknown exceptions in self._get() + # It's better to break completely, otherwise we can provide partial data in self.properties. + _LOGGER.debug(f"Returning earlier after {attempt} attempts") + self.properties = [] + return + _LOGGER.debug(f"Properties {properties}") self.properties = properties