Files
goodwe-addon/goodwe_controller/tools/ev20_day.py
T
Claude 226e5a1e8b
checks / unit (pull_request) Successful in 57m6s
infra-pin / fresh (pull_request) Successful in 3s
EV-20: Car Solar mode, spare sun to the car (charging only)
Solar as a second writer next to Manual (carmode.SolarPlan, SolarWriter on
ManualWriter's one write site, safe phase sequence and other-writer watch):
signed spare, Batteries first on the batteries' ratings, the margin, whole
amps rounded down, the 30 s / 1 A rule, start and pause dwells, auto phases,
a one-phase car. Every way out ends at 0 A, retried by EV-04's rule; at
shutdown before the inverters, three tries inside 15 s (D3); start-up and a
returning charger or HA write 0 A first (D2); never over another writer's
hold. The peak guard treats a Solar car as a Manual car (D1 wording). Solar
kWh sensor and card line; select.goodwe_car_sun_first; ev_margin_w,
ev_start_min, ev_pause_min with config, validator, translations, DOCS,
DECISIONS and CHANGELOG.

Checks: test_ev20.py, test_ev20_exits.py, test_ev20_writers.py,
test_ev20_guard.py, test_ev20_long.py, test_ev20_long_car.py,
test_ev20_fuzz.py (harness tools/ev20_sun.py, ev20_kit.py, ev20_day.py);
test_ev03/test_ev04 no longer use Solar as the refused example.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VSY9UpTaUYnjJF5oVtbM1w
2026-11-22 20:31:26 +01:00

181 lines
7.3 KiB
Python

"""EV-20, T53: the acceptance sim day, shared by test_ev20_long.py (Batteries
first) and test_ev20_long_car.py (Car first).
The real Controller, N=2 (A 40 % and may charge, B full), 08:00-18:00 of sun
(9 kW at noon) with 12 clouds of 1 to 8 minutes, a house of 0.4 to 2.5 kW with
three +3 kW steps of a little under 5 minutes. The cycle order of `step` over
the whole day, of `step_race` over the cloudiest three hours, and a car-Off
twin over the quarters around the house steps for the alarms.
What is checked, per priority and cycle order:
- starts: at most one per stretch in which the car's target stayed under
6 A worth longer than `ev_pause_min` (the morning counts as one), so no cloud
or house step shorter than that starts or pauses the car;
- no start and no pause inside a cloud of 300 s or less;
- selects at least 600 s apart;
- the car's grid energy outside its holds: SETTLED (not within 20 s of a step
of the sun, the house or the car's own current) at most 10 Wh; the zero-grid
loop's slew after such a step (500 W per cycle) is reported apart;
- house-caused alarms at the same seconds as the car-Off twin, +2 s.
"""
import math
import random
import ev20_sun as S
DAY = 36000 # 08:00 to 18:00, 1 s cycles
PAUSE_S = 300.0
MARGIN = 100.0
_rnd = random.Random(53)
CLOUDS = []
_t = 1800
for _k in range(12):
_t += _rnd.randint(1200, 2400)
CLOUDS.append((_t, _t + _rnd.randint(60, 480)))
STEPS = [(9000, 9290), (19000, 19290), (27000, 27290)]
SOCS = {"a": 40.0, "b": 100.0}
RACE = (7200, 7200 + 10800) # the cloudiest three hours
def sun(t):
base = 9000.0 * max(0.0, math.sin(math.pi * t / DAY))
for a, b in CLOUDS:
if a <= t < b:
return 0.15 * base
return base
def house(t):
w = 400.0 + 2100.0 * (0.5 + 0.5 * math.sin(t / 1700.0))
for a, b in STEPS:
if a <= t < b:
w += 3000.0
return w
def cur_writes(r):
return [(x[2], x[1], x[3]) for x in r["c"].car_solar.log
if x[0] == "current"]
def low_stretches(r, first, longer=PAUSE_S):
"""Stretches in which the car's target was under 6 A worth on 1 phase for
longer than `longer`: (start, end); the last may run to the day's end."""
out, since = [], None
for x in r["trace"]:
sp = x["spare"]
room = x.get("room") or 0.0
tgt = None if sp is None else sp - (room if first == "batteries"
else 0.0) - MARGIN
low = tgt is None or tgt < 6.0 * 230.0
if low and since is None:
since = x["t"]
if not low and since is not None:
if x["t"] - since > longer:
out.append((since, x["t"]))
since = None
if since is not None and r["trace"] \
and r["trace"][-1]["t"] - since > longer:
out.append((since, r["trace"][-1]["t"]))
return out
def grid_split(r):
"""The car's grid energy outside its holds (W s): settled, and within 20 s
of a step of the sun, the house or the car's own current."""
tr = r["trace"]
marks = []
for i in range(1, len(tr)):
a, b = tr[i - 1], tr[i]
if abs(b["sun"] - a["sun"]) > 200.0 or abs(b["house"] - a["house"]) \
> 200.0 or b["sp"] != a["sp"] or b["ph"] != a["ph"]:
marks.append(b["t"])
settled = slew = 0.0
j = 0
for x in tr:
imp = max(0.0, min(x["car"], x["grid"])) \
if x["why"] != "hold" and x["sp"] > 0 else 0.0
if imp <= 0.0:
continue
while j < len(marks) and marks[j] < x["t"] - 20.0:
j += 1
near = j < len(marks) and marks[j] <= x["t"]
if near:
slew += imp
else:
settled += imp
return settled, slew
def run_day(first, check):
"""Run the day for one priority and grade it through `check`."""
long_c = [(a, b) for a, b in CLOUDS if b - a > PAUSE_S]
short_c = [(a, b) for a, b in CLOUDS if b - a <= PAUSE_S]
print(f" clouds (start, length): {[(a, b - a) for a, b in CLOUDS]}")
runs = {"step": S.drive(house, sun, dur=DAY, first=first, socs=dict(SOCS),
race=False),
"race": S.drive(house, sun, dur=RACE[1] - RACE[0], t0=RACE[0],
first=first, socs=dict(SOCS), race=True)}
for tag, r in runs.items():
w = cur_writes(r)
# A pause: a 0 A that is not a phase change's (no select within
# 30 s); a start: the first current after a pause or at the start.
log = r["c"].car_solar.log
sel_t = [t for k, a, t, ok in log if k == "phases"]
starts, pauses, paused = [], [], True
for kind, a, t, ok in log:
if kind != "current":
continue
if a == 0.0:
if not [1 for t2 in sel_t if t <= t2 <= t + 30.0]:
pauses.append(t)
paused = True
elif paused:
starts.append(t)
paused = False
lows = low_stretches(r, first)
check(f"T53 [{first} first, {tag}]: {len(starts)} starts, at most one "
f"per stretch under 6 A worth longer than the pause "
f"({len(lows)}: {lows[:6]})", len(starts) <= len(lows) + 1)
# A pause or start inside a short cloud is the cloud's only when the
# low stretch it ends began with that cloud.
every = low_stretches(r, first, longer=-1.0)
bad = []
for t in pauses + starts:
for a, b in short_c:
if a <= t <= b + 1:
began = [s0 for s0, e0 in every if s0 <= t <= e0 + 1]
if not began or began[-1] >= a - 1:
bad.append((t, a, b))
check(f"T53 [{first} first, {tag}]: no start and no pause from a "
f"cloud of 300 s or less ({bad[:2]}; {len(long_c)} clouds are "
f"longer)", not bad)
sel = [x[0] for x in r["sim"].log if x[1] == "phases"]
check(f"T53 [{first} first, {tag}]: selects at least 600 s apart "
f"({sel})", all(b - a >= 600.0 for a, b in zip(sel, sel[1:])))
settled, slew = grid_split(r)
print(f" [{first} first, {tag}] writes {len(w)}; car grid energy "
f"outside holds: settled {settled / 3600.0:.1f} Wh, loop slew "
f"after a step {slew / 3600.0:.1f} Wh; sun "
f"{r['c'].car_sun_kwh()} of {r['c'].car.today_kwh} kWh")
check(f"T53 [{first} first, {tag}]: the car's settled grid energy "
f"outside its holds is at most 10 Wh ({settled / 3600.0:.1f} Wh)",
settled <= 10.0 * 3600.0)
r = runs["step"]
for a, _b in STEPS:
t0 = float(a - a % 900 - 900)
off = S.drive(house, sun, dur=2700, t0=t0, first=first, mode="off",
plug=False, socs=dict(SOCS))
win = (t0 + 900.0, t0 + 2700.0) # the first quarter warms up
al = [t for t, n in r["raised"] if n in S.ALARMS
and win[0] <= t < win[1]]
ao = [t for t, n in off["raised"] if n in S.ALARMS
and win[0] <= t < win[1]]
check(f"T53 [{first} first]: house-caused alarms around the +3 kW "
f"step at {a} s at the same seconds as the car-Off twin, +2 s "
f"({al[:3]} vs {ao[:3]})",
len(al) == len(ao) and all(abs(x - y) <= 2
for x, y in zip(al, ao)))
return runs