Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit fb3ffe9

Browse files
committed
Fix bug with charge slot list when charge in progress
1 parent 5418275 commit fb3ffe9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

custom_components/ohme/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from datetime import datetime, timedelta
33
from .const import DOMAIN, DATA_OPTIONS
44
import pytz
5+
# import logging
6+
# _LOGGER = logging.getLogger(__name__)
57

68
def _format_charge_graph(charge_start, points):
79
"""Convert relative time in points array to real timestamp (s)."""
@@ -13,6 +15,30 @@ def _format_charge_graph(charge_start, points):
1315
return [{"t": x["x"] + charge_start, "y": x["y"]} for x in points]
1416

1517

18+
def _sanitise_points(points):
19+
"""Discard any points that aren't on a quarter-hour boundary."""
20+
output = []
21+
seen = []
22+
23+
points.reverse()
24+
25+
for point in points:
26+
# Round up the timestamp and get the minute
27+
ts = point['t'] + 30
28+
dt = datetime.fromtimestamp(ts)
29+
hm = dt.strftime('%H:%M')
30+
m = int(dt.strftime('%M'))
31+
32+
if m % 15 == 0 and hm not in seen:
33+
output.append(point)
34+
seen.append(hm)
35+
36+
output.reverse()
37+
# _LOGGER.warning("Charge slot graph points: " + str([{"t": datetime.fromtimestamp(x["t"] + 30).strftime('%H:%M:%S'), "y": x["y"]} for x in output]))
38+
39+
return output
40+
41+
1642
def _next_slot(data, live=False, in_progress=False):
1743
"""Get the next slot. live is whether or not we may start mid charge. Eg: For the next slot end sensor, we dont have the
1844
start but still want the end of the in progress session, but for the slot list sensor we only want slots that have
@@ -24,6 +50,7 @@ def _next_slot(data, live=False, in_progress=False):
2450
for idx in range(0, len(data) - 1):
2551
# Calculate the delta between this element and the next
2652
delta = data[idx + 1]["y"] - data[idx]["y"]
53+
delta = 0 if delta < 0 else delta # Zero floor deltas
2754

2855
# If the next point has a Y delta of 10+, consider this the start of a slot
2956
# This should be 0+ but I had some strange results in testing... revisit
@@ -71,6 +98,7 @@ def charge_graph_next_slot(charge_start, points, skip_format=False):
7198
def charge_graph_slot_list(charge_start, points, skip_format=False):
7299
"""Get list of charge slots from graph points."""
73100
data = points if skip_format else _format_charge_graph(charge_start, points)
101+
data = _sanitise_points(data)
74102

75103
# Give up if we have less than 2 points
76104
if len(data) < 2:

0 commit comments

Comments
 (0)