22from datetime import datetime , timedelta
33from .const import DOMAIN , DATA_OPTIONS
44import pytz
5+ # import logging
6+ # _LOGGER = logging.getLogger(__name__)
57
68def _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+
1642def _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):
7198def 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