Skip to content

feat(drivers): add host.http_put for device APIs that write with PUT - #2028

Open
yogiohlsson-ai wants to merge 1 commit into
srcfl:masterfrom
yogiohlsson-ai:feat/lua-http-put-core
Open

yogiohlsson-ai wants to merge 1 commit into
srcfl:masterfrom
yogiohlsson-ai:feat/lua-http-put-core

Conversation

@yogiohlsson-ai

Copy link
Copy Markdown

Need

A sonnenBatterie can only be steered through its local JSON API v2 by
switching its operating mode, and the unit accepts that write only as
PUT /api/v2/configurations. The Lua host offers http_patch and
http_post for plain-HTTP writes; http_request allows only GET and POST
over https. A sonnen control driver therefore cannot be written today.

Measured on one sonnenBatterie (software 1.40.5, JSON API v2, port 80) on
2026-11-13, with the unit's Auth-Token. The first row put the unit in manual
mode and the second set it back from 1 to 2. Every row except the first two
was sent with EM_OperatingMode 2, the mode the unit was already in, so it
changed nothing:

Request to /api/v2/configurations Body Result
PUT form EM_OperatingMode=1 200 {"EM_OperatingMode":"1"}; mode read back as 1
PUT form EM_OperatingMode=2 200 {"EM_OperatingMode":"2"}; mode read back as 2
PUT JSON {"EM_OperatingMode":"2"} 200 {"EM_OperatingMode":"2"}
POST form EM_OperatingMode=2 404
POST JSON {"EM_OperatingMode":"2"} 404
PATCH form EM_OperatingMode=2 404

PATCH with a JSON body was not tried. The owner repeated the PATCH and POST
requests on the unit and got 404 for both.

The controllable sonnen driver itself belongs in srcfl/device-drivers and
will follow as a separate PR once this host function exists. This PR changes
no driver.

Change

  • host.http_put(url, body, headers?) shares one implementation with
    host.http_patch: same capabilities.http and
    capabilities.http.allow_write gates, allowed_hosts fence, read-only
    probe and runtime-policy refusal (allowWrite("http.put")), JSON default
    Content-Type that a driver header overrides, 1 MB response cap and
    HTTP <status> error for 4xx/5xx.
  • The HTTP client's redirect policy now refuses a redirected PUT as it
    already did for PATCH. Without this, Go re-issues a 301/302/303 as a
    body-less GET and the write would report success although it never
    reached the device. The new redirect test failed before this line changed.
  • Comments on HTTPAllowWrite / AllowWrite and
    docs/writing-a-driver.md name both verbs.
  • Changeset: patch (new host function; no existing driver changes
    behaviour). The owner may prefer minor, as http_patch used.

Tests

New go/internal/drivers/lua_http_put_test.go:

  • method, body, JSON default Content-Type and headers on the wire;
  • a driver-supplied Content-Type (form body) wins over the default;
  • refused without allow_write, without the http capability, outside
    allowed_hosts, on redirect and during a fingerprint probe — in every
    case the server is never reached (or the redirect target never is).

lua_http_patch_test.go only gains a verb parameter in its helper; the
existing PATCH tests are unchanged and pass.

Run on a Raspberry Pi 4 Model B Rev 1.5 (aarch64, Debian 13 trixie), with
go version go1.26.0 linux/arm64:

go build ./... && go vet ./...                                            # ok
go test ./internal/drivers/ ./internal/config/ ./internal/api/ -count=1   # ok

make verify was not run on the Pi (it needs jq, not installed); the
drivers package ran with the pinned bundled drivers fetched by hand at
1e4b5de7.

Overlap

Open #2017 (fix(battery): explain calibration and suspend manual control)
also edits go/internal/drivers/lua.go and docs/writing-a-driver.md.
Checked on 2026-11-16 with gh pr view 2017 -R srcfl/ftw: open, not a
draft, no reviews, mergeable, head 5cf68a0f65. Its hunks are elsewhere
(set_calibrating). git merge-tree of this branch with that head merged
without conflicts. This branch is based on current master (5b25abcb).

🤖 Generated with Claude Code

sonnenBatterie's JSON API v2 changes its operating mode only through
PUT /api/v2/configurations; PATCH and POST there return 404. Add
host.http_put, sharing http_patch's implementation and gates:
capabilities.http plus allow_write, allowed_hosts, no writes during a
fingerprint probe, a JSON default Content-Type the driver can override.

The redirect policy now refuses a redirected PUT as it does PATCH, so a
301/302/303 cannot turn the write into a body-less GET that reports
success.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jörgen Ohlsson <yogiohlsson@gmail.com>
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