Skip to content

Commit

Permalink
Re-ordering condition for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Jan 1, 2024
1 parent 2c5163d commit 2fea59d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions custom_components/ohme/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,17 @@ def _calculate_state(self) -> bool:
in_charge_slot = charge_graph_in_slot(
self.coordinator.data['startTime'], self.coordinator.data['chargeGraph']['points'])
lr_in_charge_slot = self._last_reading_in_slot
lr_power = self._last_reading["power"]["watt"]
# Store this for next time
self._last_reading_in_slot = in_charge_slot

# If:
# - Power has dropped by 40% since the last reading
# - Power has dropped by 40%+ since the last reading
# - Last reading we were in a charge slot
# - Now we are not in a charge slot
# The charge has stopped but the power reading is lagging.
# The charge has JUST stopped on the session bounary but the power reading is lagging.
# This condition makes sure we get the charge state updated on the tick immediately after charge stop.
if lr_power > 0 and power / lr_power < 0.6 and not in_charge_slot and lr_in_charge_slot:
lr_power = self._last_reading["power"]["watt"]
if lr_in_charge_slot and not in_charge_slot and lr_power > 0 and power / lr_power < 0.6:
return False

# Failing that, we use the watt hours field to check charge state:
Expand Down

0 comments on commit 2fea59d

Please sign in to comment.