From 2e85e374b3424f29fc74d95d06d33c1c0cf77294 Mon Sep 17 00:00:00 2001 From: Daniel Raper Date: Mon, 1 Jan 2024 15:11:32 +0000 Subject: [PATCH] And refactor --- custom_components/ohme/binary_sensor.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/custom_components/ohme/binary_sensor.py b/custom_components/ohme/binary_sensor.py index d4b5c41..2321d22 100644 --- a/custom_components/ohme/binary_sensor.py +++ b/custom_components/ohme/binary_sensor.py @@ -124,21 +124,17 @@ def is_on(self) -> bool: def _calculate_state(self) -> bool: """Some trickery to get the charge state to update quickly.""" - bsoc = self.coordinator.data['batterySoc'] power = self.coordinator.data["power"]["watt"] # If no last reading or no batterySoc, fallback to power > 0 if not self._last_reading or not self._last_reading['batterySoc']: - return self.coordinator.data["power"]["watt"] > 0 + return power > 0 # If Wh has positive delta and a nonzero power reading, we are charging # This isn't ideal - eg. quirk of MG ZS in #13, so need to revisit - lr_bsoc = self._last_reading['batterySoc'] - delta = bsoc['wh'] - lr_bsoc['wh'] - if delta > 0 and power > 0: - return True + delta = self.coordinator.data['batterySoc'] - self._last_reading['batterySoc']['wh'] - return False + return delta > 0 and power > 0 @callback def _handle_coordinator_update(self) -> None: