From ef060259ce892cde22b804e05f06682e75b69db1 Mon Sep 17 00:00:00 2001 From: Tijs Verkoyen Date: Tue, 28 Feb 2023 14:08:41 +0100 Subject: [PATCH] Move handling unavailable entity state up --- .../fusion_solar/fusion_solar/energy_sensor.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/custom_components/fusion_solar/fusion_solar/energy_sensor.py b/custom_components/fusion_solar/fusion_solar/energy_sensor.py index 4dab9d4..a27687c 100644 --- a/custom_components/fusion_solar/fusion_solar/energy_sensor.py +++ b/custom_components/fusion_solar/fusion_solar/energy_sensor.py @@ -59,18 +59,17 @@ def state(self) -> float: if entity is not None: current_value = entity.state - realtime_power = self.coordinator.data[self._data_name][ATTR_REALTIME_POWER] + if current_value == 'unavailable': + _LOGGER.info( + f'{self.entity_id}: not available.') + return + realtime_power = self.coordinator.data[self._data_name][ATTR_REALTIME_POWER] if realtime_power == '0.00': _LOGGER.info( f'{self.entity_id}: not producing any power, so not updating to prevent positive glitched.') return float(current_value) - if current_value == 'unavailable': - _LOGGER.info( - f'{self.entity_id}: not available.') - return - if self._data_name not in self.coordinator.data: return None