Skip to content

fix(sungrow): refuse a battery command on an inverter with no battery - #39

Merged
frahlg merged 2 commits into
mainfrom
claude/frosty-shtern-20c282
Jul 28, 2026
Merged

frahlg merged 2 commits into
mainfrom
claude/frosty-shtern-20c282

Conversation

@frahlg

@frahlg frahlg commented Jul 28, 2026

Copy link
Copy Markdown
Member

Summary

driver_command accepted a battery setpoint on a model this driver had already classified as a string inverter. The read path learned the two Sungrow families apart in 1.4.0; the write path had not.

Accepting was worse than a no-op. Measured against the harness mock, one command wrote three registers and returned success:

RETURN  true
WROTE  13049=2      forced EMS mode
WROTE  13050=0xAA   force charge
WROTE  13051=1000   setpoint

An SG string inverter does not implement the 13xxx block, so those are unexpected writes to live hardware. The read-back that would have caught the mismatch fails on such a device, and set_battery_power treats a failed read-back as transient and assumes the writes are good. So the host recorded an applied setpoint, renewed the lease on that answer, and the planner went on dispatching a battery that is not there.

It now refuses with code no_battery — the same code packages/v1/sungrow/targets/ftw.lua already returns, so the two control paths refuse in one vocabulary — writes nothing, and logs the reason the way curtail already does when rated power is missing. Curtail itself is untouched: an SG inverter has no battery, but it does have PV.

This shipped once already

if model_family == "string" then return false end shipped in #17 as part of 1.2.1, with the comment explaining why. Promoting FTW's drivers in #27 replaced this file wholesale and removed it. #29 restored only the read half at 1.4.0. The guard survived in the FTW v2 target, whose own test stayed green throughout.

Third time this shape of regression reaches a catalog driver, after Pixii's 40288 and SolarEdge legacy's 40123 — and the third time a package target's green test hid it. The comment above the guard now says so, and says not to remove it again.

The test had to move

drivers/tests/lua_harness/test_all_drivers.lua caught this. It failed on unmodified main and gated nothing, because CI runs pytest and not the Lua harness. The guard now lives in drivers/tests/test_sungrow_model_family.py, which runs.

Verified against the unfixed driver — the two refusal tests fail, the two over-refusal guards pass:

Test Unfixed driver
String inverter refuses a battery command (return value + zero writes) fails
Refusal carries no_battery, unchanged, and a reason in the log fails
Hybrid still takes a battery command passes
String inverter still takes a curtail passes

Driver evidence

  • Driver id: sungrow (1.5.0 → 1.5.1)
  • Device make/model: Sungrow SG-RT / SG-CX three-phase string inverters (the SG12RT of the original outage); regression risk covers SH-RT / SH-RS / SH-T hybrids
  • Firmware tested: not applicable — no firmware change, and no new hardware claim
  • Protocol/register source: unchanged. Registers 13049/13050/13051 (EMS mode, force cmd, force power) as already documented in the driver header
  • Hardware test performed: none. This change only stops writes that were reaching hardware that cannot accept them, so it removes hardware exposure rather than adding it. Evidence is the harness mock, quoted above, plus the pytest guards. HIL is still required before the FTW control target's control_enabled moves
  • Known limits: set_self_consumption() still writes the same EMS block unconditionally from driver_init, deinit, driver_default_mode and driver_cleanup. Left out on purpose — the host blocks later control when default mode fails, and an SG inverter does support curtail, so refusing there is a different safety call and needs its own change

Safety

  • The driver starts read-only, or this is a separately reviewed control change. — Control change, and it only narrows what the driver will write
  • Vendor signs are converted at the driver boundary. — Unchanged
  • Cached telemetry becomes stale instead of being re-emitted as fresh. — Unchanged
  • No credentials, serial numbers, private addresses or site data are included.

Package or promotion evidence

  • Public source SHA: 12001703cb0c8250a057ff351dc29d09c7f87ee2277cf841298c96138844929e (drivers/lua/sungrow.lua, 41903 bytes)
  • Package ID and new version: com.sourceful.driver.sungrow — unchanged at 1.3.2. The package builds from packages/v1/sungrow/targets/ftw-observe.lua, which this PR does not touch
  • Target matrix: ftw-core only; make package-driver ID=sungrow TARGET=ftw-core builds clean
  • Material and candidate artifact SHA-256: not applicable — no package version change
  • HIL evidence or required: required, and unchanged by this PR. packages/v1/sungrow/targets/ftw.lua already refuses with no_battery and stays at control_enabled: false
  • Control enabled: false
  • Control changes include default-mode, lease-expiry and HIL evidence. — Default mode and lease behaviour are unchanged; this PR only makes the driver stop claiming a lease-renewing success it never earned

Checks

  • Commits include Signed-off-by.
  • make test-driver ID=sungrow — 57 passed, 4 skipped
  • make package-driver ID=sungrow TARGET=ftw-core — unsigned candidate 1.3.2 for ftw-core
  • make check — 2115 passed, 2251 skipped, no generator drift

Three Lua-harness drivers still fail — ferroamp, fronius, sonnen. They fail identically on unmodified main; they are harness fixture gaps, unrelated to this change.

🤖 Generated with Claude Code

frahlg and others added 2 commits July 28, 2026 20:38
driver_command accepted a battery setpoint on a model this driver had
already classified as a string inverter. That is not a no-op. Measured
against the harness mock, one command wrote three registers and returned
success:

    RETURN  true
    WROTE  13049=2      forced EMS mode
    WROTE  13050=0xAA   force charge
    WROTE  13051=1000   setpoint

An SG string inverter does not implement the 13xxx block, so these are
unexpected writes to live hardware. The read-back that would have caught
the mismatch fails on such a device, and set_battery_power treats a
failed read-back as transient and assumes the writes are good. So the
host recorded an applied setpoint, renewed the lease on that answer, and
the planner went on dispatching a battery that is not there.

The guard shipped once already, in #17 as part of 1.2.1. Promoting FTW's
drivers in #27 replaced this file wholesale and removed it; #29 restored
only the read half at 1.4.0. It survived in the FTW v2 target, whose own
test stayed green throughout. This is the third time that shape of
regression reaches a catalog driver after Pixii's 40288 and SolarEdge
legacy's 40123, and the third time a package target's green test hid it.

It now refuses with code no_battery -- the same code
packages/v1/sungrow/targets/ftw.lua already returns, so the two control
paths refuse in one vocabulary -- writes nothing, and logs the reason the
way curtail already does when rated power is missing. Curtail itself is
untouched: an SG inverter has no battery, but it does have PV.

drivers/tests/lua_harness/ caught this and gated nothing, because CI runs
pytest and not the Lua harness. The guard now lives in
drivers/tests/test_sungrow_model_family.py. It pins the refusal, the code
and the logged reason, and pins the two ways refusing could go too far: a
hybrid must still take a battery command, and a string inverter must
still take a curtail. Verified against the unfixed driver -- the two
refusal tests fail, the two over-refusal guards pass.

Signed-off-by: Fredrik Ahlgren <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Both sides added an entry at the top of the same Unreleased "Fixed"
block: sungrow 1.5.1 here, sigenergy 1.1.1 from #38. Kept both.

Signed-off-by: Fredrik Ahlgren <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2eb73924cd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread drivers/lua/sungrow.lua Resolved
@frahlg
frahlg merged commit 619659c into main Jul 28, 2026
5 checks passed
@frahlg
frahlg deleted the claude/frosty-shtern-20c282 branch July 28, 2026 18:43
frahlg added a commit that referenced this pull request Jul 28, 2026
#37 taught the boundary check to scan the repository rather than the
disk under it, so make check passes locally again with worktrees
present. #38 removed sigenergy's SoH read. #39 stopped sungrow
accepting a battery command on an inverter with no battery.

Two real conflicts, both in drivers this branch had also touched:

  sigenergy  main removed the SoH read at 30087 outright; this branch
             had bounded it. Removing a read whose result is discarded
             is strictly better than bounding it, so main's side stands
             and the bounded probe covers the remaining five reads.

  sungrow    merged cleanly in the Lua: main's control-path guard plus
             this branch's bounded reads through optional_read. Zero
             violations, and the string-inverter refusal is intact.

Both files now differ from main's bytes under main's version, so both
move again -- sigenergy 1.1.1 -> 1.1.2, sungrow 1.5.1 -> 1.5.2. This is
exactly what the signed channel refuses to publish otherwise, and what
the SemVer gate in ftw#709 checks across a pin move.

Swept the whole catalog afterwards: zero absent-register violations
across every driver. make check green, 3008 tests pass.

Co-Authored-By: Claude Opus 5 <[email protected]>
Signed-off-by: Fredrik Ahlgren <[email protected]>
frahlg added a commit that referenced this pull request Jul 28, 2026
Codex review on #39 found the guard incomplete, and it was right. When
register 4999 fails or returns the documented 0/0xFFFF sentinel,
detection settles on "unknown" and nothing ever classifies the device as
"string". A guard testing for "string" alone falls straight through, so
an SG inverter with an unreadable device-type register still got the
13049/13051 writes and still reported success.

The suggested fix -- gate on a positively identified "hybrid" -- trades
one outage for another. An SH inverter whose 4999 is unreadable settles
on "unknown" too, but its battery block answers, so the driver reads that
battery and reports its SoC every five seconds. Refusing to command a
battery the driver is actively reading is its own outage, and detection
giving up is common enough that DETECT_ATTEMPTS exists for it.

So the rule is evidence rather than labels: take a battery command when
the device named itself a hybrid, or when its battery registers have
answered. Refuse otherwise. The read path may probe its way out of
"unknown" because a failed read is bounded and self-correcting; a write
to a register the model does not implement is neither.

packages/v1/sungrow/targets/ftw.lua had the identical gap and gets the
identical fix, so both control paths still refuse on one rule with one
no_battery code.

The v2 target had no pytest at all -- only the Lua harness, which CI does
not run, which is the same way Pixii's 40288 came back. One parametrised
test now holds both files to the rule, as #34 learned to do. It pins the
refusal on a named string inverter and on an unidentified one that
answers no battery register, and pins both ways refusing could go too
far: a hybrid must still take a battery command, named or merely
observed, and a string inverter must still take a curtail.

Verified against the previous guard: the unidentified-string case fails
for the catalog driver and the v2 target alike.

Signed-off-by: Fredrik Ahlgren <[email protected]>
Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant