OPT-11 P3: price runs per battery
checks / unit (pull_request) Successful in 13m45s
infra-pin / fresh (pull_request) Successful in 1s

Cheap and expensive hours as runs of consecutive firm price slots
(priceruns.py); each battery's role per tick from its mode (modetrade.py):
grid charging for Leftover solar / Cheap hours only, never Solar first;
holding for the expensive runs; selling per battery (pack_sell) above the
spread and the floor. The site trade is sized inside the modes (loop bound)
and the peak guard's headroom; a run edge reseeds the loop. Cheap hours only
and selling are enabled on the Batteries screen and in the HA select
(appended). The site Why row no longer says "held back" when the peak limit
pushes power up (F2). Modes off: byte-identical to 7a00f12 on the trade path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude dev agent
2026-09-29 02:37:44 +02:00
parent 7a00f12c76
commit 902524ae5e
19 changed files with 2202 additions and 113 deletions
+25 -21
View File
@@ -1,4 +1,4 @@
"""OPT-11 P1: each battery's mode and on/off switch, as limits and weights.
"""OPT-11: each battery's mode and on/off switch, as limits and weights.
Pure: no I/O, no clock. main.py reads the store, calls these, and files the
result as a `mode` claim (arbiter.P_MODE) and as the charge weights.
@@ -14,13 +14,11 @@ Two rules hold for every mode, and they are what keeps modes safe:
allocator._fill()'s last-resort pass still uses it. So "may not" is a range,
and weights only set the order among batteries that may.
P1 has no price runs and no forecast (P3, P4). So both modes give power to the
house at any hour, and the only mode that closes a direction is Off. What P1
changes is the solar order: while the site charges, Leftover solar batteries
weigh 0, so they get solar only once the Solar first batteries are full or at
their power limit. Selling (the owner's 2026-09-27 answer) is a per-battery
choice that arrives with the price runs in P3; `PackMode.sell` and the
`pack_sell` store key are reserved for it and read by nothing yet.
`mode_range()` is the switch alone (Off is [0, 0]). P3 adds the price runs:
`modetrade.decide()` narrows each battery's range by its role this tick
(hold for the expensive hours, charge from the grid, sell), and main.py files
the intersection. Selling is a per-battery choice (`pack_sell`, the owner's
2026-09-27 answer), for Leftover solar and Cheap hours only.
"""
from dataclasses import dataclass
@@ -29,17 +27,18 @@ from .store import unit_key
SOLAR_FIRST = "solar_first"
LEFTOVER_SOLAR = "leftover_solar"
CHEAP_HOURS = "cheap_hours" # reserved for P3; not accepted in P1
CHEAP_HOURS = "cheap_hours" # OPT-11 P3
# The modes a battery may be set to in this release, in the order the page
# lists them. The first is the default when the store has no value.
MODES = (SOLAR_FIRST, LEFTOVER_SOLAR)
# The modes a battery may be set to, in the order the page and the HA select
# list them. The first is the default when the store has no value. A new mode
# is appended, never inserted: automations send the HA labels (P2).
MODES = (SOLAR_FIRST, LEFTOVER_SOLAR, CHEAP_HOURS)
DEFAULT_MODE = SOLAR_FIRST
# Store keys, per battery: `unit/<name>/<key>` (store.unit_key()).
USE_KEY = "pack_use"
MODE_KEY = "pack_mode"
SELL_KEY = "pack_sell" # reserved for P3
SELL_KEY = "pack_sell" # OPT-11 P3: sell in the sell run
def enabled(opts) -> bool:
@@ -50,10 +49,15 @@ def enabled(opts) -> bool:
@dataclass(frozen=True)
class PackMode:
"""One battery's choice: on or off, its mode, and (P3) selling."""
"""One battery's choice: on or off, its mode, and selling."""
use: bool = True
mode: str = DEFAULT_MODE
sell: bool = False # reserved for P3, never read in P1
sell: bool = False
@property
def can_sell(self) -> bool:
"""Selling applies to the price modes only; Solar first never sells."""
return self.mode in (LEFTOVER_SOLAR, CHEAP_HOURS)
@property
def key(self) -> str:
@@ -73,11 +77,10 @@ def read(data: dict, name: str) -> PackMode:
def mode_range(pm: PackMode, rating_w: float) -> tuple:
"""`(lo, hi)` in W for this battery's mode claim. Always holds 0 W.
"""`(lo, hi)` in W for this battery's switch. Always holds 0 W.
Off asks for 0 W both ways. In P1 both modes may charge and give power up
to the battery's rating: Leftover solar holds back its power only in the
planned expensive hours, and there are none before P3.
Off asks for 0 W both ways; On allows the rating both ways. main.py files
this intersected with the role range from `modetrade.decide()`.
"""
if not pm.use:
return 0.0, 0.0
@@ -92,7 +95,7 @@ def charge_weights(base, modes: dict, target_w: float):
charges (`target_w < 0`), a Leftover solar battery weighs 0 and a Solar
first battery keeps its weight, so the fill gives the Solar first batteries
everything they can take before the last-resort pass reaches the others.
Giving power keeps `base` (P3 adds the expensive-run order).
Giving power keeps `base`; `modetrade.weights()` adds the run order.
When no battery that is on would keep a positive weight (all Leftover
solar, or every Solar first one at 0), `base` is returned unchanged, so the
@@ -102,7 +105,8 @@ def charge_weights(base, modes: dict, target_w: float):
return base
names = sorted(modes)
w = {n: (1.0 if base is None else float(base.get(n, 1.0))) for n in names}
out = {n: (0.0 if modes[n].mode == LEFTOVER_SOLAR else w[n])
out = {n: (0.0 if modes[n].mode in (LEFTOVER_SOLAR, CHEAP_HOURS)
else w[n])
for n in names}
if not any(out[n] > 0.0 for n in names if modes[n].use):
return base