API Reference¶
Auto-generated from the source docstrings. Everything below is importable from
the top-level package, e.g. from franklinwh_direct_connect_api import LocalClient.
High-level client¶
The friendliest entry point: connect, log in, call named reads.
franklinwh_direct_connect_api.client.LocalClient
module-attribute
¶
Transport¶
Lower-level framed TCP client (login + request/response, full Frame objects).
franklinwh_direct_connect_api.transport.LocalTransport
module-attribute
¶
franklinwh_direct_connect_api.transport.TransportError ¶
Bases: RuntimeError
Protocol¶
Cipher, frame encode/decode, the streaming reader, and the pcap reader.
franklinwh_direct_connect_api.protocol.Frame
dataclass
¶
franklinwh_direct_connect_api.protocol.encode_frame ¶
encode_frame(cmd_type: int, equip_no: str, data_area: Any, *, type: int = 0, time_stamp: int = 0, snno: int = 0, seed: int | None = None, cipher: bool = True) -> bytes
Build a complete on-the-wire frame.
seed defaults to the value appropriate for equip_no. Pass
cipher=False for the cleartext cloud-REST body.
franklinwh_direct_connect_api.protocol.decode_frame ¶
Parse one complete frame (cleartext header + (ciphered) remainder).
franklinwh_direct_connect_api.protocol.detect_seed ¶
Recover the seed from a ciphered region (plaintext[0] is always '"').
franklinwh_direct_connect_api.protocol.FrameStream ¶
Feed raw bytes from one direction of a TCP connection; pull complete Frames.
Handles frames split across reads and several frames per read. The seed is detected per frame, so logins and normal frames decode transparently.
feed ¶
Decode every complete frame currently buffered.
A region that cannot be parsed is skipped and decoding CONTINUES. It used
to stop: _try_one returned (None, 0) for both "need more bytes" and
"skipped garbage", and feed broke on either -- so one damaged frame
silently discarded the rest of the stream. Those two cases are now
distinguished by consumed.
flush ¶
Decode what remains when NO more bytes are coming (a file, not a socket).
feed must wait when a frame's JSON is incomplete, because on a live
socket the rest is still in flight. For a capture the buffer is already
complete, so waiting means losing everything after the first gap -- which
is exactly what happened: a 1508-packet capture decoded as 12 frames with
no error reported, and resynchronising recovered 335.
Call this after the final feed. Skipped bytes are counted in
:attr:skipped_bytes.
franklinwh_direct_connect_api.protocol.iter_pcap_frames ¶
Yield decoded Frames from a classic-format pcap, with TCP reassembly.
Command catalog¶
franklinwh_direct_connect_api.catalog.Cmd ¶
Bases: IntEnum
Known request cmdType codes (the reply is value + 1).
franklinwh_direct_connect_api.catalog.CmdInfo
dataclass
¶
franklinwh_direct_connect_api.catalog.describe ¶
Human-readable label for a request or response cmdType.
franklinwh_direct_connect_api.catalog.response_for ¶
The response code paired with a request code.
Known requests use the catalogued response. Unknown odd requests fall back to
the protocol convention (response = request + 1) so a caller can still match the
reply frame — important when probing un-catalogued cmdTypes. Returns None for
even (non-request) codes.
franklinwh_direct_connect_api.catalog.run_status_desc ¶
Human label for a 1301 run_status code (aligns with the cloud API's
RUN_STATUS).
franklinwh_direct_connect_api.catalog.mode_label ¶
Canonical mode name for a 1726 list entry.
The local broker labels the TOU mode with a SITE-SPECIFIC tariff name
(e.g. "Ausgrid EA11 TOU"); the mobile app and FWHAI show the generic
"Time-of-Use" instead, which is clearer. Maps scheduling_type to the
canonical name, falling back to the raw name if it's unknown.
Also exported from the top level, as plain data rather than functions:
WRITES (the write table — cmdType, opt, fields and how each was confirmed),
RUN_STATUS (see the caveat in PROTOCOL.md),
OPERATING_MODES, WORK_MODE_NAMES and CATALOG itself.
Energy history, rollups and TOU¶
Stateless helpers over 1303 / 1407 payload dicts — sampling and persistence stay with
the caller. See Energy history and rollups.
franklinwh_direct_connect_api.energy.has_data ¶
True when the gateway actually had history for this day.
franklinwh_direct_connect_api.energy.period_range ¶
Inclusive date range for a period containing date.
week is the ISO week (Monday-Sunday). month and year are calendar
periods. total walks back a generous window and lets retention truncate
it. Ranges are clipped so we never request future days.
franklinwh_direct_connect_api.energy.Rollup
dataclass
¶
Aggregated energy over a date range, with explicit coverage.
franklinwh_direct_connect_api.energy.aggregate ¶
Sum daily payloads into a :class:Rollup.
franklinwh_direct_connect_api.energy.rollup ¶
rollup(fetch_day: Callable[[str], dict], period: str, date: date, *, today: date | None = None, on_day: Callable[[date, dict], None] | None = None, stop_after_empty: int | None = None) -> Rollup
Fetch and aggregate a period, newest day first.
fetch_day takes a YYYY-MM-DD string and returns the 1303 payload.
stop_after_empty short-circuits once that many consecutive days come back
empty — history is contiguous, so for total this avoids walking a year of
dates the gateway has already discarded.
franklinwh_direct_connect_api.energy.TouBlock
dataclass
¶
One tariff block: a start/end time and its tariff tier.
franklinwh_direct_connect_api.energy.decode_tou ¶
Turn 1407's parallel flag/time arrays into ordered tariff blocks.
The device stores the schedule as <day>Time (block start times) and
<day>Flag (tariff tier per block), with <day>Lv giving the count.
Each block runs until the next block's start, the last wrapping to 24:00.
franklinwh_direct_connect_api.energy.active_tier ¶
Which tariff tier accumulated between two 1301 payloads, if any.
Returns None across a day boundary (every accumulator resets) or when
nothing moved.
franklinwh_direct_connect_api.energy.tier_transitions ¶
Points where the active tier changed, from consecutive 1301 payloads.
samples is an ordered sequence of {"ts": <epoch>, **payload} dicts.
Returns (ts, from_tier, to_tier).
Probing for undocumented cmdTypes¶
Read-only sweep machinery behind franklinwh-direct-connect probe — see
Probing.
franklinwh_direct_connect_api.probe.ProbeResult
dataclass
¶
Outcome of probing one cmdType with one or more payload shapes.
cell_series
property
¶
Labels of numeric series in the reply that look like cell telemetry.
franklinwh_direct_connect_api.probe.default_payloads ¶
The payload matrix: plain read, per-device id, then serial selectors.
Order matters only for stop_on_hit; with --all-shapes every shape is
tried. id shapes come before the serial shapes because that is the
convention the local channel actually uses.
franklinwh_direct_connect_api.probe.expand_payloads ¶
Expand payload templates, substituting %fhpSn% with each serial.
A template with no token is used verbatim. A template containing the token
is emitted once per serial, so {"opt":0,"fhpSn":"%fhpSn%"} becomes one
payload per battery.
franklinwh_direct_connect_api.probe.discover_serials ¶
aPower/FHP serials to use as fhpSn selectors.
Sourced from 1831 battery_modules (devMap[].devSN) and the 1101 login
manifest (FHP_SN / BMS_SN) — the local side already holds exactly the
identifiers the cloud's BMS read wants.
franklinwh_direct_connect_api.probe.gap_codes ¶
Odd request codes in [start, end]; unconfirmed ones by default.
Known codes are derived from catalog.CATALOG rather than hardcoded, so
this cannot go stale as the catalog grows.
franklinwh_direct_connect_api.probe.probe_codes ¶
probe_codes(client: DirectConnectClient, codes: Iterable[int], payloads: Sequence[dict], *, stop_on_hit: bool = True, reconnect: Callable[[], DirectConnectClient] | None = None, on_result: Callable[[ProbeResult], None] | None = None) -> list[ProbeResult]
Probe each code with each payload shape.
stop_on_hit (default) returns as soon as a shape yields data — fastest
when the question is merely "does this code exist?".
Set it to False to try every shape and keep the richest reply. That
is the mode that matters when a code already answers {"opt": 0} with a
thin or placeholder-looking payload: the question is then not whether it
responds but whether a different shape makes it respond with more.
(Mirrors the cloud's own BMS read, which sends type 2 and type 3 and keeps
whichever came back richer.)
reconnect is required for any sweep that may leave the recognised band:
the aGate closes the connection on an out-of-band cmdType rather than
rejecting it, so without reconnecting every subsequent probe fails with a
broken pipe and the whole run is silently worthless. With it, the sweep
rebuilds the session and carries on.
Read-only: every shape here is a read (opt=0 or a selector). No opt=1
write is ever sent.
franklinwh_direct_connect_api.probe.find_cell_series ¶
Labels of series whose values all sit in a plausible cell-voltage band.
A 16-member series of values clustered in 2.0–5.0 V (or 2000–5000 mV) is the signature of per-cell telemetry — the payload shape we are hunting for.
Discovery (LAN scan)¶
franklinwh_direct_connect_api.discover.scan ¶
scan(targets: Iterable[str], *, ports: tuple[int, ...] = (PORT_SENDMQTT, PORT_MODBUS), timeout: float = 0.5, workers: int = 128, probe: bool = True) -> list[HostResult]
Scan targets; return one HostResult per host with an open target port.
Raises KeyboardInterrupt (after cancelling in-flight threads) if the
user hits Ctrl-C so the caller can print a clean exit message.
franklinwh_direct_connect_api.discover.HostResult
dataclass
¶
franklinwh_direct_connect_api.discover.expand_targets ¶
Expand a target spec into host strings.
Accepts a single host/IP, a CIDR (10.0.0.0/24), a hyphen range
(10.0.0.1-10.0.0.50), or a comma-separated mix of the above.
Raises ValueError for tokens that look like bare integers (e.g. 502
instead of an IP address) to catch common confusion between ports and hosts.
franklinwh_direct_connect_api.discover.default_gateway ¶
Best-effort guess of the current default-gateway IP (the hotspot gateway,
when joined to the FranklinWH AP). Uses a UDP socket trick — no traffic is
actually sent. Returns the local /24's .1 as a heuristic.
franklinwh_direct_connect_api.discover.probe_sendmqtt ¶
Confirm a sendMqtt broker by performing the 1101 login and reading 1102. Returns the manifest dataArea on success, else None.
franklinwh_direct_connect_api.discover.probe_modbus ¶
Send a minimal Modbus read and verify a valid MBAP response.
Emulator¶
franklinwh_direct_connect_api.emulator.Emulator ¶
A threaded fake broker. Use as a context manager or call serve_forever().
The emulator synthesises responses from a deterministic site model; it is a development and test aid, not evidence of real device behaviour. See Test without hardware.
Decoding proxy¶
Exported at the top level as proxy_serve and log_frame.
franklinwh_direct_connect_api.proxy.serve ¶
serve(listen_host: str, listen_port: int, target_host: str, target_port: int, *, on_frame: FrameCallback = log_frame, once: bool = False, timeout: float = 10.0, on_listen: Callable[[int], None] | None = None, record: str | None = None) -> None
Relay between a client on listen_* and the aGate on target_*.
Blocks serving connections until interrupted. With once=True it serves a
single connection and returns (used by the tests). listen_port=0 binds an
ephemeral port; on_listen is called with the actual bound port. Pass
record=<path> to also write a pcap of the whole conversation (openable in
Wireshark and re-decodable with iter_pcap_frames).
franklinwh_direct_connect_api.proxy.log_frame ¶
Default on_frame: one line per frame, flagging unknown cmdTypes.