86 lines
3.4 KiB
Python
86 lines
3.4 KiB
Python
"""Read-only JK02_32S settings; no numeric setting encoder exists here.
|
|
|
|
Offsets cross-checked with pinned ESPHome/batmon sources in the capability doc.
|
|
The entire frame remains private RAM for command readback comparison.
|
|
"""
|
|
|
|
import hashlib
|
|
|
|
from .protocol import HEADER
|
|
|
|
# name, offset, divisor, signed. Only the common 11.x fields are admitted.
|
|
FIELDS = (
|
|
("sleep_voltage_v", 6, 1000, False),
|
|
("cell_uvp_v", 10, 1000, False),
|
|
("cell_uvpr_v", 14, 1000, False),
|
|
("cell_ovp_v", 18, 1000, False),
|
|
("cell_ovpr_v", 22, 1000, False),
|
|
("balance_trigger_v", 26, 1000, False),
|
|
("soc100_voltage_v", 30, 1000, False),
|
|
("soc0_voltage_v", 34, 1000, False),
|
|
("requested_charge_voltage_v", 38, 1000, False),
|
|
("requested_float_voltage_v", 42, 1000, False),
|
|
("poweroff_voltage_v", 46, 1000, False),
|
|
("max_charge_current_a", 50, 1000, False),
|
|
("charge_ocp_delay_s", 54, 1, False),
|
|
("charge_ocp_recovery_s", 58, 1, False),
|
|
("max_discharge_current_a", 62, 1000, False),
|
|
("discharge_ocp_delay_s", 66, 1, False),
|
|
("discharge_ocp_recovery_s", 70, 1, False),
|
|
("short_circuit_recovery_s", 74, 1, False),
|
|
("max_balance_current_a", 78, 1000, False),
|
|
("charge_overtemp_c", 82, 10, True),
|
|
("charge_overtemp_recovery_c", 86, 10, True),
|
|
("discharge_overtemp_c", 90, 10, True),
|
|
("discharge_overtemp_recovery_c", 94, 10, True),
|
|
("charge_undertemp_c", 98, 10, True),
|
|
("charge_undertemp_recovery_c", 102, 10, True),
|
|
("mos_overtemp_c", 106, 10, True),
|
|
("mos_overtemp_recovery_c", 110, 10, True),
|
|
("nominal_capacity_ah", 130, 1000, False),
|
|
("short_circuit_delay_us", 134, 1, False),
|
|
("balance_start_voltage_v", 138, 1000, False),
|
|
)
|
|
SWITCHES = {"charging": (0x1D, 118), "discharging": (0x1E, 122), "balancing": (0x1F, 126)}
|
|
|
|
|
|
def decode_settings(frame):
|
|
if len(frame) != 300 or frame[:5] != HEADER + b"\x01" or sum(frame[:-1]) & 255 != frame[-1]:
|
|
raise ValueError("Invalid settings frame")
|
|
values = {
|
|
name: int.from_bytes(frame[offset : offset + 4], "little", signed=signed) / scale
|
|
for name, offset, scale, signed in FIELDS
|
|
}
|
|
values["cell_count"] = int.from_bytes(frame[114:118], "little")
|
|
switches = {
|
|
key: int.from_bytes(frame[offset : offset + 4], "little")
|
|
for key, (_, offset) in SWITCHES.items()
|
|
}
|
|
if not 1 <= values["cell_count"] <= 32 or any(v not in (0, 1) for v in switches.values()):
|
|
raise ValueError("Unsupported settings layout")
|
|
if not (
|
|
0 < values["cell_uvp_v"] < values["cell_ovp_v"] < 5
|
|
and 0 < values["nominal_capacity_ah"] < 100000
|
|
):
|
|
raise ValueError("Inconsistent settings layout")
|
|
return {
|
|
"values": values,
|
|
"switches": {key: bool(v) for key, v in switches.items()},
|
|
"fingerprint": hashlib.sha256(frame[6:299]).hexdigest(),
|
|
}
|
|
|
|
|
|
def switch_command(name, enabled):
|
|
if name not in SWITCHES or type(enabled) is not bool:
|
|
raise ValueError("Only three boolean operating switches are supported")
|
|
data = bytes.fromhex("aa5590eb") + bytes([SWITCHES[name][0], 4, int(enabled)]) + bytes(12)
|
|
return data + bytes([sum(data) & 255])
|
|
|
|
|
|
def unchanged_except(before, after, name, enabled):
|
|
"""Compare every settings byte, excluding counter/CRC and the requested bit."""
|
|
expected = bytearray(before[6:299])
|
|
offset = SWITCHES[name][1] - 6
|
|
expected[offset : offset + 4] = int(enabled).to_bytes(4, "little")
|
|
return bytes(expected) == after[6:299]
|