Skip to content

Commit

Permalink
Move check for producing into separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
tijsverkoyen committed Jan 24, 2024
1 parent 0680f61 commit 2d92d5a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions custom_components/fusion_solar/fusion_solar/energy_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')
Expand Down

0 comments on commit 2d92d5a

Please sign in to comment.