Skip to content

Commit

Permalink
Merges
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Jan 1, 2024
2 parents 4c334a4 + 2c7d64c commit 32919d9
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion custom_components/ohme/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Platform for sensor integration."""
from __future__ import annotations
import logging
import loggingimport logging
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity
Expand All @@ -14,6 +14,7 @@
from .utils import charge_graph_in_slot
from time import time

_LOGGER = logging.getLogger(__name__)
_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(
Expand Down Expand Up @@ -106,6 +107,13 @@ def __init__(
# Allow a state override
self._override_until = None

# Cache the last power readings
self._last_reading = None
self._last_reading_in_slot = False

# Allow a state override
self._override_until = None

self.entity_id = generate_entity_id(
"binary_sensor.{}", "ohme_car_charging", hass=hass)

Expand Down Expand Up @@ -173,6 +181,61 @@ def _calculate_state(self) -> bool:
# Fallback to the old way
return power > 0

@callback
def _handle_coordinator_update(self) -> None:
"""Update data."""
# If we have power info and the car is plugged in, calculate state. Otherwise, false
if self.coordinator.data and self.coordinator.data["power"] and self.coordinator.data['mode'] != "DISCONNECTED":
self._state = self._calculate_state()
return self._state

def _calculate_state(self) -> bool:
"""Some trickery to get the charge state to update quickly."""
# If we have overriden the state, return the current value until that time
if self._override_until and time() < self._override_until:
_LOGGER.debug("State overridden to False for 310s")
return self._state

# We have passed override check, reset it
self._override_until = None

power = self.coordinator.data["power"]["watt"]

# No last reading to go off, use power draw based state only - this lags
if not self._last_reading:
_LOGGER.debug("Last reading not found, default to power > 0")
return power > 0

# Get power from last reading
lr_power = self._last_reading["power"]["watt"]

# See if we are in a charge slot now and if we were for the last reading
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

# Store this for next time
self._last_reading_in_slot = in_charge_slot

# If:
# - 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.
if lr_power > 0 and power / lr_power < 0.6 and not in_charge_slot and lr_in_charge_slot:
_LOGGER.debug("Charge stop behaviour seen - overriding to False for 310 seconds")
self._override_until = time() + 310 # Override for 5 mins (and a bit)
return False

# Its possible that this is the 'transitionary' reading - slots updated but not power
# Override _last_reading_in_slot and see what happens next time around
elif lr_power > 0 and not in_charge_slot and lr_in_charge_slot:
_LOGGER.debug("Possible transitionary reading. Treating as slot boundary in next tick.")
self._last_reading_in_slot = True

# Fallback to the old way
return power > 0

@callback
def _handle_coordinator_update(self) -> None:
"""Update data."""
Expand Down

0 comments on commit 32919d9

Please sign in to comment.