From 2d92d5a9ec48801657c640425db2a4817607868d Mon Sep 17 00:00:00 2001 From: Tijs Verkoyen Date: Wed, 24 Jan 2024 17:47:31 +0100 Subject: [PATCH] Move check for producing into separate method --- .../fusion_solar/fusion_solar/energy_sensor.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/custom_components/fusion_solar/fusion_solar/energy_sensor.py b/custom_components/fusion_solar/fusion_solar/energy_sensor.py index bd69ee4..91fc27a 100644 --- a/custom_components/fusion_solar/fusion_solar/energy_sensor.py +++ b/custom_components/fusion_solar/fusion_solar/energy_sensor.py @@ -61,10 +61,10 @@ def native_value(self) -> float: _LOGGER.info(f'{self.entity_id}: not available, so no update to prevent issues.') return - realtime_power = self.coordinator.data[self._data_name][ATTR_REALTIME_POWER] - if math.isclose(float(realtime_power), 0, abs_tol = 0.001): - _LOGGER.info(f'{self.entity_id}: not producing any power, so no energy update to prevent glitches.') - return float(current_value) + # Return the current value if the system is not producing + if not self.is_producing_at_the_moment(): + _LOGGER.info(f'{self.entity_id}: not producing any power, so no update to prevent glitches.') + return current_value try: return self.get_float_value_from_coordinator(self._attribute) @@ -84,6 +84,14 @@ def state_class(self) -> str: def device_info(self) -> dict: return self._device_info + def is_producing_at_the_moment(self) -> bool: + try: + realtime_power = self.get_float_value_from_coordinator(ATTR_REALTIME_POWER) + return not math.isclose(realtime_power, 0, abs_tol=0.001) + except FusionSolarEnergySensorException as e: + _LOGGER.info(e) + return False + def get_float_value_from_coordinator(self, attribute_name: str) -> float: if self.coordinator.data is False: raise FusionSolarEnergySensorException('Coordinator data is False')