From 51999a2c5f72236dd8c2d579c9705a4201851383 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 119283a..750a01a 100644 --- a/custom_components/alfen_wallbox/alfen.py +++ b/custom_components/alfen_wallbox/alfen.py @@ -251,10 +251,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: @@ -273,9 +272,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 37d0514a08fe5cf7f741583dc1d955d4bb422a76 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 750a01a..d878788 100644 --- a/custom_components/alfen_wallbox/alfen.py +++ b/custom_components/alfen_wallbox/alfen.py @@ -271,16 +271,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