EV-15 round 2: the grace opens when the 0 A write is sent
checks / unit (pull_request) Successful in 20m56s
infra-pin / fresh (pull_request) Successful in 1s

The writer runs beside the cycle and the car can stop before the write
returns, so no sequence existed yet and the latch set. Window now opens at
send (zero_sent_at), cleared on return; ends on another writer in the tail too.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
glenn schrooyen
2026-11-17 15:24:30 +01:00
co-authored by Claude
parent 07f703d1bc
commit 6d7046fc45
4 changed files with 100 additions and 9 deletions
@@ -128,7 +128,9 @@ up to 1.8 kW; the 40 Wh floor fixes that.)
PEAK_GUARD_FLOOR_LIMITED and one `goodwe_peak_soc_floor`. Now, only for grading
(`car_cycle`), `ctl_car._car_own_pause` lets the car's fresh, not stuck figure
(`CarTracker.car_w_paused`, 0 W while paused) stand in without corroboration:
from our 0 A write (`seq` exists) until the current is set again, at most
from the moment our 0 A write is SENT (`zero_sent_at`: the cycle runs beside
the writer and the car stops before the write returns), until the current is
set again, at most
`OWN_PAUSE_MAX_S` (85 s, the sequence's own waits), then at most one
corroboration window (60 s). It ends at once on Off, Stop, shadow, unplug,
another writer, or a refused select or write (`seq["failed"]`). The energy it
+9 -2
View File
@@ -126,6 +126,7 @@ class ManualWriter:
self.phases_gave_up = False
self.undone = 0 # sequences undone before the select
self.phase_done_at = None # EV-15: our phase change ended well then
self.zero_sent_at = None # EV-15: our 0 A write is out, not yet back
# ------------------------------------------------------------------ user
def user_changed(self) -> None:
@@ -293,11 +294,17 @@ class ManualWriter:
async def _begin(self, drv, r, cur, n, now, tracker) -> None:
"""Step 1 of the safe sequence: 0 A. A charger that does not take it
stops the sequence before the phases are touched."""
ok = await self._write(drv, 0.0, cur, now, tracker)
self.phase_done_at = None
# EV-15: the car stops while the write waits for its read-back, and the
# cycle runs beside it: the window opens when the write is SENT.
self.zero_sent_at = now
try:
ok = await self._write(drv, 0.0, cur, now, tracker)
finally:
self.zero_sent_at = None
if not ok:
self._failed(now)
return
self.phase_done_at = None
self.seq = {"stage": "zero", "t0": now, "n": n, "tries": 0,
"prev": cur}
+7 -4
View File
@@ -214,19 +214,22 @@ class CarMixin:
return self.car.car_w_trusted(self.car_now())
def _car_own_pause(self, mono) -> bool:
"""EV-15: is the car held at 0 A by the add-on's OWN phase change (its
0 A write until the current is set again, then at most one
corroboration window)? No when it was aborted (Off, Stop, shadow,
"""EV-15: is the car held at 0 A by the add-on's OWN phase change (from
the moment its 0 A write is SENT, the cycle runs beside the write,
until the current is set again, then at most one corroboration window)? No when it was aborted (Off, Stop, shadow,
unplug, another writer), a write was refused, or it ran past its own
maximum."""
wr = getattr(self, "car_writer", None)
if wr is None or (wr.seq is None and wr.phase_done_at is None):
if wr is None or (wr.seq is None and wr.phase_done_at is None
and wr.zero_sent_at is None) or wr.hold:
return False
r = self.car.reading
if not (self.car_manual() and self.car_writing()
and r is not None and r.available and r.state != "A"):
return False
q = wr.seq
if q is None and wr.zero_sent_at is not None: # sent, not yet back
return 0.0 <= mono - wr.zero_sent_at <= OWN_PAUSE_MAX_S
if q is not None:
return not q.get("failed") and 0.0 <= mono - q["t0"] \
<= OWN_PAUSE_MAX_S
+81 -2
View File
@@ -42,8 +42,26 @@ def at(t, fn):
return {"t": t, "fn": fn}
def beside(plant, c):
"""The writer runs BESIDE the cycle, as in production (`car_kick`): its 1 s
poll for the read-back lands after the cycle's own read, and the cycle does
not wait for the writer (`ev04_peak.drive` does)."""
gate = asyncio.Event()
async def sleep(_s):
gate.clear()
await gate.wait()
async def settle():
gate.set()
for _ in range(5):
await asyncio.sleep(0)
c.charger._sleep = sleep
c.car_settle = settle
def run(house, cap, t_res, t_sw, new, *, extra=None, mode="manual",
sim_fn=None, dur=895, plug=True):
sim_fn=None, dur=895, plug=True, side=False):
"""Reserve at `t_res`, our phase change to `new` at `t_sw`. Returns the
run, a per-second trace (second, own-pause, explains, red rows) and the
alarms."""
@@ -69,13 +87,16 @@ def run(house, cap, t_res, t_sw, new, *, extra=None, mode="manual",
ev = {t: note for t in range(0, dur)}
ev[t_res] = lambda p, c: (reserve(p, c), note(p, c))
ev[t_sw] = lambda p, c: (switch(p, c), note(p, c))
if side:
ev[t_sw - 2] = lambda p, c: (beside(p, c), note(p, c))
for t, fn in (extra or {}).items():
prev = ev.get(t, note)
ev[t] = (lambda p, c, prev=prev, fn=fn: (fn(p, c), prev(p, c)))
sim = None
if sim_fn is not None:
if sim_fn is not None or side:
from chargersim import SimCharger
sim = SimCharger(setpoint_a=16)
sim_fn = sim_fn or (lambda s: None)
sim_fn(sim)
r = P.drive(s81.constant(house), dur=dur, rating=12000.0, soc=60.0,
cap=cap, phases=(3 if new == 1 else 1), user_a=16, setpoint=16,
@@ -241,6 +262,20 @@ check("shadow mode: no grace", not c._car_own_pause(110.0))
c.shadow, c.auto = False, False
check("control stopped: no grace", not c._car_own_pause(110.0))
c.auto = True
c.car_writer.hold = True
check("another writer (hold): no grace", not c._car_own_pause(110.0))
c.car_writer.hold = False
c.car_writer.seq, c.car_writer.zero_sent_at = None, 100.0
check("0 A sent, not yet back: grace from the send, to the maximum, no more",
c._car_own_pause(100.0) and c._car_own_pause(100.0 + M)
and not c._car_own_pause(101.0 + M))
c.car_writer.zero_sent_at = None
c.car_writer.phase_done_at = 200.0
c.car_writer.hold = True
check("another writer in the 60 s after the current is set: no grace",
not c._car_own_pause(210.0))
c.car_writer.hold, c.car_writer.phase_done_at = False, None
c.car_writer.seq = {"stage": "settle", "t0": 100.0}
c.car.reading = CH.ChargerReading(available=True, state="A")
check("unplugged: no grace", not c._car_own_pause(110.0))
c.car.reading = CH.ChargerReading(available=True, state="B")
@@ -256,6 +291,50 @@ check("a stale reading stands in for nothing", tk.car_w_paused(100.0) is None)
tk.stuck_now = True
check("a stuck reading stands in for nothing", tk.car_w_paused(5.0) is None)
# =========================================================================== #
print("\n5. THE WRITER RUNS BESIDE THE CYCLE (review F-1)")
# =========================================================================== #
# The car stops (state B) in the same Home Assistant update as the 0 A
# read-back; the write has not returned yet, so no sequence exists. The window
# opens when the write is SENT (`ManualWriter.zero_sent_at`).
def lagged(lag, delay):
def f(s):
s.lag_s, s.stop_delay_s = lag, delay
return f
for lag, delay in ((2, 0), (0, 0), (2, 3), (5, 2)):
r, tr, al = run(3000.0, 6050.0, 300, 600, 1, side=True,
sim_fn=lagged(lag, delay))
nm = f"beside the cycle, lag {lag} s, stop delay {delay} s"
limited = [x for x in tr if x[0] >= 600 and x[6]]
check(f"{nm}: our sequence ran (select once, then 16 A again)",
len([x for x in r["sim"].writes("phases")]) == 1
and r["sim"].phases_allowed == 1
and [x[2] for x in r["sim"].writes("current")][-2:] == [0.0, 16.0])
check(f"{nm}: no PEAK_GUARD_FLOOR_LIMITED, no goodwe_peak_soc_floor",
al == [])
check(f"{nm}: no red row, and the car explains every limited cycle",
reds(tr) == [] and all(x[2] for x in limited))
def not_taken(plant, c):
plant.ch.refuse = "ignore" # HA accepts, the charger does nothing
r, tr, al = run(3000.0, 6050.0, 300, 600, 1, side=True,
sim_fn=lagged(0, 0), extra={600: not_taken})
check("the 0 A write is not taken (read-back never follows): the window was "
"open while it waited", any(x[1] for x in tr if 600 <= x[0] <= 606))
own = [x[0] for x in tr if x[1]]
runs = 1 + len([1 for a, b in zip(own, own[1:]) if b - a > 1])
check("...and no grace after the refusal (the retry, RETRY_S later, opens it "
"again only while that write waits: at most 3 runs of 11 s), no "
"sequence made", not hit(tr, 613, 40) and r["c"].car_writer.seq is None
and 1 <= runs <= 3 and len(own) <= 3 * 11)
check("...the car went on charging and nothing was written but the 0 A",
r["sim"].power_w > 5000.0 and not r["sim"].writes("phases"))
print()
if fails:
print(f"FAIL: {len(fails)} check(s)")