EV-05 round 3: a refused way-out 0 A is tried again; one cut per excess
checks / unit (pull_request) Successful in 24m5s
infra-pin / fresh (pull_request) Successful in 2s

Review of 17a7b29:
- Critical: every way out of Solar (Off, Stop, shadow, safety stop, charger
  error, stuck reading, no car) now owes a 0 A until the charger takes it:
  tried again 60 s apart, 3 times (EV-04's rule), then the gave-up row and
  CHARGER_REFUSED; never over another writer. Shutdown does not trust last_a
  while a write of ours is unconfirmed. The card no longer says paused while
  the 0 A is not taken.
- Major: an excess episode is taken off the car once, measured from the car's
  power when it began, until the car shows it and 15 s more.

Tests: refused-then-accepted 0 A for each exit, a 0 A never taken, shutdown
during a start write's read-back, a cut from 16 A with a slow car; the fuzz
checks refused exits of every kind (N9).

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
ems-agent
2026-11-18 12:15:00 +01:00
co-authored by Claude
parent 17a7b299d2
commit d2504efa82
9 changed files with 338 additions and 58 deletions
+104 -39
View File
@@ -410,6 +410,12 @@ UP_W, UP_S = 4500.0, 180.0 # auto phases: to 3 at this for this long
DOWN_W, DOWN_S = 4000.0, 300.0 # ... back to 1 under this for this long
ONE_PHASE_S = 10.0 # one phase drawing while 3 are allowed this long
ONE_PHASE_MIN_W = 500.0 # ... with the car drawing at least this
# EV-05 round 3: one excess episode is taken off the car ONCE, measured from
# the car's power when it began, until the car shows the lower power
# (within CUT_SEEN_W) and then CUT_SETTLE_S more for the meter to see it (a
# DSMR4 telegram is 10 s). Only then can a still standing excess cut again.
CUT_SEEN_W = 100.0
CUT_SETTLE_S = 15.0
def _num(v):
@@ -495,6 +501,7 @@ class SolarPlan:
self.unknown_since = None # the spare cannot be measured, since
self.up_since = self.down_since = None
self.cap_a = self.cap_key = None # the peak cut, for this period
self.ep = None # the cut episode: [base W, target W, seen]
self.why = self.pause_why = why
self.target_w = None
@@ -531,14 +538,27 @@ class SolarPlan:
self.target_w = target_w
# The peak guard's excess comes off the car first, at once (no 30 s
# rule, no hold). Nothing starts while the period heads over the aim.
if not (cut_w and cut_w > 0.0):
self.ep = None
if cut_w and cut_w > 0.0:
self.ok_since = None
if self.a > 0.0:
base = self.a * VOLTS * n_calc
cw = _num(car_w)
if cw is not None:
base = min(base, cw)
new = amps_for(base - float(cut_w), n_calc, clamp)
ep = self.ep
if ep is not None and ep[2] is None \
and (cw is None or cw <= ep[1] + CUT_SEEN_W):
ep[2] = now # the car shows the cut
if ep is None or (ep[2] is not None
and now - ep[2] >= CUT_SETTLE_S):
base = self.a * VOLTS * n_calc
if cw is not None:
base = min(base, cw)
ep = self.ep = [base, base, None]
# Measured from the episode's start, so the same excess is not
# taken off again while the meter still shows the old power.
new = amps_for(ep[0] - float(cut_w), n_calc, clamp)
if new is not None:
ep[1] = min(ep[1], new * VOLTS * n_calc)
if new is None or new < MIN_A:
self._pause("cut")
elif new < self.a:
@@ -634,18 +654,21 @@ class SolarPlan:
class SolarWriter(ManualWriter):
"""What Solar does to the charger, one `step()` per cycle. It shares
ManualWriter's write, phase sequence and other-writer watch; what differs
is that every way out ends at 0 A, written once, and that an interrupted
phase change is never undone to the old current."""
is that every way out ends at 0 A, and that an interrupted phase change is
never undone to the old current. The way-out 0 A is written until the
charger takes it (EV-04's rule: `RETRY_S` apart, `MAX_TRIES`, then the
gave-up row and warning), and once taken nothing more."""
def __init__(self):
super().__init__()
self.plan = SolarPlan()
self.engaged = False # Solar has run since it was chosen
self.reinit = True # start again from 0 A and the start rule
self.out = False # not writing: the 0 A has been tried
self.stand = False # no car: the standing 0 A has been tried
self.out = False # not writing (shadow, Stop, safety stop)
self.zero_for = None # the way out a 0 A is owed for
self.zero_ok = False # ... and the charger has taken it
self.inflight = None # a current being written, not yet back
self.fault = None # "error" | "stuck" while it holds
self.fault_done = False
self.warned = False # the charger error warning, once
self.single = False # a one-phase car on a 3-phase allowance
self._one_since = None
@@ -699,19 +722,55 @@ class SolarWriter(ManualWriter):
super().user_changed()
self.sent_a = None
async def _zero_once(self, drv, cur, now, tracker, force=False) -> None:
"""One try of 0 A on the way out. Never over another writer, and not
when the charger already shows 0 A (unless the reading is stuck, so
its 0 A means nothing: `force`)."""
if self.hold or (not force and cur is not None and cur <= TOL_A):
async def _write(self, drv, a, was, now, tracker) -> bool:
self.inflight = a # cleared only when the write returns
ok = await super()._write(drv, a, was, now, tracker)
self.inflight = None
return ok
def _zero_done(self) -> None:
"""No way out is under way any more: forget its 0 A and its tries."""
if self.zero_for is not None:
self.zero_for, self.zero_ok = None, False
self.fails, self.fail_at, self.gave_up = 0, None, False
async def _exit_zero(self, why, drv, cur, now, tracker,
force=False) -> None:
"""0 A on the way out (`why`), until the charger takes it: a write
that is not taken is tried again `RETRY_S` later, `MAX_TRIES` times,
then the gave-up row and warning. Once taken, or once the charger
shows 0 A, nothing more. Never over another writer. `force`: the
reading is stuck, so its 0 A means nothing; only a taken write
counts."""
if self.zero_for != why:
self.zero_for, self.zero_ok = why, False
self.fails, self.fail_at, self.gave_up = 0, None, False
if self.zero_ok or self.hold:
return
if not force and cur is not None and cur <= TOL_A:
self.zero_ok = True
self.fails, self.fail_at = 0, None
return
if self.gave_up or (self.fail_at is not None
and now - self.fail_at < RETRY_S):
return
self.zero_at, self.sent_a = now, 0.0
await self._write(drv, 0.0, cur, now, tracker)
if await self._write(drv, 0.0, cur, now, tracker):
self.zero_ok = True
self.fails, self.fail_at = 0, None
else:
self._failed(now)
@property
def zero_owed(self) -> bool:
"""A way-out 0 A the charger has not taken yet (for the card)."""
return self.zero_for is not None and not self.zero_ok and not self.hold
def _leave(self) -> None:
self.engaged, self.seq, self.would = False, None, None
self.out = self.stand = False
self.fault, self.fault_done = None, False
self.out = False
self._zero_done()
self.fault = None
self.reinit = True
self.sent_a = None
self.plan.reset(self.plan.n)
@@ -748,21 +807,22 @@ class SolarWriter(ManualWriter):
self.would = None
if not self.engaged:
return
if mode != MANUAL: # Off: 0 A once, then nothing
if mode != MANUAL: # Off: 0 A until taken, then nothing
self.seq = None
await self._zero_once(drv, cur, now, tracker)
await self._exit_zero("off", drv, cur, now, tracker)
if self.zero_owed:
return # tried again next cycle (or gave up)
self._leave() # Manual writes its own current
return
self.engaged = True
if sun is None:
return
if not writing:
# Shadow, Stop, a safety stop, stopping: 0 A once, then nothing.
# The plan goes on, so the card says what it would set.
# Shadow, Stop, a safety stop, stopping: 0 A until taken, then
# nothing. The plan goes on, so the card says what it would set.
self.seq = None
if not self.out:
self.out = True
await self._zero_once(drv, cur, now, tracker)
self.out = True
await self._exit_zero("out", drv, cur, now, tracker)
self._plan_step(sun, now, clamp)
self.would = (self.amps_out(clamp), self.n_calc)
self.reinit = True
@@ -776,31 +836,27 @@ class SolarWriter(ManualWriter):
# 0 A once, then no write until it clears; then the start rule.
self.seq = None
if self.fault != fault:
self.fault, self.fault_done = fault, False
self.fault = fault
if fault == "error" and not self.warned:
self.warned = True
_LOG.warning("The charger reports an error, so the add-on "
"sets the car to 0 A: CHARGER_ERROR.")
if not self.fault_done:
self.fault_done = True
await self._zero_once(drv, cur, now, tracker,
force=fault == "stuck")
await self._exit_zero(fault, drv, cur, now, tracker,
force=fault == "stuck")
self.plan.reset(self.plan.n, why=fault)
self.reinit = True
return
if self.fault is not None:
self.fault, self.fault_done = None, False
self.fault = None
self.warned = False
if r.state == "A" or status == CH.NONE_:
# No car: the standing current is 0 A, written once.
# No car: the standing current is 0 A, written until taken.
self.seq = None
if not self.stand:
self.stand = True
await self._zero_once(drv, cur, now, tracker)
await self._exit_zero("none", drv, cur, now, tracker)
self.plan.reset(self.plan.n, why="none")
self.reinit = False
return
self.stand = False
self._zero_done() # no way out under way now
if self.reinit:
self.reinit = False
self.seq = None
@@ -864,17 +920,26 @@ class SolarWriter(ManualWriter):
self._failed(now)
async def release(self, drv, r, now, tracker) -> None:
"""The add-on stops in Solar: 0 A, once, before the inverters go to
0 W. A phase change in the middle is not undone to the old current."""
"""The add-on stops in Solar: 0 A before the inverters go to 0 W, one
try (it is stopping). A phase change in the middle is not undone to
the old current. A write of ours that is not confirmed (cancelled
while it waited for its read-back) means the charger's current is not
known, so the 0 A is written."""
self.seq = None
if r is not None and r.available:
cur = r.allowed_a if r.allowed_a is not None else r.setpoint_a
else:
cur = None
if self.last_done is not None and tracker is not None \
if self.inflight is not None or self.attempted_a is not None:
cur = None # a write of ours is not confirmed: assume nothing
elif self.last_done is not None and tracker is not None \
and tracker.obs_at is not None \
and tracker.obs_at <= self.last_done:
cur = self.last_a # the reading predates our own write
self._clock = self._clock or (lambda: now)
await self._zero_once(drv, cur, now, tracker)
self.inflight = None
# One try: the add-on is stopping, bounded by `ctl_car.RELEASE_WAIT_S`.
if not self.hold and not (cur is not None and cur <= TOL_A):
self.zero_at, self.sent_a = now, 0.0
await self._write(drv, 0.0, cur, now, tracker)
self.out = True