EV-52: log one plain line for a new car current and for a car mode change

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VSY9UpTaUYnjJF5oVtbM1w
This commit is contained in:
ems-agent
2026-11-29 06:48:52 +01:00
parent 02166e28eb
commit c1536b0e93
3 changed files with 899 additions and 6 deletions
+93 -6
View File
@@ -142,11 +142,18 @@ class ManualWriter:
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
self.nonzero_at = None # EV-20 C6: the latest non-zero write sent
# EV-52: what the one info line per taken write needs.
self.boot = True # no user change since the process started
self._seen = False # the charger was available once
self._returned = False # ... and came back after being gone
self._r = None # the latest reading, for the phase count
self._late = None # (a, was, why, n) of a write not taken
# ------------------------------------------------------------------ user
def user_changed(self) -> None:
"""The user changed the mode or a value: one more write is allowed,
and another writer's hold is lifted."""
self.boot = False
self.rev_applied = None
self.hold = self.other = False
self.mismatch_since = None
@@ -164,6 +171,7 @@ class ManualWriter:
if r is None or not r.available:
self.was_available = False
return
self._r = r
# A reading taken before our latest write was confirmed says nothing
# about it (the writer runs beside the cycle and can finish between a
# read and the step that follows): wait for one taken after.
@@ -172,6 +180,8 @@ class ManualWriter:
and tracker.obs_at <= self.last_done:
return
if not self.was_available:
self._returned = self._returned or self._seen
self._seen = True
self.was_available = True
self.reapply = True # on return: only if it differs
cur = r.allowed_a if r.allowed_a is not None else r.setpoint_a
@@ -264,7 +274,47 @@ class ManualWriter:
"the add-on set %.0f A), so the add-on stops writing "
"to it until you change it.", cur, known[0])
# ----------------------------------------------------- EV-52: the info line
def _why_default(self) -> str:
"""The reason of a write nobody named: Manual's own, or `start-up`
while nothing was changed since the process started."""
return "start-up" if self.boot and not self._returned else "Manual"
def _close_why(self, q):
return None
def _phases_of(self, n=None):
"""The phase count a current is written for: the one the caller
names, else what the charger reports. None when unknown."""
if n in (1, 3):
return n
if getattr(self, "single", False):
return 1
p = getattr(self._r, "phases_allowed", None)
return p if p in (1, 3) else None
def _log_set(self, a, was, why, n) -> None:
"""One plain INFO line for a current the charger took. Never raises:
a line is not worth a failed write."""
try:
new, old, ph = _num(a), _num(was), self._phases_of(n)
if new is None:
return
head = f"Car: set to {new:.0f} A"
if old is not None:
if abs(old - new) <= TOL_A:
return # it stood there: not a change
head = f"Car: set from {old:.0f} A to {new:.0f} A"
tail = "" if ph is None else (
" on 1 phase" if ph == 1 else " on 3 phases")
_LOG.info("%s%s (%s).", head, tail, why)
except Exception: # noqa: BLE001
pass
async def _write(self, drv, a, was, now, tracker, **kw) -> bool:
why = kw.pop("why", None) or self._why_default()
n = kw.pop("n", None)
self._late = None
tracker.begin_write() # EV-12: the read-back is ours
if float(a) > TOL_A:
# EV-20 C6, THE ONE WRITE CLOCK: every non-zero current either
@@ -283,8 +333,10 @@ class ManualWriter:
if ok:
self.last_a = a
self.attempted_a = self.prior_a = None
self._log_set(a, was, why, n)
else:
self.attempted_a, self.prior_a = a, was
self._late = (a, was, why, n) # logged if it is adopted later
return ok
def _stamp(self, now) -> None:
@@ -310,7 +362,7 @@ class ManualWriter:
self.rev_applied, self.reapply = None, True
a = self._restore_a(q.get("prev"), clamp)
if a is not None:
await self._write(drv, a, 0.0, now, tracker)
await self._write(drv, a, 0.0, now, tracker, why="Manual")
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
@@ -337,7 +389,7 @@ class ManualWriter:
a = None if q is None else self._restore_a(q.get("prev"), clamp)
if a is not None:
self._clock = self._clock or (lambda: now)
await self._write(drv, a, 0.0, now, tracker)
await self._write(drv, a, 0.0, now, tracker, why="shutdown")
async def _advance(self, drv, r, cur, now, tracker, want_a, clamp, rev):
q = self.seq
@@ -379,6 +431,7 @@ class ManualWriter:
q["stage"] = "select"
if q["stage"] == "select":
ok = await drv.write_phases(q["n"])
q["sel_ok"] = bool(ok) # EV-52: the close's phase count
self._stamp(now)
self.last_switch = now
self.log.append(("phases", q["n"], now, ok))
@@ -403,7 +456,9 @@ class ManualWriter:
# on whether the phases changed); Manual a number, as before.
want = want_a(q) if callable(want_a) else want_a
a = min(max(float(want), CH.MIN_A), hi)
if await self._write(drv, a, 0.0, now, tracker):
if await self._write(drv, a, 0.0, now, tracker,
why=self._close_why(q),
n=q["n"] if q.get("sel_ok") else None):
# Judged afresh next cycle: a value the user changed meanwhile is
# still wanted, and one that is in place is adopted.
self.seq, self.rev_applied, self.reapply = None, None, False
@@ -782,12 +837,15 @@ class SolarWriter(ManualWriter):
self.held_step = False # C18: a held read: only a NEW 0 A goes out
self.cut_a = None # EV-21: the peak cut's cap, set per cycle
self.sent_at = None # EV-21: when `sent_a` went out
self.startup_zero = False # EV-52: the 0 A the start itself caused
self.sent_why = None # EV-52: the reason of `sent_a`'s write
def owe_start(self) -> None:
"""C1: the add-on starts in Off while an exit 0 A of its own was still
pending at the stop: the current the charger shows is the add-on's,
so the 0 A is owed (once it may write), retried by the exit rule."""
self.start_owed = self.engaged = True
self.startup_zero = True
def _stuck_end(self) -> None:
"""SPIKE-06: a stuck episode ends (the reading followed a start, a
@@ -843,6 +901,13 @@ class SolarWriter(ManualWriter):
the write goes out, so a write cancelled while it waits for its
read-back is never trusted (`live`)."""
prev = self.sent_a
why = kw.get("why")
if why is None:
# EV-52: the 0 A the start caused is `start-up`, else Solar's own.
why = "start-up" if self.startup_zero and float(a) <= TOL_A \
and self.boot else "Solar"
kw["why"] = why
self.sent_why = why
self.sent_a, self.taken = float(a), False
if prev is None or abs(prev - float(a)) > TOL_A:
# EV-21 (PM Q1, formal C15): the 2 s grace of a lowering on its
@@ -864,6 +929,7 @@ class SolarWriter(ManualWriter):
self.zero_sent_at = None
if ok:
self.taken = True
self.startup_zero = False
self.fails, self.fail_at, self.gave_up = 0, None, False
if zero:
self._owed(False)
@@ -871,6 +937,11 @@ class SolarWriter(ManualWriter):
self._failed(now)
return ok
def _close_why(self, q):
"""EV-52: a phase change's closing current: capped by the peak cut,
or Solar's."""
return "peak cut" if q.get("cut") else "Solar"
def _failed(self, now) -> None:
self.fails += 1
self.fail_at = now
@@ -885,9 +956,15 @@ class SolarWriter(ManualWriter):
if self.sent_a is not None and self.sent_a <= TOL_A:
self._owed(False)
self.taken = True
self.startup_zero = False
self.last_a = cur
self.attempted_a = self.prior_a = None
self.fails, self.fail_at, self.gave_up = 0, None, False
# EV-52 (PM Q2): a write the charger took late is logged once, here.
late, self._late = self._late, None
if late is not None and cur is not None \
and abs(float(cur) - float(late[0])) <= TOL_A:
self._log_set(*late)
async def _put(self, drv, want, cur, now, tracker, stuck=False) -> None:
"""Make the charger match `want`: write a new decision, try one the
@@ -904,7 +981,9 @@ class SolarWriter(ManualWriter):
self.fail_at is not None
and now - self.fail_at < RETRY_S):
return
await self._write(drv, want, cur, now, tracker)
# EV-52: a retry keeps the reason of the first try.
await self._write(drv, want, cur, now, tracker,
why=self.sent_why)
return
if not stuck and cur is not None and abs(cur - want) <= TOL_A:
self.sent_a = want # it already stands
@@ -918,7 +997,8 @@ class SolarWriter(ManualWriter):
elif self.gave_up or (self.fail_at is not None
and now - self.fail_at < RETRY_S):
return
await self._write(drv, want, cur, now, tracker)
await self._write(drv, want, cur, now, tracker,
why="peak cut" if cut else None)
def _own_readback(self, cur) -> bool:
"""C9: does the read-back show a current of Solar's own (sent,
@@ -1023,6 +1103,7 @@ class SolarWriter(ManualWriter):
cap = self.cut_a # EV-21: never above a peak cut's cap
if cap is not None and cap >= CH.MIN_A and a > cap:
q["close_a"] = a = float(cap)
q["cut"] = True # EV-52: the close is a peak cut
return a
return want
@@ -1102,6 +1183,7 @@ class SolarWriter(ManualWriter):
if r is None or not r.available:
self.was_available = False # nothing is tried while it is down
return
self._r = r
if 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:
@@ -1119,6 +1201,10 @@ class SolarWriter(ManualWriter):
# the charger's current is its own, not another writer's, and the
# first write is 0 A whatever it shows (D2, round 9).
self.was_available = True
self._returned = self._returned or self._seen
self._seen = True
# EV-52: only the process's own start owes a `start-up` 0 A.
self.startup_zero = self.boot and not self._returned
self.fresh_start = self.restart = True
self.mismatch_since = None
self.last_a = self.attempted_a = self.prior_a = None
@@ -1324,6 +1410,7 @@ class SolarWriter(ManualWriter):
if not self.engaged or not self.live or self.hold:
return None
if r is not None and r.available:
self._r = r
cur = r.allowed_a if r.allowed_a is not None else r.setpoint_a
else:
cur = None
@@ -1336,7 +1423,7 @@ class SolarWriter(ManualWriter):
if wait > 0:
await sleep(wait)
if await self._write(drv, 0.0, cur, clock(), tracker,
took_s=RELEASE_TOOK_S):
took_s=RELEASE_TOOK_S, why="shutdown"):
self.released = True
return True
self.released = False
+5
View File
@@ -88,6 +88,9 @@ class CarMixin:
ok = carmode.allowed_phases(opts)
self.car_n = n if n in ok else ok[0]
self.car_rev = 0
if self.charger.configured(): # EV-52: the mode at the start
_LOG.info("Car mode is now %s.",
carmode.MODE_LABELS[self.car_mode])
self.car_writer = carmode.ManualWriter()
self.car_solar = carmode.SolarWriter() # EV-20
self.car_solar.on_owed = self._car_owed
@@ -476,6 +479,8 @@ class CarMixin:
# A Manual phase change in the middle is not restored: Solar
# starts from the 0 A it stands at.
self.car_writer.seq = None
if value != self.car_mode: # EV-52: one plain line
_LOG.info("Car mode is now %s.", carmode.MODE_LABELS[value])
self.car_mode = value
self.store.set(carmode.KEY_MODE, value)
elif what == "car_sun_first":