Business operations API: get_battery_soc/get_household_wattage/get_device_inventory

This commit is contained in:
kfj001 committed 2026-10-16 04:58:33 +00:00
1 parent 363c7cc6a2
commit b79adc5d54
7 files changed
+307 -112

No files matched your search

+23 -30
View File
@@ -1,8 +1,8 @@
#!/usr/bin/env python3
"""Host probe v8 — live household wattage via the library business API.
"""Host probe v8 — household wattage via the business operations API.
Three socket APIs: realtime detail (home-page poll) -> ``totalPower``,
per-switch load status -> ``SWxExpPower``, EMS telemetry -> watts math.
One call per port: ``LocalDeviceClient.get_household_wattage`` (realtime
detail → per-switch load → EMS math). Prints the watts.
Run from the repo root on the SoftAP-joined host:
python3 scripts/host_probe8.py --sn A03X2516013210060005 --ports 9000
@@ -20,50 +20,43 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(
from franklinwh_agate_client.device_local import ( # noqa: E402
LocalDeviceClient,
)
from franklinwh_agate_client.power import ( # noqa: E402
ems_load_watts,
extract_power,
)
from franklinwh_agate_client.protocol import ( # noqa: E402
generate_secret_key,
)
def run(label, client, call):
try:
reply = call()
except (ConnectionError, socket.error, TimeoutError) as exc:
print("--- %s: error: %s: %s" % (label, type(exc).__name__, exc))
return None
print("--- %s" % label)
print(" reply: " + json.dumps(reply, default=str)[:2000])
result = extract_power(reply)
value, source = result["value"], result["source"]
if value is None:
watts = ems_load_watts(reply)
if watts is not None:
value, source = watts, "ems(loadCurr*V)"
print(" ==> watts: %s (source=%s)" % (value, source))
return reply
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--sn", required=True)
ap.add_argument("--host", default="10.100.1.1")
ap.add_argument("--ports", default="9000")
ap.add_argument("--timeout", type=float, default=5.0)
ap.add_argument("--raw", action="store_true",
help="print the full reply envelope")
args = ap.parse_args()
print("secret key for %s: %s (radix 16)"
% (args.sn, generate_secret_key(args.sn)))
for port in [int(p) for p in args.ports.split(",") if p.strip()]:
print("=== port %d" % port)
client = LocalDeviceClient(args.sn, host=args.host,
socket_port=port, timeout=args.timeout)
run("detail/1301", client, client.query_detail_info)
run("load/1411", client, client.query_load_status)
run("ems/1707", client, client.query_ems_info)
try:
result = client.get_household_wattage()
except (ConnectionError, socket.error, TimeoutError) as exc:
print("=== port %d: error: %s: %s"
% (port, type(exc).__name__, exc))
continue
print("=== port %d" % port)
if args.raw:
print(json.dumps(result.get("reply"), indent=2, default=str))
return 0
value = result["value"]
if value is not None:
print("==> watts: %gW (source=%s, cmd=%s)"
% (value, result["source"], result["cmd"]))
else:
print("==> watts: unknown")
return 0
if __name__ == "__main__":
main()
raise SystemExit(main())