EV-05 round 9: one test of control; another writer's phases are a hold
checks / unit (pull_request) Successful in 31m31s
infra-pin / fresh (pull_request) Successful in 2s

SolarWriter.in_control is the single test of "Solar has the charger":
writing, read-back current and phases are Solar's own, no hold, no write
refused, given up or owed. Alarm grading, banking, the own-pause grace and
the cut use it. A phase change Solar did not make is another writer:
nothing written over it, hold after 30 s, no select back after 10 min.
While Solar's own switch waits, a lower current is written at once, and
the arithmetic uses the phases the car draws on. A car another writer
starts inside the own-pause grace is no longer graded out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VSY9UpTaUYnjJF5oVtbM1w
This commit is contained in:
ems-agent
2026-11-18 23:49:47 +01:00
co-authored by Claude Opus 5.5
parent e348a808ff
commit c9a95f3c10
5 changed files with 211 additions and 24 deletions
@@ -138,6 +138,23 @@ inputs (`car_sun_inputs`) and runs the writer in the same background task.
`OWN_PAUSE_MAX_S`. A car another writer starts while Solar waits at 0 A for
sun gets the alarms as in Off.
## Round 9 (review of e348a80)
- **One test of control**: `SolarWriter.in_control`. Solar writes, the
read-back current AND phases are what it set (`own_rb`), there is no hold,
and no write of its own is refused, given up or owed. The alarm grading,
the banking, EV-15's own-pause grace and the cut all use it.
- **A phase change Solar did not make is another writer**, like a current
change: nothing is written over it, and after `WRITER_S` the hold
(CHARGER_OTHER). Solar no longer selects its own phases back after the
10 minutes. A select the charger did not confirm counts both phases as
Solar's until the read-back shows one.
- **While Solar's own phase switch waits** (`PHASE_GAP_S`), a lower current
(the cut, the hold's 6 A) is written at once. The arithmetic uses the
phases the car draws on.
- **The own-pause grace ends when the read-back is not Solar's 0 A**: a car
another writer starts inside it gets the alarms as in Off.
## Solar kWh
Per cycle in Solar: `min(car power, max(0, spare - room))` Batteries first,
+2 -1
View File
@@ -3368,7 +3368,8 @@ checks within 10 seconds that the charger took it.
Choosing Manual leaves it to Manual.
- Another writer stops the add-on's writes after 30 seconds, with an amber row
and a warning, until you press the mode again or the charger shows the
add-on's value again.
add-on's value again. A change of the current or of the phases counts. In
the meantime nothing is written over it and the peak alarms stand.
- The card shows *{kWh} charged today, {kWh} from the sun*, also as
`sensor.goodwe_car_today_solar_kwh`. Only the sun the car could take counts,
not the battery or grid share of a hold, and not Manual.
+88 -15
View File
@@ -675,6 +675,13 @@ class SolarWriter(ManualWriter):
self._one_since = None
self.zero_at = None # EV-15: our own latest 0 A, sent then
self.sent_a = None # the current we last decided to write
# Round 9: the phases Solar knows as its own (None: not yet taken;
# both after a select the charger did not confirm), whether the
# latest read-back (current and phases) is Solar's own, and the
# phases the car draws on while they differ from the plan's.
self.ph_known = None
self.own_rb = True
self.n_draw = None
# ------------------------------------------------------------------ help
def _mark(self, r, now) -> None:
@@ -697,18 +704,58 @@ class SolarWriter(ManualWriter):
_LOG.info("The car draws one phase while three are allowed, so "
"the add-on counts one phase for this charge.")
def _watch(self, cur, now) -> None:
"""As Manual's, and in Solar the hold is a warning: the add-on stops
writing until you press the mode or the read-back matches again."""
was = self.hold
super()._watch(cur, now)
if self.hold and not was:
def _watch(self, cur, now, ph=None) -> None:
"""As Manual's, for the current AND (round 9) the phases: a read-back
that is not what Solar set (`own_rb` False) is another writer's, and
after `WRITER_S` the hold. In Solar the hold is a warning: the add-on
stops writing until you press the mode or the read-back matches."""
known = [x for x in (self.last_a, self.attempted_a, self.prior_a)
if x is not None]
a_known = bool(known) and cur is not None
a_bad = a_known and not any(abs(cur - k) <= TOL_A for k in known)
p_known = ph in (1, 3) and bool(self.ph_known)
p_bad = p_known and ph not in self.ph_known
if p_known and not p_bad:
self.ph_known = {ph} # a select confirmed late
self.own_rb = not (a_bad or p_bad)
if self.own_rb:
self.mismatch_since = None
if self.hold and (a_known or p_known):
self.hold = self.other = False
return
if self.mismatch_since is None:
self.mismatch_since = now
elif now - self.mismatch_since >= WRITER_S and not self.hold:
self.hold = self.other = True
if p_bad:
_LOG.info("Something else changed the charger's phases, so "
"the add-on stops writing to it until you change "
"it.")
else:
_LOG.info("Something else changes the charger (read-back "
"%.0f A, the add-on set %.0f A), so the add-on "
"stops writing to it until you change it.",
cur, known[0])
_LOG.warning("Something else changes the charger, so the add-on "
"stops writing to it: CHARGER_OTHER.")
@property
def in_control(self) -> bool:
"""Round 9, the one test of "Solar has the charger now": it writes,
the read-back current and phases are what it set, no other writer's
hold, and no write of its own refused, given up or owed. Every place
that acts as if Solar can cut the car asks this."""
return bool(self.had_control and not self.out and self.own_rb
and not self.hold and not self.gave_up
and self.fail_at is None and self.attempted_a is None
and self.fault is None and not self.zero_owed)
@property
def n_calc(self) -> int:
return 1 if self.single else self.plan.n
"""The phases the sun arithmetic uses: those the car draws on."""
if self.single:
return 1
return self.n_draw or self.plan.n
def amps_out(self, clamp) -> float:
"""The plan's current, never above the clamp; 0 when under 6 A."""
@@ -722,6 +769,10 @@ class SolarWriter(ManualWriter):
hold is lifted by pressing the mode)."""
super().user_changed()
self.sent_a = None
# Round 9: Solar takes the charger as it is (current and phases), so
# the other writer's value is no longer a difference.
self.last_a = None
self.ph_known, self.own_rb = None, True
async def _write(self, drv, a, was, now, tracker) -> bool:
self.inflight = a # cleared only when the write returns
@@ -774,6 +825,7 @@ class SolarWriter(ManualWriter):
self.fault = None
self.reinit = True
self.sent_a = None
self.ph_known, self.own_rb, self.n_draw = None, True, None
self.plan.reset(self.plan.n)
def _plan_step(self, sun, now, clamp) -> None:
@@ -803,6 +855,7 @@ class SolarWriter(ManualWriter):
if not self.was_available:
self.was_available = True
self.reinit = True # back: 0 A first, then the rules
self.ph_known = None # ... on the phases it came back on
cur = r.allowed_a if r.allowed_a is not None else r.setpoint_a
if mode != SOLAR:
self.would = None
@@ -831,9 +884,10 @@ class SolarWriter(ManualWriter):
# every reader (the exit, the shutdown, the peak alarms).
self.seq = None
self.out = True
self.n_draw = None
if self.had_control:
await self._exit_zero("out", drv, cur, now, tracker)
self._watch(cur, now)
self._watch(cur, now, r.phases_allowed)
self._plan_step(sun, now, clamp)
self.would = (self.amps_out(clamp), self.n_calc)
self.reinit = True
@@ -876,13 +930,24 @@ class SolarWriter(ManualWriter):
self.sent_a = None
n = r.phases_allowed if r.phases_allowed in (1, 3) else self.plan.n
self.plan.reset(n)
if self.ph_known is None and r.phases_allowed in (1, 3):
self.ph_known = {r.phases_allowed} # Solar takes the charger
if status == CH.FULL: # the car is full: nothing written
self.plan.why = "full"
return
self.n_draw = None
if self.seq is None:
self._watch(cur, now)
self._watch(cur, now, r.phases_allowed)
if self.hold:
return
if not self.own_rb:
# Round 9: another writer's current or phases, not yet held
# (`WRITER_S`): the plan goes on, nothing is written over it.
self._plan_step(sun, now, clamp)
return
if r.phases_allowed in (1, 3) and cur is not None \
and cur >= CH.MIN_A:
self.n_draw = r.phases_allowed # what the car draws on
self._plan_step(sun, now, clamp)
if self.seq is not None:
q = self.seq
@@ -892,6 +957,10 @@ class SolarWriter(ManualWriter):
pf = self.phase_fails
await self._advance(drv, r, cur, now, tracker, self.amps_out(clamp)
or CH.MIN_A, clamp, None)
if "t1" in q and not q.get("ph_set"):
q["ph_set"] = True # the select is out: whose phases
self.ph_known = {q["n"]} if self.phase_fails == pf \
else {x for x in (q.get("from"), q["n"]) if x in (1, 3)}
if self.phase_fails > pf and q.get("from") in (1, 3):
self.plan.n = q["from"] # the select was not taken
if self.seq is None:
@@ -904,13 +973,17 @@ class SolarWriter(ManualWriter):
if self.last_switch is not None \
and now - self.last_switch < PHASE_GAP_S:
self.blocked = True
# Round 9: the switch waits, a lower current does not (the
# cut, the hold's 6 A; a pause is 0 A and never waits here).
if not (cur is not None and cur > want + TOL_A):
return
else:
self.blocked = False
self.zero_at, self.sent_a = now, 0.0
await self._begin(drv, r, cur, self.plan.n, now, tracker)
if self.seq is not None:
self.seq["from"] = r.phases_allowed
return
self.blocked = False
self.zero_at, self.sent_a = now, 0.0
await self._begin(drv, r, cur, self.plan.n, now, tracker)
if self.seq is not None:
self.seq["from"] = r.phases_allowed
return
if self.sent_a is not None and abs(self.sent_a - want) <= TOL_A \
and self.fail_at is None:
return # nothing new: a read-back that differs is `_watch`'s
+13 -8
View File
@@ -459,13 +459,14 @@ class CarMixin:
way-out 0 A is not taken, the charger shows a fault, or it cannot be
read, and not while Solar is not writing (Stop, shadow, a safety stop:
round 7), and not a car Solar is not running (round 8). Only then may
the car be graded out of the peak alarms."""
the car be graded out of the peak alarms. Round 9: all of that, and a
read-back current and phases that are Solar's own, is
`SolarWriter.in_control`, the one test."""
w = getattr(self, "car_solar", None)
r = self.car.reading
if not (w is not None and r is not None and r.available
and self.car_writing() # round 7: Stop, shadow, latch
and not w.hold and not w.gave_up and w.fail_at is None
and w.fault is None and not w.zero_owed):
and w.in_control):
return False
# Round 8: Solar cuts only a car it runs (`plan.a > 0`). While paused
# at 0 A a car that draws is someone else's, unless it is Solar's own
@@ -523,9 +524,9 @@ class CarMixin:
shadow, an unplugged car or another writer."""
wr = getattr(self, "car_solar", None)
r = self.car.reading
if wr is None or wr.hold or not self.car_writing() or r is None \
or not r.available or r.state == "A":
return False
if wr is None or not wr.in_control or not self.car_writing() \
or r is None or not r.available or r.state == "A":
return False # round 9: only a charger Solar has
q = wr.seq
if q is not None:
return not q.get("failed") and 0.0 <= mono - q["t0"] \
@@ -645,8 +646,12 @@ class CarMixin:
if self._car_want_since is None:
self._car_want_since = mono
left = QUARTER_S - float(real_proj.elapsed_s)
if not self._car_batt_headroom(guard) or left <= CUT_NEAR_END_S \
or mono - self._car_want_since >= CUT_WAIT_S:
w9 = self.car_solar
if (not self._car_batt_headroom(guard) or left <= CUT_NEAR_END_S
or mono - self._car_want_since >= CUT_WAIT_S) \
and w9.own_rb and not w9.hold:
# Round 9: never for a car another writer runs (its current
# or phases): the plan keeps no cut it cannot write.
self.car_cut_w = cut
else:
self._car_want_since = None
+91
View File
@@ -2174,6 +2174,97 @@ for H8 in (40000.0, 42000.0):
f"{None if cut8 is None else cut8['t']})",
want8 is not None and cut8 is not None and head8 is True
and cut8["t"] - want8 <= 1)
# =========================================================================== #
print("\nROUND 9. ONE TEST OF CONTROL: THE READ-BACK CURRENT AND PHASES ARE SOLAR'S")
# =========================================================================== #
# 1. The review's p_blocked: Solar starts in auto on a charger at 3 phases, so
# its own select to 1 phase comes at about 122 s. Another writer selects 3
# phases at T and leaves the current. N=2 at the cut-out, the grid takes it.
# The twin is Off with the car at Solar's 12 A on 3 phases from T.
def phases_vs_off(T, dur, label):
kw = dict(house=const(500.0), sun=const(3500.0), first="car", socs=CUT10,
dur=dur, opts={"slew_w": 0}, phases=3)
def oth(p, c):
p.ch.set_phases(3)
p.ch.set_current(12)
rs = S.drive(events={T: lambda p, c: p.ch.set_phases(3)},
sim=SimCharger(setpoint_a=0), **kw)
ro = S.drive(events={T: oth}, mode="off", sim=SimCharger(setpoint_a=0),
**kw)
own = [t for t, _v in S.writes(rs, "phases") if t < T]
late = after(rs, T) + [x for x in S.writes(rs, "phases") if x[0] >= T]
a_s, a_o = first_alarm(rs, T), first_alarm(ro, T)
ex = sum(1 for x in rs["trace"] if x["t"] > T and x["explains"])
w = rs["c"].car_solar
check(f"R9 another writer selects 3 phases {label}: Solar writes nothing "
f"over it ({late}), holds ({w.hold}), alarms as Off (+2 s) "
f"({a_s} vs {a_o}), the car never explained away ({ex} cycles)",
late == [] and w.hold and a_s is not None and a_o is not None
and abs(a_s - a_o) <= 2 and ex == 0)
return rs, ro, own
r9a, _r9ao, own9 = phases_vs_off(300, 1000, "inside Solar's 10 minutes")
check(f"R9 (precondition) Solar's own select came less than "
f"{carmode.PHASE_GAP_S:.0f} s before ({own9})",
own9 and 300 - own9[-1] < carmode.PHASE_GAP_S)
check(f"R9 the quarter the other writer's phases ran in is banked as the "
f"car's ({r9a['c'].car_peak_q})", len(r9a["c"].car_peak_q) == 1)
r9b, _r9bo, own9b = phases_vs_off(800, 1300, "after Solar's 10 minutes")
check(f"R9 (precondition) Solar's own select came more than "
f"{carmode.PHASE_GAP_S:.0f} s before ({own9b})",
own9b and 800 - own9b[-1] > carmode.PHASE_GAP_S)
# 2. The review's p_young: Solar's own 0 A pause at about 603 s, another
# writer starts the car at T inside OWN_PAUSE_MAX_S of it. The read-back is
# not Solar's 0 A, so the grace does not cover it.
for T9 in (615, 640):
r9y = paused_vs_off(T9, f"another writer starts the car at {T9} s, "
f"inside the grace of Solar's own 0 A",
sun=steps((0, 7000.0), (300, 0.0)))
_own9 = [t for t, v in cur(r9y) if v == 0.0 and t < T9]
check(f"R9 (precondition) Solar's own 0 A came less than "
f"{ctl_car.OWN_PAUSE_MAX_S:.0f} s before {T9} s ({_own9[-1:]})",
_own9 and T9 - _own9[-1] < ctl_car.OWN_PAUSE_MAX_S)
# 3. Solar holds control while its phases settle: its own select to 3 phases
# returns False although the charger took it (Home Assistant's answer was
# lost), so the plan stays on 1 phase and the next switch waits 10 minutes.
# A cut in that time is written at once, worked out on the 3 phases the car
# draws on.
def _lost_select(p, c):
orig = p.ch.request_phases
def rp(n):
orig(n)
return False
p.ch.request_phases = rp
r9c = S.drive(house=lambda t: 15000.0 if t >= 650 else 500.0,
sun=const(9000.0), first="car", socs=CUT10, dur=700,
opts={"slew_w": 0}, phases=1, events={0: _lost_select})
w9c = r9c["c"].car_solar
c9 = next((x for x in r9c["trace"] if x["t"] >= 650 and x["cut"] > 0), None)
pre9 = next((x for x in r9c["trace"] if x["t"] == 649), {})
check(f"R9 (precondition) the plan is on 1 phase, the charger on 3, the "
f"switch waits (n {pre9.get('n')}, phases {pre9.get('ph')}, current "
f"{pre9.get('sp')}, blocked {w9c.blocked})",
pre9.get("n") == 1 and pre9.get("ph") == 3 and w9c.blocked
and (pre9.get("sp") or 0) >= 6)
if c9 is not None:
w9 = math.floor((pre9["car"] - c9["cut"]) / (carmode.VOLTS * 3))
got9 = after(r9c, c9["t"], c9["t"] + 3)
else:
w9, got9 = None, None
check(f"R9 a cut while Solar's own phases settle is written at once, on the 3 "
f"phases the car draws on (cut at {None if c9 is None else c9['t']}, "
f"wanted {w9} A, written {got9})",
c9 is not None and w9 is not None and w9 >= 6
and got9 == [(got9[0][0], float(w9))] if got9 else False)
print("\n(T39, T44, T45, T50 and T51, the long runs, are in test_ev05_long.py)")
print()