From 57de4ff31300b2765971dd99d2c9b8d48e7f4c50 Mon Sep 17 00:00:00 2001 From: Olivier Bossaer Date: Mon, 3 Oct 2022 14:10:48 +0200 Subject: [PATCH 1/3] If new_value is not a float: log and return current value --- custom_components/fusion_solar_kiosk/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/custom_components/fusion_solar_kiosk/__init__.py b/custom_components/fusion_solar_kiosk/__init__.py index 2d53d9a..c968619 100644 --- a/custom_components/fusion_solar_kiosk/__init__.py +++ b/custom_components/fusion_solar_kiosk/__init__.py @@ -27,6 +27,12 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool: """Set up the FusionSolar Kiosk component.""" return True +def isfloat(num) -> bool: + try: + float(num) + return True + except ValueError: + return False class FusionSolarKioskEnergyEntity(CoordinatorEntity, SensorEntity): """Base class for all FusionSolarKioskEnergy entities.""" @@ -66,6 +72,10 @@ def state(self) -> float: if entity is not None: current_value = entity.state new_value = self.coordinator.data[self._kioskId][ATTR_DATA_REALKPI][self._attribute] + if not isfloat(new_value): + _LOGGER.warning(f'{self.entity_id}: new value ({new_value}) is not a float, so not updating.') + return float(current_value) + if float(new_value) < float(current_value): _LOGGER.debug(f'{self.entity_id}: new value ({new_value}) is smaller then current value ({entity.state}), so not updating.') return float(current_value) From bbd29d41778a9162ee3097a096ffda70355a9ef9 Mon Sep 17 00:00:00 2001 From: Olivier Bossaer Date: Wed, 5 Oct 2022 13:57:02 +0200 Subject: [PATCH 2/3] Adds warning on current_value is None --- custom_components/fusion_solar_kiosk/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/custom_components/fusion_solar_kiosk/__init__.py b/custom_components/fusion_solar_kiosk/__init__.py index c968619..7660ca1 100644 --- a/custom_components/fusion_solar_kiosk/__init__.py +++ b/custom_components/fusion_solar_kiosk/__init__.py @@ -76,6 +76,10 @@ def state(self) -> float: _LOGGER.warning(f'{self.entity_id}: new value ({new_value}) is not a float, so not updating.') return float(current_value) + if not isfloat(current_value): + _LOGGER.warning(f'{self.entity_id}: current value ({current_value}) is not a float, send 0.') + return 0 + if float(new_value) < float(current_value): _LOGGER.debug(f'{self.entity_id}: new value ({new_value}) is smaller then current value ({entity.state}), so not updating.') return float(current_value) From afcaea2a3e066316c2a3ee225231f4d0646f2afc Mon Sep 17 00:00:00 2001 From: Olivier Bossaer Date: Thu, 6 Oct 2022 10:28:12 +0200 Subject: [PATCH 3/3] Logs KeyError --- .../fusion_solar_kiosk/fusion_solar_kiosk_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/fusion_solar_kiosk/fusion_solar_kiosk_api.py b/custom_components/fusion_solar_kiosk/fusion_solar_kiosk_api.py index c8f9039..5e068f3 100644 --- a/custom_components/fusion_solar_kiosk/fusion_solar_kiosk_api.py +++ b/custom_components/fusion_solar_kiosk/fusion_solar_kiosk_api.py @@ -38,9 +38,9 @@ def getRealTimeKpi(self, id: str): _LOGGER.debug(jsonData[ATTR_DATA][ATTR_DATA_REALKPI]) return jsonData[ATTR_DATA][ATTR_DATA_REALKPI] - except FusionSolarKioskApiError as error: + except KeyError as error: _LOGGER.error(error) - _LOGGER.debug(response.text) + _LOGGER.error(response.text) except FusionSolarKioskApiError as error: _LOGGER.error(error)