diff --git a/ha/custom_components/li3_battery/parser.py b/ha/custom_components/li3_battery/parser.py index 3510928..2ae8005 100644 --- a/ha/custom_components/li3_battery/parser.py +++ b/ha/custom_components/li3_battery/parser.py @@ -31,10 +31,19 @@ STATUS_FLAGS = [ ] -def decode_status(hex_code): +def status_value(status): + """Parse the raw hex status field to an int; None if unparseable. + Single source of truth for both the "is this OK" gate and the flag + decode, so the two can't disagree on e.g. "0" vs "000000".""" try: - value = int(hex_code, 16) + return int(status, 16) except (ValueError, TypeError): + return None + + +def decode_status(hex_code): + value = status_value(hex_code) + if value is None: return hex_code active = [ label for i, label in enumerate(STATUS_FLAGS) @@ -61,7 +70,7 @@ def parse_line(line): "cell4": round(int(parts[4]) * 0.01, 2), "bms_temp_f": int(parts[5]), "batt_temp_f": int(parts[6]), - "current_a": int(parts[7]), + "current_a": round(int(parts[7]) * 0.1, 1), # reported in 0.1A units "soc_pct": int(parts[8]), "status": parts[9] if len(parts) > 9 else "?", } @@ -113,7 +122,11 @@ def build_state_payload(cs_reading, trace_fields): # ("&" line) fields come from a separate message and are published # regardless. status = cs_reading["status"] - if status != "000000": + # "OK" == integer zero in ANY representation. Gate on the parsed int (not + # the raw string) so "0"/"0000"/"000000" all agree with decode_status, and + # an unparseable status (e.g. "?") is treated as unreliable and suppressed. + value = status_value(status) + if value is None or value != 0: payload = {"status": status} else: payload = {