Skip to content

Commit

Permalink
And refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Jan 1, 2024
1 parent b5a7944 commit dc3228e
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions custom_components/ohme/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']['wh'] - self._last_reading['batterySoc']['wh']

return False
return delta > 0 and power > 0

@callback
def _handle_coordinator_update(self) -> None:
Expand Down

0 comments on commit dc3228e

Please sign in to comment.