From b3e6b7e1b86b1258798aea10d8e23545376383c3 Mon Sep 17 00:00:00 2001 From: DjeAvd Date: Thu, 21 May 2026 16:56:24 +0200 Subject: [PATCH] feat(gateway): add battery level support (key 0x05) - Add KEY_BATTERY constant (0x05) - Update EXPECTED_PAYLOAD_SIZE from 12 to 14 bytes - Decode battery level from payload - Publish battery field in MQTT JSON payload --- gateway/gateway.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gateway/gateway.py b/gateway/gateway.py index 5258b95..4280de2 100644 --- a/gateway/gateway.py +++ b/gateway/gateway.py @@ -23,13 +23,14 @@ class Gateway: KEY_HUMIDITY = 0x02 KEY_TEMP = 0x03 KEY_CO2 = 0x04 + KEY_BATTERY = 0x05 # Battery level (1 byte, integer %) # Sentinel value indicating sensor failure or not ready INVALID_VALUE = 0xFFFFFFFF # Expected payload size in bytes: - # 4 keys (1B each) + window(1B) + humidity(1B) + temp(2B) + co2(4B) = 12 bytes - EXPECTED_PAYLOAD_SIZE = 12 + # 5 keys (1B each) + window(1B) + humidity(1B) + temp(2B) + co2(4B) + battery(1B) = 14 bytes + EXPECTED_PAYLOAD_SIZE = 14 def __init__(self, config: dict): self.gateway_id = config["gateway_id"] @@ -88,6 +89,7 @@ class Gateway: 0x02 : humidity (1 byte, integer %) 0x03 : temperature (2 bytes big-endian, integer / 10 = degrees C) 0x04 : CO2 ppm (4 bytes big-endian, integer) + 0x05 : battery level (1 byte, integer %) Values equal to 0xFFFFFFFF indicate sensor failure or not ready and are discarded. @@ -117,6 +119,9 @@ class Gateway: else: log.debug(f"CO2 sensor not ready — discarding value 0xFFFFFFFF") i += 4 + elif key == self.KEY_BATTERY and i < len(data): + result["battery"] = data[i] + i += 1 else: # Unknown key — likely a non-Thingy device, ignore silently log.debug(f"Unknown key 0x{key:02x} at offset {i-1}") @@ -139,6 +144,8 @@ class Gateway: payload["co2_ppm"] = data["co2_ppm"] if "window_open" in data: payload["window_open"] = data["window_open"] + if "battery" in data: + payload["battery"] = data["battery"] self.mqttc.publish(topic, json.dumps(payload)) log.info(f"Published to {topic} : {payload}")