|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Predbat Home Battery System |
| 3 | +# Copyright Trefor Southwell 2026 - All Rights Reserved |
| 4 | +# This application maybe used for personal use only and not for commercial use |
| 5 | +# ----------------------------------------------------------------------------- |
| 6 | +# fmt off |
| 7 | +# pylint: disable=consider-using-f-string |
| 8 | +# pylint: disable=line-too-long |
| 9 | +# pylint: disable=attribute-defined-outside-init |
| 10 | + |
| 11 | +from datetime import datetime |
| 12 | +import pytz |
| 13 | +from utils import parse_car_plan_windows, in_car_plan_window |
| 14 | + |
| 15 | +LONDON = pytz.timezone("Europe/London") |
| 16 | + |
| 17 | + |
| 18 | +def _window(start, end): |
| 19 | + """A planned-attribute entry as output.py publishes it.""" |
| 20 | + return {"start": start, "end": end, "kwh": 5.0, "average": 20.0, "cost": 1.0} |
| 21 | + |
| 22 | + |
| 23 | +def test_car_plan_windows(my_predbat=None): |
| 24 | + """ |
| 25 | + utils.parse_car_plan_windows() rebuilds the year on the car charging plan (published as |
| 26 | + "%m-%d %H:%M:%S") around now. Shared by every charger that follows the plan - myenergi, |
| 27 | + GivEnergy EVC, Ohme and the gateway EVC (#269, #5120 review). |
| 28 | + """ |
| 29 | + failed = False |
| 30 | + print("**** Testing parse_car_plan_windows ****") |
| 31 | + |
| 32 | + cases = [ |
| 33 | + # (name, now, start, end, expected start year, expected end year, now inside) |
| 34 | + ("ordinary window", datetime(2026, 6, 15, 14, 0), "06-15 13:00:00", "06-15 15:00:00", 2026, 2026, True), |
| 35 | + ("New Year window read before midnight", datetime(2026, 12, 31, 23, 45), "12-31 23:30:00", "01-01 01:30:00", 2026, 2027, True), |
| 36 | + # Once now itself is in January the Dec 31 start belongs to the previous year, or the window |
| 37 | + # lands a year in the future and a charging car is stopped mid-window |
| 38 | + ("New Year window read after midnight", datetime(2027, 1, 1, 0, 15), "12-31 23:30:00", "01-01 01:30:00", 2026, 2027, True), |
| 39 | + # A window wholly in January, read on 31 December, parses into January of the year ending and |
| 40 | + # has to move forward a year |
| 41 | + ("January window read on 31 December", datetime(2026, 12, 31, 23, 30), "01-01 00:30:00", "01-01 02:00:00", 2027, 2027, False), |
| 42 | + # A long window whose start is days old is still the current one, not a year rollover (#269) |
| 43 | + ("long active window", datetime(2026, 6, 16, 23, 5), "06-14 00:10:00", "06-17 02:00:00", 2026, 2026, True), |
| 44 | + ("midnight-crossing window", datetime(2026, 6, 15, 23, 30), "06-15 23:00:00", "06-16 00:30:00", 2026, 2026, True), |
| 45 | + # strptime with no year assumes 1900, which has no 29 February - the window used to be dropped |
| 46 | + ("leap day window", datetime(2028, 2, 29, 1, 0), "02-29 00:30:00", "02-29 05:00:00", 2028, 2028, True), |
| 47 | + ("window starting on leap day, read on 1 March", datetime(2028, 3, 1, 0, 30), "02-29 23:30:00", "03-01 02:00:00", 2028, 2028, True), |
| 48 | + ] |
| 49 | + for name, now_naive, start, end, start_year, end_year, inside in cases: |
| 50 | + now = LONDON.localize(now_naive) |
| 51 | + windows = parse_car_plan_windows([_window(start, end)], now, LONDON) |
| 52 | + if len(windows) != 1: |
| 53 | + print("ERROR: {}: expected one window, got {}".format(name, windows)) |
| 54 | + failed = True |
| 55 | + continue |
| 56 | + start_dt, end_dt = windows[0] |
| 57 | + if (start_dt.year, end_dt.year) != (start_year, end_year) or in_car_plan_window(windows, now) != inside: |
| 58 | + print("ERROR: {}: got start={} end={} (now {}, inside {})".format(name, start_dt, end_dt, now, in_car_plan_window(windows, now))) |
| 59 | + failed = True |
| 60 | + |
| 61 | + # A malformed entry, or a stale 29 February one that has no counterpart in the neighbouring year, |
| 62 | + # is skipped without costing the rest of the plan |
| 63 | + now = LONDON.localize(datetime(2028, 9, 1, 12, 0)) |
| 64 | + planned = [_window("not-a-date", "09-01 13:00:00"), {"start": None, "end": "09-01 13:00:00"}, {"end": "09-01 13:00:00"}, _window("02-29 01:00:00", "02-29 02:00:00"), _window("09-01 11:00:00", "09-01 13:00:00")] |
| 65 | + windows = parse_car_plan_windows(planned, now, LONDON) |
| 66 | + if len(windows) != 1 or not in_car_plan_window(windows, now): |
| 67 | + print("ERROR: malformed entries should be skipped and the valid window kept, got {}".format(windows)) |
| 68 | + failed = True |
| 69 | + |
| 70 | + if not failed: |
| 71 | + print("**** All parse_car_plan_windows tests PASSED ****") |
| 72 | + return failed |
0 commit comments