"""EV-54: after the quarter's refused cut tries, Solar's normal control goes on, and the cut is tried again at the next quarter start while one is needed. `python3 -B test_ev54.py` Rows T1-T13 and T15-T23 of reports/EV-54-test-plan.md (T14 and T25-T35, the drop rule, are in test_ev54_drop.py; T24 is the run of the existing files). The real Controller on `tools/ev20_sun.py` through `tools/ev54_kit.py`; Shape C is N=2, both batteries at 10 %, the car at 10 A on 3 phases, the house 3000 W and the sun 0 from 960; the second period runs 900-1800. "Refusal" is the charger ignoring a write ("ignore") or Home Assistant refusing it ("error"). Golden values marked bdfd3e6 were taken on that tree with `EV54_GOLDEN=1 python3 -B test_ev54.py`. Mutating a module this file grades? Read MUTATION.md first (purge __pycache__). """ import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "tools")) from app import carmode, messages # noqa: E402 from ev20_kit import LOG, check, fails, act, step_fn, const # noqa: E402 from ev21_kit import (ALARMS, digest, merge, shape, view_guard, # noqa: E402 view_writes, refuse_n1, set_resp) from ev54_kit import (HOLD_TEXT, Q3_TEXT, W54, as_off, cut_writes, # noqa: E402 house_c, key_change, never54, refused_by_key, row, rows, run, sun_c, t_lift, twin, window, writes, dropped, first, nth, present) n_log0 = LOG.mark() TAKE = os.environ.get("EV54_GOLDEN") == "1" CUT = messages.ID_CAR_CUT GOLDEN = { 'T10 [C]': ('938225cc1dd6d8f8', 'ba8e36dfa3011013'), 'T10 [W]': ('dfb6d075dd1f2330', 'eec4c29653ce1e3e'), 'T10 [M]': ('385ae1fbf07b25d6', 'a52b1c929747cd07'), 'T11 [Manual]': ([(1601.0, 8.0), (1671.0, 8.0), (1741.0, 8.0)], ['WARNING']), 'T11 [Off]': [(1080.0, 0.0, False)], } got = {} def golden(name, value): got[name] = value if TAKE: print(f"GOLDEN {name!r}: {value!r},") return GOLDEN.get(name) def refused(r, t0=0, t1=1e9): """(t, a) of the refused cut tries in [t0, t1).""" return [(t, a) for t, a, ok, _why, _k in cut_writes(r, t0, t1) if not ok] def gaps(ts): return [b - a for a, b in zip(ts, ts[1:])] def near(t, want, lo, hi): return t is not None and want - lo <= t <= want + hi def amber(r, t0, t1, rid="charger_refused"): return [x["t"] for x in r["trace"] if t0 <= x["t"] < t1 and rid in (x.get("amber") or [])] def gave_up_at(r, t0=0, t1=1e9): return [x["t"] for x in rows(r, t0, t1) if x["gave_up"]] def roll_try(name, r, kc, n, *, value=0.0, taken_by=None, hold_s=0.0, graded=True): """T4's bound: the new key's first try in its first cycle (at N=2 up to max_hold_s before :00), then (if `taken_by`) the next try 60 s or more later is taken, read back within 2 s, and (`graded`, T4's own row) graded can-cut from that cycle (+1 s) with the CAR_CUT note once.""" w2 = [(t, a, ok) for t, a, ok, _w, _k in cut_writes(r, 1800 - hold_s - 1)] t1 = nth(w2, 0) lo = 1800 - (hold_s if n == 2 else 0) - 1 ok1 = present(t1) and lo <= t1[0] <= 1801 and abs(t1[1] - value) < 0.01 \ and present(kc) and abs(t1[0] - kc) <= 1 if taken_by is None: check(f"{name}: the new key ({kc}) is tried in its first cycle " f"({t1}, bound [{lo}, 1801]), as {value} A", ok1) return t2 = nth(w2, 1) ok2 = present(t1, t2) and t2[2] and not t1[2] \ and t2[0] - t1[0] >= carmode.RETRY_S and t2[0] <= taken_by + 61 rb = [x["t"] for x in rows(r, t2[0] if present(t2) else 1e9) if abs(x["rb"] - value) < 0.01] can = [x["t"] for x in rows(r, t2[0] if present(t2) else 1e9) if x["can"]] cuts = [t for t, n_ in r["raised"] if n_ == CUT and t >= 1790] check(f"{name}: the new key ({kc}) tried in its first cycle ({t1}, bound " f"[{lo}, 1801]) and refused; the next try {t2} 60 s or more later " f"is taken, read back by {first(rb)}, can-cut from {first(can)}; " f"CAR_CUT once ({cuts})", ok1 and ok2 and bool(rb) and rb[0] <= t2[0] + 2 and (not graded or (bool(can) and can[0] <= rb[0] + 1 and len(cuts) == 1))) # =========================================================================== # print("\n1. SOLAR GOES ON AFTER THE QUARTER'S TRIES (T1, T2, T3)") # =========================================================================== # T1R = {} for kind in ("ignore", "error"): r = run(house_c((1250, 900.0)), sun_c((1250, 9000.0)), dur=1800, events=window({}, 959, 1100, kind)) T1R[kind] = r tr = refused(r, 900, 1800) ts = [t for t, _a in tr] tl = t_lift(r, 1100, 1800) nz = [(t, a, why) for t, a, ok, why in writes(r, (tl or 1e9) + 1, (tl or 1e9) + 131) if a > 0.0 and ok] check(f"T1 [{kind}]: tries {tr}: 0 A at 960+-1, 1020+-2, 1080+-2, no " f"4th in 900-1800", len(tr) == 3 and all(a == 0.0 for _t, a in tr) and near(nth(ts, 0), 960, 1, 1) and near(nth(ts, 1), 1020, 2, 2) and near(nth(ts, 2), 1080, 2, 2)) check(f"T1 [{kind}]: the cap lifts at {tl} (1370+-2); the drop then: " f"sent_a 10, taken, plan 10 ({dropped(r, tl or 0, 10.0)}); a " f"non-zero Solar write within 130 s ({nz[:2]}); gave_up never " f"({gave_up_at(r, 900, 1800)[:2]})", near(tl, 1370, 2, 2) and dropped(r, tl or 0, 10.0) is not None and bool(nz) and 6.0 <= nz[0][1] <= 16.0 and nz[0][2] == "Solar" and not gave_up_at(r, 900, 1800)) never54(f"T1 [{kind}]", r) ev2 = window({}, 959, None) T2 = run(house_c(), sun_c(), dur=1800, events=ev2) tr = refused(T2, 900, 1800) ts = [t for t, _a in tr] nz2 = [(t, a) for t, a, _ok, _why in writes(T2, 900, 1800) if a > 0.0] caps = sorted({x["cap"] for x in rows(T2, 961, 1800)}, key=str) check(f"T2: a cut needed all quarter: exactly 3 tries ({tr}), all 0 A, gaps " f"{gaps(ts)} >= 60; no non-zero Solar write ({nz2[:2]}); the cap stays " f"0 ({caps})", len(tr) == 3 and all(a == 0.0 for _t, a in tr) and all(g >= 60 for g in gaps(ts)) and not nz2 and caps == [0.0]) never54("T2", T2) # T1 continued, in the next period: in 900-1800 the import of 960-1250 keeps # the projection near the aim, so a sun drop there makes a new cut due, and # with the quarter's tries used up none goes out (Q1). From 1800 the period # is fresh: the sun falls to 7 A worth at 1900, under 6 A worth at 2000. r = run(house_c((1250, 900.0)), sun_c((1250, 9000.0), (1900, 5900.0), (2000, 4400.0)), dur=2350, events=window({}, 959, 1100)) w3 = [(t, a, ok, why) for t, a, ok, why in writes(r, 1081, 2350)] down = [x for x in w3 if 1900 <= x[0] <= 1932] hold6 = [x for x in w3 if 2000 <= x[0] <= 2032 and x[1] == 6.0] pause = [x for x in w3 if 2298 <= x[0] <= 2302 and x[1] == 0.0] prev = [x for x in w3 if x[0] < 1900] caps3 = [x["t"] for x in rows(r, 1800, 2350) if x["cap"] is not None] check(f"T3: after the drop Solar follows the spare: a step-down by 1932 " f"({down}), the 6 A hold by 2032 ({hold6}), the pause 0 A 'Solar' at " f"2300+-2 ({pause}); all taken ({[x for x in w3 if not x[2]]}); no cut " f"in that period ({caps3[:2]})", bool(down) and bool(prev) and down[0][1] < prev[-1][1] and bool(hold6) and bool(pause) and pause[0][3] == "Solar" and all(x[2] for x in w3) and not caps3) never54("T3", r) # =========================================================================== # print("\n2. THE NEXT QUARTER TRIES AGAIN (T4, T5, T6)") # =========================================================================== # T4R = {} for n in (2, 1): for kind in ("ignore", "error"): r = run(house_c(), sun_c(), dur=1950, n=n, events=window({}, 959, 1830, kind)) T4R[(n, kind)] = r hold_s = r["c"].p1.averager.max_hold_s rbk = refused_by_key(r) kc = key_change(r, 1700, 1900) k900 = [v for k, v in rbk.items() if k == row(r, 1000)["key"]] \ if present(row(r, 1000)) else [] check(f"T4 [N={n}, {kind}]: 3 refused tries in 900-1800 ({k900})", bool(k900) and len(k900[0]) == 3) roll_try(f"T4 [N={n}, {kind}]", r, kc, n, taken_by=1830, hold_s=hold_s) never54(f"T4 [N={n}, {kind}]", r) T5R = {} for tag, socs in (("both at 10 %", None), ("a 10 %, b 60 %", {"a": 10.0, "b": 60.0})): r = run(house_c(), sun_c(), dur=3600, socs=socs, events=window({}, 959, None)) T5R[tag] = r rbk = refused_by_key(r, 900, 3600) per = sorted(len(v) for v in rbk.values()) allt = sorted(t for v in rbk.values() for t in v) other = [(t, a) for t, a, ok, _why in writes(r, 900, 3600) if t not in allt] vals = sorted({a for t, a, ok, _w in writes(r, 900, 3600)}) check(f"T5 [{tag}]: refusal never lifted: exactly 3 tries in each of 3 " f"quarter keys ({ {k: v for k, v in rbk.items()} }), all 0 A " f"({vals}), gaps {gaps(allt)} >= 60 within and across; no other " f"write ({other[:3]})", per == [3, 3, 3] and vals == [0.0] and all(g >= 60 for g in gaps(allt)) and not other) never54(f"T5 [{tag}]", r) r = run(house_c((1790, 900.0)), sun_c((1790, 9000.0)), dur=2700, events=window({}, 959, 1900)) kc = key_change(r, 1700, 1900) bad = [(t, a, why) for t, a, ok, why in writes(r, 1795, 2700) if a == 0.0 or why == "peak cut"] nxt = first(x for x in writes(r, (kc or 1e9) + 1, 2700)) check(f"T6: the new quarter starts under the aim: no 0 A and no 'peak cut' " f"write from 1795 ({bad[:3]}); the drop at the key change {kc} " f"(1800+-2): {dropped(r, kc or 0, 10.0)}; the next write a normal one " f"({nxt})", not bad and near(kc, 1800, 2 + r["c"].p1.averager.max_hold_s, 2) and dropped(r, kc or 0, 10.0) is not None and (nxt is None or not present(nxt) or nxt[3] == "Solar")) never54("T6", r) # =========================================================================== # print("\n3. AS OFF WHILE THE CUT IS REFUSED (T7)") # =========================================================================== # for name, r, win in ( ("T1 [ignore]", T1R["ignore"], (961, 1360)), ("T2", T2, (961, 1800)), ("T4 [N=2, ignore]", T4R[(2, "ignore")], (961, 1855)), ("T5 [both at 10 %]", T5R["both at 10 %"], (961, 3600))): as_off(f"T7 {name}", r, twin(r), ALARMS, refused=win) evw = merge(window({}, 959, 1830), 900, set_resp(20.0)) r = run(house_c(), sun_c(), dur=1900, socs={"a": 60.0, "b": 60.0}, events=evw) as_off("T7 [Shape W, refusal 959-1830]", r, twin(r, {900: set_resp(20.0)}), ALARMS, refused=(961, 1800)) never54("T7 [Shape W]", r) # =========================================================================== # print("\n4. THE NOTICE (T8) AND SOLAR'S OWN REFUSED WRITE (T9)") # =========================================================================== # for name, r, t1 in (("T2", T2, 1800), ("T5", T5R["both at 10 %"], 3600)): ts = [t for t, _a in refused(r, 900, 1800)] q3 = r["w"].lines(Q3_TEXT) q3w = [(t, lv) for t, lv, _m in q3 if lv == "WARNING"] old = [t for t, _lv, _m in r["w"].lines("until you press Solar")] amb = amber(r, (nth(ts, 2) if present(nth(ts, 2)) else 1e9) + 1, 1800) want_amb = list(range(int(nth(ts, 2)) + 1, 1800)) \ if present(nth(ts, 2)) else [None] check(f"T8 [{name} run]: at the 3rd refused try ({nth(ts, 2)}) one WARNING " f"'{Q3_TEXT}' ({q3w}) over the run (DEBUG after: " f"{[(t, lv) for t, lv, _m in q3 if lv != 'WARNING'][:3]}); no " f"'until you press Solar' ({old[:2]}); the amber row from the 3rd " f"try to 1800 ({len(amb)} of {len(want_amb)} s); gave_up never " f"({gave_up_at(r)[:2]})", len(q3w) == 1 and present(nth(ts, 2)) and abs(q3w[0][0] - nth(ts, 2)) <= 1 and not old and [float(t) for t in want_amb] == amb and not gave_up_at(r)) r = run(const(900.0), step_fn((1000, 6700.0), base=7900.0), dur=1900, events=window({}, 999, None)) w9 = writes(r, 999, 1900) hold = [(t, lv) for t, lv, _m in r["w"].lines(HOLD_TEXT)] third = nth(w9, 2) amb = amber(r, (third[0] if present(third) else 1e9) + 1, 1900) check(f"T9: Solar's own step-down refused 3 times ({w9[:5]}): the existing " f"'{HOLD_TEXT}' once ({hold}), the amber row after it ({len(amb)} s), " f"no write after the 3rd, also across 1800 ({w9[3:5]})", len(w9) == 3 and all(not ok for _t, _a, ok, _w in w9) and len({a for _t, a, _o, _w in w9}) == 1 and len(hold) == 1 and hold[0][1] == "WARNING" and len(amb) >= 1800 - third[0] - 2) never54("T9", r) # =========================================================================== # print("\n5. A CHARGER THAT TAKES THE CUT; MANUAL AND OFF (T10, T11)") # =========================================================================== # for kind in ("C", "W", "S", "F", "M"): rec = W54() r = shape(kind, dur=1300, on_cycle=rec) r["w"] = rec cw = cut_writes(r, 950) vals = [a for _t, a, _ok, _w, _k in cw] check(f"T10 [{kind}]: the charger takes the cut: one write per value, " f"each taken, no retry ({[(t, a, ok) for t, a, ok, _w, _k in cw]})", all(ok for _t, _a, ok, _w, _k in cw) and len(vals) == len(set(vals))) if kind in ("C", "W", "M"): d = (digest(view_writes(r)), digest(view_guard(r))) want = golden(f"T10 [{kind}]", d) check(f"T10 [{kind}]: view_writes and view_guard as bdfd3e6 " f"({d} vs {want})", d == want) import opt11_identity as rig # noqa: E402 st = rig._Store() st.data["car_current"], st.data["car_phases"] = 10.0, 3 evm = window({1600: act("car_current", 8)}, 1599, None) r = run(house_c(), sun_c(), dur=1900, mode="manual", store=st, events=evm) wm = [(x[0], x[2]) for x in r["sim"].log if x[1] == "current" and x[0] >= 1590] mt = [lv for _t, lv, _m in r["w"].lines( "stops asking until you change it: CHARGER_REFUSED.")] d = (wm, mt) want = golden("T11 [Manual]", d) check(f"T11 [Manual]: a change refused 3 times ({wm}), then the roll at " f"1800: no 4th; the Manual text once ({mt}); as bdfd3e6 ({want})", len(wm) == 3 and mt == ["WARNING"] and d == want) evo = window({1030: act("car_mode", "off")}, 959, None) r = run(house_c(), sun_c(), dur=1300, events=evo) wo = [(t, a, ok) for t, a, ok, _w in writes(r, 1025)] want = golden("T11 [Off]", wo) check(f"T11 [Off]: Solar left to Off during a refused cut: Off's way-out " f"0 A writes {wo} as bdfd3e6's ({want})", wo == want and bool(wo)) # =========================================================================== # print("\n6. AN UNTRUSTED CAR (T12), A SILENT METER (T13)") # =========================================================================== # def no_energy(p, c): p.ch.no_energy = True ev12 = window({950: no_energy}, 959, 1830) r = run(house_c(h=9000.0), sun_c(), dur=1950, events=ev12) tr = refused(r, 900, 1795) check(f"T12: an untrusted car figure: the cut is 0 A, 3 tries in 900-1800 " f"({tr})", len(tr) == 3 and all(a == 0.0 for _t, a in tr)) roll_try("T12", r, key_change(r, 1700, 1900), 2, taken_by=1830, hold_s=r["c"].p1.averager.max_hold_s, graded=False) o = twin(r, {950: no_energy}) a_r = first(t for t, n in r["raised"] if n in ALARMS and t >= 900) a_o = first(t for t, n in o["raised"] if n in ALARMS and t >= 900) check(f"T12: the alarm at the Off twin's second +2 ({a_r} vs {a_o})", present(a_r, a_o) and a_r is not None and a_o is not None and abs(a_r - a_o) <= 2) never54("T12", r) def silent(on): def f(p, c): p.meter_on = not on return f seen13 = [] def fresh13(p, c): """Before the cycle (a refused write's read-back wait moves the model clock inside it): is the meter fresh for the cut (N9)?""" seen13.append((p.t, c._car_cut_meter_fresh())) ev13 = window({1780: silent(True), 1900: silent(False)}, 959, None) for t in range(1781, 1900): ev13 = merge(ev13, t, fresh13) r = run(house_c(), sun_c(), dur=2000, events=ev13) # The bound is EV-21's N9 (`_car_cut_meter_fresh`): stale_input_s, or # meter_max_age_s on `ha_signed` (the harness's meter source). stale_t = first(t for t, ok in seen13 if not ok) dark = [(t, a) for t, a, _ok, _w in writes(r, stale_t or 1e9, 1900)] nxt = first((t, a) for t, a, _ok, _w in writes(r, 1900)) k2 = [len(v) for k, v in refused_by_key(r, 1795).items()] check(f"T13: the meter silent 1780-1900, stale for the cut (N9) from " f"{stale_t}: no try while stale ({dark}); the first try at {nxt} " f"(within [1900, 1902]); at most 3 in the new key ({k2})", present(stale_t) and stale_t is not None and stale_t < 1860 and not dark and present(nxt) and nxt is not None and 1900 <= nxt[0] <= 1902 and all(k <= 3 for k in k2)) never54("T13", r) # =========================================================================== # print("\n7. A RAISING WRITE (T15), A RESTART (T16), THE KEY (T17)") # =========================================================================== # calls15 = [] def boom(p, c): drv = c.charger drv._ev54_wc = drv.write_current async def wc(a, **kw): calls15.append((p.t, a)) raise RuntimeError("EV-54 T15: the write raised") drv.write_current = wc def unboom(p, c): c.charger.write_current = c.charger._ev54_wc m15 = LOG.mark() r = run(house_c(), sun_c(), dur=1950, events={959: boom, 1830: unboom}) c1 = [t for t, _a in calls15 if t < 1795] c2 = [t for t, _a in calls15 if t >= 1795] step_w = [m for m in LOG.since(m15, "ERROR") + LOG.since(m15, "WARNING") if "charger step failed" in m] tb15 = LOG.tracebacks(m15) kc = key_change(r, 1700, 1900) taken2 = [(t, a) for t, a, ok, _w in writes(r, 1830) if ok and a == 0.0] check(f"T15: a write that raises counts as a refused try: {c1} in 900-1800 " f"(3, >= 60 s apart), never once per cycle; the new key's try at " f"{c2[:1]} (key {kc}), the next taken {taken2[:1]}; step-failed " f"warnings {len(step_w)} (<= 1), Tracebacks {len(tb15)}", len(c1) == 3 and all(g >= 60 for g in gaps(c1)) and bool(c2) and present(kc) and kc is not None and abs(c2[0] - kc) <= 1 and bool(taken2) and taken2[0][0] - c2[0] >= 60 and len(step_w) <= 1 and not tb15) r1 = run(house_c(), sun_c(), dur=1050, events=window({}, 959, None)) r2 = run(house_c(), sun_c(), t0=1050.0, dur=850, sim=r1["sim"], store=r1["store"], plug=False, events={1830: lambda p, c: setattr(p.ch, "refuse", None)}) w16 = [(t, a, ok, why) for t, a, ok, why in writes(r2, 1050, 1795)] ts = [t for t, _a, _ok, _w in w16] keys = [k for k in r1["store"].data if "cut" in str(k).lower() or "tries" in str(k).lower()] check(f"T16: 2 refused tries, then a restart at 1050: D2's 0 A at 1050+-1 " f"'start-up' ({w16[:1]}); its retries >= 60 s apart ({gaps(ts)}), at " f"most 3 in 1050-1800 ({len(w16)}); no store key holds a count " f"({keys})", bool(w16) and near(w16[0][0], 1050, 1, 1) and w16[0][1] == 0.0 and w16[0][3] == "start-up" and all(g >= 60 for g in gaps(ts)) and len(w16) <= 3 and all(a == 0.0 for _t, a, _o, _w in w16) and not keys) roll_try("T16", r2, key_change(r2, 1700, 1900), 2, taken_by=1830, hold_s=r2["c"].p1.averager.max_hold_s) never54("T16", r2) def none_proj(p, c): orig = c._car_cut_tick def tick(proj, guard, target, mono): if 1801 <= p.t <= 1803: proj = None return orig(proj, guard, target, mono) c._car_cut_tick = tick r = run(house_c(), sun_c(), dur=2700, events=window({1795: none_proj}, 959, None)) rbk = refused_by_key(r, 1700, 2700) keyed = {k: v for k, v in rbk.items() if k is not None} check(f"T17 (a): the early roll, then no projection 1801-1803, then fresh: " f"the count reset once ({keyed}: exactly 3 in the new key, the first " f"at the roll)", len(keyed) == 1 and [len(v) for v in keyed.values()] == [3]) never54("T17 (a)", r) def jump(dt): def f(p, c): p.utc_off = dt return f r = run(house_c(), sun_c(), dur=2000, events=window({1790: jump(-3600.0)}, 959, None)) allt = sorted(t for t, _a, ok, _w in writes(r, 900) if not ok) check(f"T17 (b): the wall clock jumps -3600 s at 1790: tries {allt} stay " f">= 60 s apart on the monotonic clock", bool(allt) and all(g >= 60 for g in gaps(allt))) never54("T17 (b)", r) # =========================================================================== # print("\n8. A PHASE CHANGE (T18), A LATE TAKE (T19), COVER OFF (T20)") # =========================================================================== # kw18 = dict(n=2, opts={"ev_phases": "auto"}, phases=1) r0 = run(const(900.0), const(9280.0), dur=700, **kw18) log0 = r0["c"].car_solar.log sel = [t for k, _a, t, _ok in log0 if k == "phases"] zb = [t for k, a, t, _ok in log0 if k == "current" and a == 0.0 and sel and t <= sel[0]] tb = int(nth(zb, -1)) if present(nth(zb, -1)) else 400 q1 = (tb // 900 + 1) * 900 ev18 = window({}, tb - 1, None) r = run(step_fn((tb + 1, 9280.0 + 3400.0 + 2000.0), base=900.0), const(9280.0), dur=q1 + 100, events=ev18, **kw18) ph = [(t, n, ok) for k, n, t, ok in r["c"].car_solar.log if k == "phases" and t >= tb - 1] nz = [(t, a) for t, a, _ok, _w in writes(r, tb - 1) if a > 0.0] cw = [(t, a, ok) for t, a, ok, _w, k in cut_writes(r, tb - 1, q1)] sw = [x["t"] for x in rows(r, tb + 5, q1) if x["switch"]] nq = first((t, a) for t, a, _ok, _w, _k in cut_writes(r, q1 - 15)) check(f"T18: Solar's own change sent its 0 A at {tb}, the cut due from " f"{tb + 1}, the charger refusing: no select ({ph}), no closing current " f"({nz[:2]}); at most 3 cut tries in the quarter ({cw}); the plan's " f"switch ended ({sw[:2]}); the next quarter's try {nq} is 0 A", bool(zb) and not ph and not nz and 1 <= len(cw) <= 3 and not sw and present(nq) and nq is not None and nq[1] == 0.0) never54("T18", r) def late_take(p, c): p.ch.refuse = None p.ch.set_current(0) r = run(house_c(), sun_c(), dur=1300, events=window({1110: late_take}, 959, None)) set_l = [(t, m) for t, _lv, m in r["w"].lines("Car: set") if t >= 900] after3 = [(t, a) for t, a, _ok, _w in writes(r, 1081)] can = [x["t"] for x in rows(r, 1110) if x["can"]] cuts = [t for t, n in r["raised"] if n == CUT] new_ref = [t for t, _lv, _m in r["w"].lines("CHARGER_REFUSED") if t >= 1100] check(f"T19: the charger shows 0 A at 1110 after the 3rd try: one 'Car: set " f"from 10 A to 0 A (peak cut).' at 1110+-1 ({set_l}); no further try " f"({after3}); can-cut from {can[:1]}; CAR_CUT once ({cuts}); no new " f"CHARGER_REFUSED ({new_ref})", len(set_l) == 1 and near(set_l[0][0], 1110, 1, 1) and set_l[0][1] == "Car: set from 10 A to 0 A on 3 phases (peak cut)." and not after3 and bool(can) and can[0] <= 1111 and len(cuts) == 1 and not new_ref) r = run(house_c(), sun_c(), dur=1950, opts={"ev_batt_cover": False}, events=window({}, 959, 1830)) tr = refused(r, 900, 1795) check(f"T20: the cover off: the cut at once at 960+-1 and 3 tries in " f"900-1800 ({tr})", len(tr) == 3 and near(tr[0][0], 960, 1, 1)) roll_try("T20", r, key_change(r, 1700, 1900), 2, taken_by=1830, hold_s=r["c"].p1.averager.max_hold_s, graded=False) never54("T20", r) # =========================================================================== # print("\n9. TWO EPISODES (T21), A 50 W CAR (T22), A PRESS (T23)") # =========================================================================== # r = run(house_c(), sun_c((961, 9000.0), (1400, 0.0)), dur=1700, opts={"ev_start_min": 1}, events=window({}, 959, 1800)) tr = refused(r, 900, 1800) ts = [t for t, _a in tr] tl = t_lift(r, 961, 1300) check(f"T21: episode 1 (lifted at {tl}) and episode 2 from 1400 share the " f"quarter's 3 refused tries ({tr}): 960, 1020, then 1400+-1; none " f"after", len(tr) == 3 and near(nth(ts, 2), 1400, 0, 1) and present(tl) and tl is not None and nth(ts, 1) < tl) never54("T21", r) def car50(p, c): p.ch.target_w = lambda: 50.0 ev22 = window({900: car50}, 959, None) r = run(house_c(h=9000.0), sun_c(), dur=1800, events=ev22) o = twin(r, {900: car50}) a_r = first(t for t, n in r["raised"] if n in ALARMS and t >= 900) a_o = first(t for t, n in o["raised"] if n in ALARMS and t >= 900) tr = refused(r, 900, 1800) check(f"T22: a 50 W car on 10 A: the house alarm at the Off twin's second " f"({a_r} vs {a_o}); tries {tr} (<= 3)", present(a_r, a_o) and a_r is not None and a_o is not None and abs(a_r - a_o) <= 2 and len(tr) <= 3) never54("T22", r) for tag, ev in (("Solar pressed at 1100", {1100: act("car_mode", "solar")}), ("Off at 1100, Solar at 1200", {1100: act("car_mode", "off"), 1200: act("car_mode", "solar")})): r = run(house_c(), sun_c(), dur=1950, events=window(ev, 959, 1830)) tr = refused(r, 900, 1795) check(f"T23 [{tag}]: no 4th cut try in 900-1800 ({tr})", len(tr) == 3) roll_try(f"T23 [{tag}]", r, key_change(r, 1700, 1900), 2, taken_by=1830, hold_s=r["c"].p1.averager.max_hold_s) never54(f"T23 [{tag}]", r) tb_all = LOG.tracebacks(n_log0) check(f"N13: no traceback in this file's runs ({tb_all[:1]})", not tb_all) if TAKE: print("(golden taken; no checks graded)") sys.exit(0) print(f"\n{'FAILED: ' + str(len(fails)) if fails else 'ALL PASSED'}") sys.exit(1 if fails else 0)