|
| 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() |
0 commit comments