#!/usr/bin/env python3 """TARIFF-30 (T8): a decision trace of the grid-charge code with NO efficiency set, to be compared with one captured on release/1.0 831c33c. It uses only the API 831c33c already has, so the same file runs on the base and on the head: `python3 tools/tariff30_trace.py` prints one JSON object, per group the number of calls and the sha256 of every outcome in order. The golden copy in scenarios/tariff30-base-trace.json was printed on 831c33c. The test compares the head's against it, so with no efficiency set arbitrage.decide(), the battery-mode charge/sell decision and its energy sizing give the same answers as before the change, field by field. """ import hashlib import itertools import json import os import sys from datetime import datetime, timedelta, timezone # T30_TREE: another checkout's goodwe_controller/ (how the golden was printed, # from a 831c33c worktree, with this very file). HERE = os.environ.get("T30_TREE") or os.path.dirname( os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, HERE) sys.path.insert(0, os.path.join(HERE, "tools")) import ux54_scenarios as S # noqa: E402 from app import arbitrage, modetrade, priceruns # noqa: E402 from app.price import Uncertainty # noqa: E402 from app.static_tariff import static_plan # noqa: E402 from app.tariff import BILLING_TZ # noqa: E402 UTC = timezone.utc NO_WX = frozenset({Uncertainty.NO_WEATHER}) def _plan(now): opts = dict(S.BASE) opts.update(static_profile=json.dumps(S.DN)) return static_plan(opts, S.day0_of(now), S.day0_of(now) + timedelta(hours=48), now=now) def _digest(rows): h = hashlib.sha256() for r in rows: h.update(repr(r).encode()) h.update(b"\n") return {"n": len(rows), "sha256": h.hexdigest()} def arbitrage_rows(): """`arbitrage.decide()` over buy, sell and off-peak instants, every SoC, headroom, guard state, threshold and size the call can be given.""" rows = [] for hh, mm in ((22, 5), (23, 30), (3, 0), (7, 0), (10, 0), (21, 59)): now = datetime(2026, 11, 1, hh, mm, tzinfo=BILLING_TZ).astimezone(UTC) plan = _plan(now) for soc, room, guard, thr, cw, dw in itertools.product( (None, 5.0, 50.0, 96.0), (None, 0.0, 1200.0, 10000.0), (True, False, None), (0.0, 0.03, 0.10), (0.0, 5000.0, 9600.0), (0.0, 9600.0)): d = arbitrage.decide( plan, now, peak_guard_active=guard, charge_headroom_w=room, soc=soc, min_spread_eur_kwh=thr, uncertainty=NO_WX, charge_w=cw, discharge_w=dw, soc_floor_pct=10.0, soc_ceiling_pct=95.0) rows.append((hh, mm, soc, room, guard, thr, cw, dw, d.action, d.power_w, d.reason, d.spread_eur_kwh, d.required_eur_kwh, d.price_now_eur_kwh)) return rows def mode_rows(): """`modetrade.chargers()`, `energy()` and `decide()` over mode, level, refusal and probe states of two batteries, in a cheap run.""" rows = [] now = datetime(2026, 11, 1, 23, 30, tzinfo=BILLING_TZ).astimezone(UTC) plan = _plan(now) modes = ("solar_first", "leftover_solar", "cheap_hours") for ma, mb, soca, socb, refb, probb, ok, room, blk in itertools.product( modes, modes, (20.0, 99.0, None), (50.0,), (0, -1, 1), (False, True), (True, False), (None, 300.0, 6000.0), (False, True)): packs = [modetrade.Pack("A", True, ma, False, 5000.0, soca, 15.36), modetrade.Pack("B", True, mb, False, 4600.0, socb, 20.48, refused=refb, probing=probb)] e = modetrade.energy(packs, 10.0, 95.0) rp = priceruns.plan(plan.read.points, now, required_eur_kwh=0.0, charge_kwh=e[0], charge_kw=e[1], sell_kwh=e[2], sell_kw=e[3]) ch = [p.name for p in modetrade.chargers(packs, 95.0, probe=True)] t = modetrade.decide( rp, now, packs, charge_ok=ok, sell_ok=ok, trade_why="not_worth", price_now=0.2333, headroom_w=room, floor_pct=10.0, ceiling_pct=95.0, house_w=500.0, blocked=blk) rows.append((ma, mb, soca, socb, refb, probb, ok, room, blk, e, ch, sorted(t.ranges.items()), sorted(t.roles.items()), t.action, t.ask_w, t.why, t.probe_w, modetrade.size(t, (-9600.0, 9600.0), room))) return rows def trace(): return {"arbitrage_decide": _digest(arbitrage_rows()), "modetrade": _digest(mode_rows())} if __name__ == "__main__": print(json.dumps(trace(), indent=1, sort_keys=True))