- .gitea/ci/affected_tests.py: which test_*.py a diff reaches (static import graph plus names in strings, transitive), a fixed safety set, SELECT ALL for anything it cannot map. .gitea/ci/run-affected.sh runs the selection in the CI image. run-tests.sh takes CI_ONLY for that; checks.yml never sets it and test_affected_tests.py fails if it does. The merge gate still runs every file. - test_modes_p4 -> p4/p4b/p4c/p4d, test_safety126 -> +b, test_safety125 -> +b: same check lines, none dropped (146, 35, 45 before and after). - run-tests.sh: 10 workers instead of 6, new longest-first order. Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Bdqr7PiaHcBnzPituu2yTC
233 lines
9.5 KiB
Python
233 lines
9.5 KiB
Python
"""Runnable check for OPT-11 P4, part 6b: cheap hours on a partly sunny day, the forecast lost mid-run, a forecast far too high.
|
|
`python3 test_modes_p4c.py`
|
|
|
|
Split out of test_modes_p4.py by CI-18 so the slow part 6 runs in pieces (CI
|
|
runs files side by side; the one file took ~13 min). The docstring there describes
|
|
the whole. Same fake Home Assistant, same engine (tools/opt11p3_replay.py), same
|
|
checks, none dropped: the four files print exactly the check lines the one file did.
|
|
Read MUTATION.md before you report a survivor."""
|
|
|
|
import asyncio
|
|
import logging
|
|
import os
|
|
import random
|
|
import sys
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
sys.path.insert(0, HERE)
|
|
sys.path.insert(0, os.path.join(HERE, "tools"))
|
|
logging.disable(logging.WARNING)
|
|
|
|
from named_absence import nth, present # noqa: E402
|
|
import main_files # noqa: E402
|
|
import opt11p3_replay as rp # noqa: E402
|
|
from app import arbitrage, dayplan, homeview, modetrade, packmode # noqa
|
|
from app import priceruns, solarfc, validate # noqa: E402
|
|
from app.tariff import BILLING_TZ # noqa: E402
|
|
|
|
fails = []
|
|
|
|
|
|
def check(name, cond):
|
|
print(f" {'ok ' if cond else 'FAIL'} {name}")
|
|
if not cond:
|
|
fails.append(name)
|
|
|
|
|
|
L = timezone(timedelta(hours=2))
|
|
A, B = "inverter-a", "inverter-b"
|
|
D0 = datetime(2026, 9, 21, tzinfo=L)
|
|
UTC0 = D0.astimezone(timezone.utc)
|
|
|
|
# ============================================================================ #
|
|
def profile(day_w=None, days=1, evening=None, start=UTC0 - timedelta(days=1)):
|
|
hp = dayplan.HouseProfile()
|
|
t = start
|
|
end = start + timedelta(days=days)
|
|
while t < end:
|
|
h = t.astimezone(L).hour
|
|
w = (evening if evening is not None and 17 <= h < 22 else
|
|
day_w if day_w is not None else 600.0)
|
|
hp.add(t, w)
|
|
t += timedelta(seconds=5)
|
|
hp.add(t, None)
|
|
return hp
|
|
|
|
P = modetrade.Pack
|
|
|
|
def pk(name, mode, soc, cap=10.0, **kw):
|
|
return P(name, True, mode, False, 2500.0, soc, cap, **kw)
|
|
|
|
|
|
# Prices: cheap 01:00-03:00, dear 19:00-20:00 local (as in test_modes_p3).
|
|
DAYW = [0.20] * 96 * 2
|
|
for i in list(range(4, 12)) + list(range(100, 108)):
|
|
DAYW[i] = 0.02
|
|
for i in list(range(76, 80)) + list(range(172, 176)):
|
|
DAYW[i] = 0.40
|
|
WPTS = [(UTC0 + timedelta(minutes=15 * i), 900, p) for i, p in enumerate(DAYW)]
|
|
from app.price import PricePoint # noqa: E402
|
|
|
|
# ============================================================================ #
|
|
print("6. the real controller over a night and a day")
|
|
AIM = rp.PEAK_CAP - 50
|
|
FC_KWH = [None] # today's forecast in the fake sensor (None: down)
|
|
|
|
|
|
async def _sensor(self, e):
|
|
now = rp_start[0] + timedelta(seconds=self.t)
|
|
kwh = FC_KWH[0](now) if callable(FC_KWH[0]) else FC_KWH[0]
|
|
if kwh is None:
|
|
return {"state": "unavailable", "attributes": {},
|
|
"last_updated": now.isoformat()}
|
|
if e.endswith("tomorrow"):
|
|
return None
|
|
loc = now.astimezone(L)
|
|
d0 = loc.replace(hour=0, minute=0, second=0, microsecond=0)
|
|
wh = {(d0 + timedelta(hours=h)).astimezone(timezone.utc).isoformat():
|
|
(kwh * 100.0 if 8 <= h < 18 else 0.0) for h in range(24)}
|
|
left = sum(v for k, v in wh.items()
|
|
if datetime.fromisoformat(k) >= now) / 1000.0
|
|
return {"state": f"{left:.3f}",
|
|
"attributes": {"unit_of_measurement": "kWh", "wh_period": wh},
|
|
"last_updated": now.isoformat(), "last_reported": now.isoformat()}
|
|
|
|
|
|
rp.Hass.state = _sensor
|
|
rp_start = [UTC0]
|
|
FC_OPTS = {"pv_forecast_entity": "sensor.energy_production_today_remaining",
|
|
"cycle_cost_threshold_eur": 0.20}
|
|
# TARIFF-24 round 2: with a forecast read, a price mode needs the cycle cost
|
|
# itself (0.20 here), no longer 1.5 times it (0.30). These scenarios were
|
|
# written for a 0.30 gap, and a midday 0.21 gap now plans a grid top-up they
|
|
# are not about, so they ask for 0.30 with the forecast; the no-forecast leg
|
|
# (`rows_nf`) keeps 0.20, which is 0.30 widened: the same gate on both sides.
|
|
GATE_030 = {"cycle_cost_threshold_eur": 0.30}
|
|
|
|
|
|
def seed_profile(c):
|
|
if c.house_profile is not None:
|
|
c.house_profile = profile(day_w=600.0, evening=3000.0)
|
|
|
|
|
|
def house_flat(w):
|
|
return [(UTC0.timestamp() - 10, float(w))]
|
|
|
|
|
|
def run_day(choice, *, fc, soc0, hours=21.0, start_h=0.75, house_w=800.0,
|
|
refuse=(), extra=None, patch=seed_profile):
|
|
FC_KWH[0] = fc
|
|
start = UTC0 + timedelta(hours=start_h)
|
|
rp_start[0] = start
|
|
rows = []
|
|
|
|
def cb(c, h, now, pr):
|
|
sh = dict(c.allocation.shares) if c.allocation is not None and \
|
|
c.allocation.shares else {}
|
|
rows.append(dict(
|
|
now=now, grid=h.grid(), shares=sh,
|
|
action=getattr(c.arb, "action", None),
|
|
roles=dict(c.mode_tick.roles) if c.mode_tick else {},
|
|
soc={u: p.soc for u, p in h.plants.items()},
|
|
targets=c.plan_targets(), plan=c.day_plan, runs=c.mode_runs))
|
|
st_, c = rp.run("p3_ls", house_flat(house_w), WPTS, start, 3600 * hours,
|
|
check=cb, soc0=soc0, choice=choice, refuse=refuse,
|
|
extra=dict(FC_OPTS, **(extra or {})), patch=patch)
|
|
return rows, st_, c
|
|
|
|
|
|
SF_LS = {"a": (True, "solar_first", False), "b": (True, "leftover_solar",
|
|
False)}
|
|
SF_CH = {"a": (True, "solar_first", False), "b": (True, "cheap_hours",
|
|
False)}
|
|
|
|
|
|
def invariants(tag, rows, c):
|
|
charge = [r for r in rows if r["action"] == arbitrage.CHARGE]
|
|
mixed = [r for r in rows if any(v > 0 for v in r["shares"].values())
|
|
and any(v < 0 for v in r["shares"].values())]
|
|
check(f"{tag}: no opposite-direction split on any cycle", not mixed)
|
|
check(f"{tag}: Solar first never charges from the grid (A's share >= 0 "
|
|
"while the site charges)",
|
|
all(r["shares"].get(A, 0) >= 0 for r in charge))
|
|
q = {int(r["now"].timestamp() // 900) for r in charge}
|
|
banked = [b.offtake_avg_w for b in c.p1.blocks
|
|
if int(b.start.timestamp() // 900) in q]
|
|
check(f"{tag}: every banked quarter with a grid charge <= cap - margin "
|
|
f"(max {max(banked, default=0):.1f} W, aim {AIM})",
|
|
not banked or max(banked) <= AIM + 0.5)
|
|
return charge
|
|
|
|
|
|
# Cheap hours on a partly sunny day: ignores the plan, fills as in P3.
|
|
rows, st_, c = run_day(SF_CH, fc=14.0, soc0={"a": 60.0, "b": 30.0},
|
|
extra=GATE_030)
|
|
charge = invariants("cheap hours, partly", rows, c)
|
|
top = max(r["soc"]["b"] for r in rows)
|
|
print(f" cheap hours partly: B reached {top:.1f} %")
|
|
check("Cheap hours gets no planned level",
|
|
not any(r["targets"] and B in r["targets"] for r in rows))
|
|
rows_nf, _s, c_nf = run_day(SF_CH, fc=None, soc0={"a": 60.0, "b": 30.0})
|
|
check("Cheap hours with a sun forecast charges as with none (P3)",
|
|
charge and abs(max(r["soc"]["b"] for r in rows_nf) - top) < 1.0
|
|
and top > 80.0)
|
|
|
|
# The forecast goes away mid-run: a charge under way goes on to its level
|
|
# (OPT-34; it stopped at once, P3 option A, before). With no charge under way
|
|
# the levels go with the forecast (below, and test_opt27.py).
|
|
# OPT-27: a sensor that is unavailable is covered by its last fresh reading
|
|
# for up to 3 h (`solarfc.Hold`), so "lost" here is a hold that never covers;
|
|
# the next scenario is the same loss with the hold (test_opt27.py).
|
|
DROP = UTC0 + timedelta(hours=1, minutes=30)
|
|
|
|
|
|
class _NoHold(solarfc.Hold):
|
|
def stand_in(self, *a, **k):
|
|
return None
|
|
|
|
|
|
def seed_profile_no_hold(c):
|
|
seed_profile(c)
|
|
c.fc_hold = _NoHold()
|
|
|
|
|
|
rows, st_, c = run_day(SF_LS, fc=lambda now: 1.0 if now < DROP else None,
|
|
soc0={"a": 60.0, "b": 10.0}, patch=seed_profile_no_hold)
|
|
invariants("forecast lost mid-run", rows, c)
|
|
before = [r for r in rows if r["now"] < DROP and r["action"]
|
|
== arbitrage.CHARGE]
|
|
late = [r for r in rows if r["now"] > DROP + timedelta(
|
|
seconds=2 * 60 + 10) and r["action"] == arbitrage.CHARGE]
|
|
check("forecast lost mid-run (OPT-34): Leftover solar was charging, and goes "
|
|
"on charging past a forecast read (60 s) and a cycle, to its level",
|
|
before and late)
|
|
rows, st_, c = run_day(SF_LS, fc=lambda now: 1.0 if now < DROP else None,
|
|
soc0={"a": 60.0, "b": 10.0})
|
|
invariants("forecast unavailable mid-run, covered", rows, c)
|
|
check("forecast unavailable mid-run (OPT-27): the last reading covers it, so "
|
|
"Leftover solar goes on charging past the point where it was lost",
|
|
[r for r in rows if r["now"] < DROP and r["action"]
|
|
== arbitrage.CHARGE]
|
|
and [r for r in rows if r["now"] > DROP + timedelta(
|
|
seconds=2 * 60 + 10) and r["action"] == arbitrage.CHARGE])
|
|
|
|
# The forecast is far too high (40 kWh, and there is no sun at all): no grid
|
|
# charge, so B covers less of the evening - a money risk, never a safety one.
|
|
rows, st_, c = run_day(SF_LS, fc=40.0, soc0={"a": 30.0, "b": 30.0},
|
|
extra=GATE_030)
|
|
invariants("forecast too high", rows, c)
|
|
check("forecast too high: no grid charge, and B still keeps its power for "
|
|
"the dear run", not [r for r in rows if r["action"]
|
|
== arbitrage.CHARGE]
|
|
and all(r["roles"].get(B) != modetrade.GIVE for r in rows
|
|
if r["now"].astimezone(L).hour not in (19,)))
|
|
|
|
print()
|
|
if fails:
|
|
print(f"FAILED: {len(fails)}")
|
|
for f in fails:
|
|
print(" -", f)
|
|
sys.exit(1)
|
|
print("all checks passed")
|