diff --git a/goodwe_controller/DECISIONS/EV-04-manual-mode.md b/goodwe_controller/DECISIONS/EV-04-manual-mode.md index bc27ac7..f6183ca 100644 --- a/goodwe_controller/DECISIONS/EV-04-manual-mode.md +++ b/goodwe_controller/DECISIONS/EV-04-manual-mode.md @@ -86,18 +86,24 @@ up to 1.8 kW; the 40 Wh floor fixes that.) ## EV-10, EV-11, EV-12: the rig's three findings (EV-04 test F-1 to F-3) -- **The car is the cause when the period without it would not need the - batteries** (EV-10). `car_cycle` tests `projected - car_avg_w <= aim` (the car's - average over the same window, fresh and corroborated only) instead of - `projected > aim`, so it also holds when the car alone has taken the batteries - to the reserve and the projection is still under the aim (rig: 5888 W against - 6000 W). The car must draw at least `CAR_PEAK_MIN_W`. A stale, stuck or +- **The car is the cause when the alarm's own test says so** (EV-10). + `car_cycle` (`_car_explains_alarm`) grades the alarm's test on the period as it + is and on the period without the car's fresh, corroborated average + (`car_exclude`): the car explains the alarm only if the test is true with it + and false without it. The test is the alarm's own landing (`_car_grade`: the + batteries give only what they still can, `load_too_high` for a saturated guard + and for a floor with other batteries, `misses_aim` for a floor with none), not + the plain projection, which still counts the batteries' help. So the car alone + taking the batteries to the reserve is silent (rig: 5888 W against 6000 W), + while a house-caused drain with a small car still alarms. No minimum car power + is needed: a car that changes nothing cannot flip the test. A stale, stuck or uncorroborated figure is `None` and the normal alarms stand. - **Steady row, no hold on the alarm** (EV-11). Cause: after the car's own quarter became the month peak, the aim sat on the grid it made (14.04 kW +-40 W), so the test crossed the aim every few seconds. `car_over` (row, card, notice) now - stays on until the test has been false for `OVER_OFF_HOLD_S` = 180 s (the rig's - longest gap; shorter than a quarter) and a gap does not restart the 10 s + stays on until the test has been false for `OVER_OFF_HOLD_S` = 180 s (longer than + the gaps seen on the rig, 150 to 250 s at 50 s sampling, and shorter than a + quarter) and a gap does not restart the 10 s on-count. `car_explains`, which the alarms read, has no hold. - **Our own write is ours until its mute ends** (EV-12). The mute of the tracker's own detector opens before the write and runs 15 s from when it returns, because diff --git a/goodwe_controller/app/ctl_car.py b/goodwe_controller/app/ctl_car.py index 72504b5..e269068 100644 --- a/goodwe_controller/app/ctl_car.py +++ b/goodwe_controller/app/ctl_car.py @@ -21,7 +21,8 @@ CAR_PEAK_MIN_W = 50.0 # a quarter the car hardly touched is not kept CAR_PEAK_KEEP = 3200 # about a month of quarters OVER_HOLD_S = peakguard.ALARM_HOLD_S # EV-11: the card, the row and Why stay on this long after the car stops being -# the cause (the rig's off gaps were 20 s to 3 min), or until it really ends. +# the cause: longer than the gaps seen on the rig (150 to 250 s at 50 s +# sampling) and shorter than a quarter, or until the session really ends. OVER_OFF_HOLD_S = 180.0 @@ -50,6 +51,8 @@ class CarMixin: self.car_over = False # ... held: the card and row say so self._car_over_since = None self._car_over_gone = None + self._car_batt_prev = None + self._car_needed = False # EV-10: SAFETY-126's latch without the car self._car_over_notified = None # the session a notice was raised for self._car_session = 0 self._car_prev_state = None @@ -257,24 +260,16 @@ class CarMixin: self._car_quarter(real_proj, w, mono) self._car_session_tick(mono) explains = over = False - if w is not None and real_proj is not None and target_w is not None \ + if real_proj is not None and target_w is not None \ and real_proj.block_start is not None: - # The car explains the excess when the period WITHOUT the car - # (`projected - car_avg_w`, over the same window) would be at or - # under the aim: the car, not the house, needs the batteries. EV-10: - # this holds with the projection still under the aim, when the - # car alone took the batteries to the reserve. - car_avg = self.car_avg_w(real_proj, w) - without = float(real_proj.projected_w) - car_avg - ex = float(real_proj.projected_w) - float(target_w) if self.car_cover: - explains = bool( - car_avg >= CAR_PEAK_MIN_W and without <= float(target_w) - and guard is not None - and (guard.saturated or guard.floor_limited)) + # EV-10: the car explains the ALARM (`_car_explains_alarm`). + explains = self._car_explains_alarm(real_proj, guard, + target_w, w) over = explains - else: - over = ex > 0.0 and car_avg >= ex + elif w is not None: + ex = float(real_proj.projected_w) - float(target_w) + over = ex > 0.0 and self.car_avg_w(real_proj, w) >= ex self.car_explains = explains # EV-11: on once the condition has stood for OVER_HOLD_S, a short gap # in it not starting that count again; off only after it has been gone @@ -292,6 +287,44 @@ class CarMixin: self.car_over, self._car_over_since = False, None self._car_over_gone = None + def _car_explains_alarm(self, real_proj, guard, target_w, w) -> bool: + """EV-10: is the alarm's own test true with the car and false without + its fresh, corroborated share (`car_exclude`)? The tests are the + alarms': a saturated guard and a floor with other batteries left are + graded by `load_too_high` on where the period lands (SAFETY-81 / 87); a + floor that leaves nothing is graded by `within_aim`, which SAFETY-126 + latches for the quarter once the batteries at 0 W would miss the aim. + `_car_needed` is that latch on the period without the car; an + untrusted car figure (`w` None) sets it, so nothing is explained.""" + batt, prev = self.site_batt_w, self._car_batt_prev + self._car_batt_prev = batt + wants = bool(guard is not None and guard.engaged + and guard.blind is None + and (guard.wanted_w or 0.0) > 0.0) + if not wants: + self._car_needed = False + if batt is not None and prev is not None: + batt = min(float(batt), float(prev)) + excl = self.car_exclude(real_proj, w) + if wants: + self._car_needed = self._car_needed or w is None or batt is None \ + or peakguard.misses_aim(excl, batt_w=batt, target_w=target_w) + if w is None or batt is None or not wants \ + or not (guard.saturated or guard.floor_limited): + return False + if guard.saturated: + rating = self.site_rating_w + elif (guard.floor_w or 0.0) > 0.0 and guard.able_w is not None: + rating = guard.able_w + else: + return bool(not guard.within_aim and not self._car_needed) + + def high(p): + return peakguard.load_too_high( + p, batt_w=batt, ceiling_w=guard.ceiling_w, + achievable_w=rating) + return bool(high(real_proj) and not high(excl)) + def _car_session_tick(self, mono) -> None: """A charging session is a plug-in: the state goes from no car to a car. The notice is raised once per session.""" diff --git a/goodwe_controller/test_ev10.py b/goodwe_controller/test_ev10.py index 55af1cb..fbb0288 100644 --- a/goodwe_controller/test_ev10.py +++ b/goodwe_controller/test_ev10.py @@ -14,6 +14,7 @@ Mutating a module this file grades? Read MUTATION.md first (purge __pycache__). """ import asyncio +import dataclasses import logging import os import sys @@ -135,6 +136,38 @@ check("a house-only drain to the reserve with the car charging little: the " "normal alarm still fires", alarms(r10h) != [] and not r10h["c"].car_explains) + + +def plug_late(plant, c): + plant.ch.plug_in() + + +def house_reserve(plant, c): + plant.soc = 19.0 + plant.load = lambda at: 3075.0 + plant.ch.power_w + + +def swapped_events(): + ev = watch(850, house_reserve) + ev[800] = plug_late + return ev + + +def swapped(mode): + return P.drive(s81.constant(6000.0), dur=895, rating=12000.0, soc=60.0, + cap=CAP, phases=1, user_a=6, setpoint=6, mode=mode, + plug=False, events=swapped_events()) + + +r10x = swapped("manual") +r10xo = swapped("off") +check("PRECONDITION (review): a 6 kW HOUSE drains the batteries, the car " + "plugs in late (a small share); the projection is under the aim at the " + "reserve, and Off raises the alarm", alarms(r10xo) != [] + and any(p < a for _t, p, a in UNDER)) +check("the house drains the batteries and a small car is plugged in: the " + "normal alarm still fires in Manual", alarms(r10x) != []) + r10off = P.drive(s81.constant(HOUSE), dur=895, rating=12000.0, soc=60.0, cap=CAP, phases=1, mode="off", events=watch(850, at_reserve)) check("PRECONDITION: Off, the same case raises the normal alarm (the false " @@ -147,6 +180,7 @@ class _Car(ctl_car.CarMixin): self.o = {"ev_batt_cover": cover} self.w = w self._now = 0.0 + self.site_batt_w, self.site_rating_w = 300.0, 5000.0 self.car_init({}, type("S", (), {"data": {}, "set": lambda s, k, v: 0})()) def car_now(self): @@ -161,33 +195,44 @@ class _Car(ctl_car.CarMixin): return "charging" +@dataclasses.dataclass class _Proj: - def __init__(self, proj_w, elapsed=850.0): - self.projected_w, self.elapsed_s, self.held_w = proj_w, elapsed, proj_w - self.block_start = datetime(2026, 11, 16, 20, 0, tzinfo=timezone.utc) + projected_w: float + elapsed_s: float = 850.0 + held_w: float = 0.0 + block_start: object = datetime(2026, 11, 16, 20, 0, tzinfo=timezone.utc) + + def __post_init__(self): + self.held_w = self.projected_w class _G: - def __init__(self, floor=True, sat=False): + def __init__(self, floor=True, sat=False, within_aim=False): self.floor_limited, self.saturated = floor, sat + self.floor_w, self.able_w, self.ceiling_w = 0.0, None, 4500.0 + self.engaged, self.blind, self.wanted_w = True, None, 300.0 + self.within_aim = within_aim -def explains(proj_w, car_w, *, floor=True, cover=True, car_so_far_w=None): +def explains(proj_w, car_w, *, floor=True, cover=True, car_so_far_w=None, + within_aim=False): c = _Car(cover, car_w) p = _Proj(proj_w) c._carq = {"key": p.block_start, "ws": (car_w if car_so_far_w is None else car_so_far_w) * p.elapsed_s, "mono": 0.0} - c.car_cycle(p, _G(floor), 4450.0, None) + c.car_cycle(p, _G(floor, within_aim=within_aim), 4450.0, None) return c.car_explains -check("rule: the period without the car (4468 - 3700 = 768 W) is under the " - "aim: the car explains it, even with the projection under the aim", - explains(4300.0, 3700.0) and explains(4468.0, 3700.0)) +check("rule: the alarm's test is true with the car (4468 W, the batteries " + "at 0 W land 4485 W) and false without it (768 W): the car explains it, " + "with the projection under the aim", explains(4468.0, 3700.0)) +check("rule: where the alarm would not fire anyway nothing is explained", + not explains(4300.0, 3700.0, within_aim=True)) check("rule: without the car it is still over the aim: the car does not " "explain it", not explains(9000.0, 3700.0)) -check("rule: no car power, no explanation", not explains(4300.0, 0.0)) +check("rule: no car power, no explanation", not explains(9000.0, 0.0)) check("rule: the guard is not floor-limited or saturated: nothing to explain", not explains(4300.0, 3700.0, floor=False)) @@ -209,7 +254,7 @@ def over_run(c, secs, proj_of, w_of=lambda t: 11000.0, t0=0.0, target=14000.0, t = t0 + k c._now = t c.w = w_of(t) - p = _Proj(proj_of(t), elapsed=300.0 + (t % 400)) + p = _Proj(proj_of(t), elapsed_s=300.0 + (t % 400)) c._carq = {"key": p.block_start, "ws": 11000.0 * p.elapsed_s, "mono": t} c.car_cycle(p, _G(True), target, None) @@ -236,7 +281,7 @@ c11o = _Car(cover=False) raw = [] for k in range(1200): c11o._now = float(k) - p = _Proj(noisy(k), elapsed=300.0 + (k % 400)) + p = _Proj(noisy(k), elapsed_s=300.0 + (k % 400)) c11o._carq = {"key": p.block_start, "ws": 11000.0 * p.elapsed_s, "mono": k} ex = p.projected_w - 14000.0 raw.append(ex > 0.0 and c11o.car_avg_w(p, 11000.0) >= ex) @@ -442,6 +487,36 @@ wm = asyncio.run(manual_own()) check("Manual: two writes of ours, never 'another writer' in the tracker", wm.c.car.writer_at_utc is None and "charger_other" not in wm.rows()) + +async def failed_write_then_outside(how): + """A write that RAISES or returns FAILURE must still close the self-mute: + a later outside change shows the row.""" + w = World() + await w.step(3) + await w.c.handle_action("car_mode", value="manual") + + async def bad(a): + await w.sleep(1.0) + if how == "raise": + raise RuntimeError("the charger link broke") + return False + w.drv.write_current = bad + await w.c.handle_action("car_current", value=12) + await w.step(3) # the write is tried + await w.c.handle_action("car_mode", value="off") + await w.step(20) # past the 15 s mute + w.sim.set_current(7) # somebody else + await w.step(3) + return w + + +for how in ("raise", "fail"): + wf = asyncio.run(failed_write_then_outside(how)) + check(f"a write that {'raises' if how == 'raise' else 'returns failure'}" + ": the mute closes within 15 s, and a later outside change shows " + "the row", wf.c.car.mute_until < wf.t + and "charger_writer" in wf.rows()) + print() print("FAIL: " + ", ".join(fails) if fails else "PASS: every check passed") sys.exit(1 if fails else 0)