Skip to content

Commit d662c3e

Browse files
authored
fix(zap): read the meter DER under data model v2 (#208)
Some Zap gateway firmware serves the local API in data model v2 (srcful-data-models 2.3.0): every meter field gets an "_ac" suffix and the L1/L2/L3 phase prefix is lowercased (W -> W_ac, L1_V -> l1_V_ac; Hz is unchanged). emit_meter read the v1 names unconditionally, so a Zap on v2 firmware gave FTW no meter reading at all. Add raw_field(), which tries the v1 name, then the _ac suffix, then the lowercased-prefix _ac variant -- no explicit version branch needed. Scoped to the meter DER only; pv/battery have the identical assumption against v2's _ac/_dc split and are left to #170, since this site runs read_pv/read_battery both false and I have no v2 hardware with those enabled to verify against. Fixes (partially) #170. Signed-off-by: Marcus Murwall <[email protected]>
1 parent 107eb60 commit d662c3e

9 files changed

Lines changed: 196 additions & 13 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22

33
## [Unreleased]
44

5+
### zap 3.1.2
6+
7+
The P1/HAN site meter reads again on Zap firmware serving the data model v2
8+
local API. That firmware renames every meter field with an `_ac` suffix and
9+
lowercases the `L1`/`L2`/`L3` phase prefix (`W` → `W_ac`, `L1_V` → `l1_V_ac`;
10+
`Hz` is unchanged). `emit_meter` read the v1 names unconditionally, so a Zap
11+
on v2 firmware gave FTW no meter reading at all — `driver_poll` logged
12+
`Zap: site-meter payload unavailable` every cycle and FTW lost its site
13+
meter, with no site meter data for the home's grid import/export.
14+
15+
- `emit_meter` now tries the v1 field name first, then the `_ac` suffix,
16+
then the lowercased-phase-prefix `_ac` variant, so it reads either
17+
firmware generation without checking `version` explicitly.
18+
- Scoped to the meter DER only. `pv`/`battery` (`emit_pv`/`emit_battery`)
19+
have the identical `.W`-style field assumption against v2 and are not
20+
covered here — see #170 for that broader scope (`pv` → `solar` rename,
21+
`_dc`/`_ac` split, `null`-vs-`0` handling).
22+
23+
FTW's bundled copy does not move with this commit. Bump the pin and run
24+
`scripts/sync-bundled-drivers.sh`.
25+
526
### tesla_wall_connector 0.1.2
627

728
An idle Wall Connector no longer shows charge power. With no car on the

‎SUPPORT_STATUS.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ Catalog source is not proof that a target can install or run a driver.
180180
| victron_mqtt | 1.0.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
181181
| wallbox | 1.0.3 | ftw-core | not_assessed | — | not_recorded | not_assessed |
182182
| wallbox | 1.0.3 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
183-
| zap | 3.1.1 | ftw-core | not_assessed | — | not_recorded | not_assessed |
184-
| zap | 3.1.1 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
183+
| zap | 3.1.2 | ftw-core | not_assessed | — | not_recorded | not_assessed |
184+
| zap | 3.1.2 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
185185
| zaptec | 1.0.1 | ftw-core | not_assessed | — | not_recorded | not_assessed |
186186
| zaptec | 1.0.1 | blixt-l1 | not_assessed | — | not_recorded | not_assessed |
187187
| zaptec_cloud | 0.1.1 | ftw-core | not_assessed | — | not_recorded | not_assessed |

‎devices.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,7 @@ manufacturers:
17491749
protocols:
17501750
- protocol: http
17511751
driver: "zap"
1752-
version: "3.1.1"
1752+
version: "3.1.2"
17531753
ders: [meter, pv, battery]
17541754
control: false
17551755
firmware_versions: ""

‎drivers/lua/zap.lua‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@
3131
-- meter: +W import, -W export
3232
-- pv: -W generation
3333
-- battery:+W charge, -W discharge
34+
--
35+
-- Meter schema compatibility: some Zap firmware serves the local data model
36+
-- v2 (srcful-data-models 2.3.0, "version":"v2" in /data/json), which renames
37+
-- every meter field with an "_ac" suffix and lowercases the L1/L2/L3 phase
38+
-- prefix (e.g. "L1_V" -> "l1_V_ac", "W" -> "W_ac"; "Hz" is unchanged either
39+
-- way). This driver reads both v1 and v2 field names for the meter DER; see
40+
-- https://github.com/srcfl/device-drivers/issues/170 for the full v2 scope
41+
-- (pv -> solar, battery, DC/AC split), which is not yet covered here.
3442

3543
DRIVER = {
3644
host_api_min = 1,
3745
host_api_max = 1,
3846
id = "zap",
3947
name = "Sourceful Zap (P1/HAN meter)",
4048
manufacturer = "Sourceful",
41-
version = "3.1.1",
49+
version = "3.1.2",
4250
protocols = { "http" },
4351
capabilities = { "meter", "pv", "battery" },
4452
read_only = true,
@@ -377,15 +385,30 @@ local meter_fields = {
377385
{ "total_export_wh", "total_export_Wh", "Wh" },
378386
}
379387

388+
-- Data model v2 renames every meter field with an "_ac" suffix and
389+
-- lowercases the L1/L2/L3 phase prefix (e.g. "L1_V" -> "l1_V_ac"); "Hz" is
390+
-- unchanged. Try the v1 name first, then the v2 variants, so this driver
391+
-- reads either firmware generation without needing to branch on "version".
392+
local function raw_field(raw, name)
393+
if raw[name] ~= nil then return raw[name] end
394+
if raw[name .. "_ac"] ~= nil then return raw[name .. "_ac"] end
395+
local lowered = name:gsub("^L(%d)_", "l%1_")
396+
if lowered ~= name then
397+
if raw[lowered] ~= nil then return raw[lowered] end
398+
if raw[lowered .. "_ac"] ~= nil then return raw[lowered .. "_ac"] end
399+
end
400+
return nil
401+
end
402+
380403
local function emit_meter(data)
381404
if type(data) ~= "table" or type(data.meter) ~= "table" then return false end
382405
local raw = data.meter
383-
local w = number(raw.W)
406+
local w = number(raw_field(raw, "W"))
384407
if w == nil then return false end
385408

386409
local meter_reading = { w = w }
387410
for _, mapping in ipairs(meter_fields) do
388-
local value = number(raw[mapping[2]])
411+
local value = number(raw_field(raw, mapping[2]))
389412
if value ~= nil then
390413
meter_reading[mapping[1]] = value
391414
host.emit_metric("meter_" .. mapping[1], value, mapping[3])
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
-- Exercise zap.lua's meter field reading against both the v1 and v2 Zap
2+
-- local API field names. Run with the scenario as arg[1]: "v1" or "v2".
3+
--
4+
-- This file is a draft for drivers/tests/lua_harness/test_zap_meter_schema.lua
5+
-- in srcfl/device-drivers — see pr-drafts/README.md for how this fits that
6+
-- repo's `make test-driver ID=zap` harness. Not run against the real harness
7+
-- yet (no local checkout with lua55/pytest); matches the structure of the
8+
-- existing drivers/tests/lua_harness/test_esphome_dsmr_aliases.lua, which
9+
-- solves the same kind of field-name-aliasing problem.
10+
11+
local scenario = arg[1]
12+
if scenario ~= "v1" and scenario ~= "v2" then
13+
error("schema scenario must be v1 or v2")
14+
end
15+
16+
dofile("drivers/tests/lua_harness/host_mock.lua")
17+
host.reset()
18+
19+
local function respond(path, body)
20+
host._http_responses["http://127.0.0.1" .. path] = body
21+
end
22+
23+
-- The device list is unaffected by the v2 meter-field rename (see
24+
-- https://github.com/srcfl/device-drivers/issues/170 — only `ders[].type`
25+
-- for pv/battery changes, not for the P1 meter), so both scenarios use the
26+
-- same /api/devices response.
27+
respond("/api/devices", [[
28+
{"devices":[{"sn":"TESTZAP-P1-1","type":"p1_uart","device_type":"energy_meter",
29+
"ders":[{"type":"meter","enabled":true}]}]}
30+
]])
31+
32+
if scenario == "v1" then
33+
respond("/api/devices/TESTZAP-P1-1/data/json", [[
34+
{"meter":{"W":1850,"Hz":50.0,
35+
"L1_W":600,"L1_V":230.1,"L1_A":2.61,
36+
"L2_W":600,"L2_V":230.4,"L2_A":2.60,
37+
"L3_W":650,"L3_V":229.8,"L3_A":2.83,
38+
"total_import_Wh":58000000,"total_export_Wh":9000000}}
39+
]])
40+
else
41+
respond("/api/devices/TESTZAP-P1-1/data/json", [[
42+
{"meter":{"W_ac":1850,"Hz":50.0,
43+
"l1_W_ac":600,"l1_V_ac":230.1,"l1_A_ac":2.61,
44+
"l2_W_ac":600,"l2_V_ac":230.4,"l2_A_ac":2.60,
45+
"l3_W_ac":650,"l3_V_ac":229.8,"l3_A_ac":2.83,
46+
"total_import_Wh_ac":58000000,"total_export_Wh_ac":9000000},
47+
"version":"v2","format":"p1_uart"}
48+
]])
49+
end
50+
51+
dofile("drivers/lua/zap.lua")
52+
53+
if DRIVER.id ~= "zap" or DRIVER.version ~= "3.1.2" then
54+
error("Zap identity metadata is wrong or version was not bumped")
55+
end
56+
if DRIVER.host_api_min ~= 1 or DRIVER.host_api_max ~= 1 or DRIVER.read_only ~= true then
57+
error("Zap host API or read-only metadata is wrong")
58+
end
59+
60+
driver_init({ host = "127.0.0.1" })
61+
local ok, err = pcall(driver_poll)
62+
if not ok then error("driver_poll failed: " .. tostring(err)) end
63+
64+
local samples = host._emitted.meter
65+
if not samples or #samples ~= 1 then
66+
error("expected exactly one meter sample for scenario " .. scenario
67+
.. ", got " .. tostring(samples and #samples or 0))
68+
end
69+
70+
local meter = samples[1]
71+
local function near(actual, expected)
72+
return type(actual) == "number" and math.abs(actual - expected) < 0.001
73+
end
74+
75+
if not near(meter.w, 1850) then error(scenario .. ": meter.w wrong: " .. tostring(meter.w)) end
76+
if not near(meter.l1_w, 600) or not near(meter.l2_w, 600) or not near(meter.l3_w, 650) then
77+
error(scenario .. ": phase power wrong")
78+
end
79+
if not near(meter.l1_v, 230.1) or not near(meter.l2_v, 230.4) or not near(meter.l3_v, 229.8) then
80+
error(scenario .. ": phase voltage wrong")
81+
end
82+
if not near(meter.l1_a, 2.61) or not near(meter.l2_a, 2.60) or not near(meter.l3_a, 2.83) then
83+
error(scenario .. ": phase current wrong")
84+
end
85+
if not near(meter.freq_hz, 50.0) then error(scenario .. ": Hz wrong (unsuffixed in both schemas)") end
86+
if not near(meter.total_import_wh, 58000000) or not near(meter.import_wh, 58000000) then
87+
error(scenario .. ": total_import_Wh / import_wh alias wrong")
88+
end
89+
if not near(meter.total_export_wh, 9000000) or not near(meter.export_wh, 9000000) then
90+
error(scenario .. ": total_export_Wh / export_wh alias wrong")
91+
end
92+
93+
driver_cleanup()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Exercise zap.lua's meter reading against the v1 and v2 Zap local API schemas.
2+
3+
Draft for drivers/tests/test_zap_meter_schema.py in srcfl/device-drivers,
4+
paired with drivers/tests/lua_harness/test_zap_meter_schema.lua. Modeled on
5+
the existing test_esphome_dsmr_aliases.py, which runs the equivalent
6+
field-aliasing check for a different driver through the same lua55 harness.
7+
"""
8+
9+
from pathlib import Path
10+
import re
11+
import subprocess
12+
13+
import pytest
14+
15+
16+
ROOT = Path(__file__).resolve().parents[2]
17+
18+
19+
def test_zap_source_has_ftw_read_only_contract():
20+
source = (ROOT / "drivers/lua/zap.lua").read_text(encoding="utf-8")
21+
for entrypoint in ("driver_init", "driver_poll", "driver_command",
22+
"driver_default_mode"):
23+
assert re.search(rf"\bfunction\s+{entrypoint}\s*\(", source), entrypoint
24+
assert re.search(r"\bhost_api_min\s*=", source)
25+
assert re.search(r"\bhost_api_max\s*=", source)
26+
assert re.search(r"\bread_only\s*=\s*true\b", source)
27+
assert not re.search(
28+
r"\bhost\.(?:http_post|modbus_write(?:_multi)?|mqtt_publish|serial_write)\s*\(",
29+
source,
30+
), "a read-only driver calls a write-capable host function"
31+
32+
33+
@pytest.mark.parametrize("scenario", ["v1", "v2"])
34+
def test_zap_meter_schema(scenario):
35+
result = subprocess.run(
36+
[
37+
str(ROOT / "lua55"),
38+
"drivers/tests/lua_harness/test_zap_meter_schema.lua",
39+
scenario,
40+
],
41+
cwd=ROOT,
42+
text=True,
43+
capture_output=True,
44+
check=False,
45+
)
46+
assert result.returncode == 0, result.stdout + result.stderr

‎index.yaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -814,14 +814,14 @@ drivers:
814814
size_bytes: 2842
815815
sha256: "1c0362e0a25f1fe297de838a85089eb7676b25e78a7d6d1d03ccea1258b94403"
816816
- name: "zap"
817-
version: "3.1.1"
817+
version: "3.1.2"
818818
tier: core
819819
protocol: http
820820
connectivity: local
821821
ders: [meter, pv, battery]
822822
control: false
823-
size_bytes: 26911
824-
sha256: "dbbef0e614162219c3774292c647f944d0210c752688165afb9a491244700c19"
823+
size_bytes: 28162
824+
sha256: "5be6d7793a9daa074557182e4333443b382a7a34427dd07143d90f27a8cf01b6"
825825
- name: "zaptec"
826826
version: "1.0.1"
827827
tier: community

‎manifests/zap.yaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "zap"
2-
version: "3.1.1"
2+
version: "3.1.2"
33
tier: core
44
author: "Sourceful Labs AB"
55
protocol: http
@@ -15,9 +15,9 @@ tested_devices:
1515
notes: "P1/HAN site meter from a Sourceful Zap. PV and battery telemetry from devices on Zap are opt-in (read_pv / read_battery) and read-only. Prefer a native FTW driver; use Zap as the reader only when it already owns the bus."
1616
min_driver_version: "3.1.0"
1717
min_host_version: "2.0.0"
18-
size_bytes: 26911
18+
size_bytes: 28162
1919
dkb_id: "zap"
20-
sha256: "dbbef0e614162219c3774292c647f944d0210c752688165afb9a491244700c19"
20+
sha256: "5be6d7793a9daa074557182e4333443b382a7a34427dd07143d90f27a8cf01b6"
2121
signature: ""
2222

2323
bytecode_sha256: ""

‎support-status.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,7 @@
18501850
},
18511851
{
18521852
"catalog_source": true,
1853-
"catalog_version": "3.1.1",
1853+
"catalog_version": "3.1.2",
18541854
"driver_id": "zap",
18551855
"targets": {
18561856
"blixt-l1": {

0 commit comments

Comments
 (0)