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
This commit is contained in:
DjeAvd
2026-05-21 16:56:24 +02:00
committed by Klagarge
parent 9ffc4f77ba
commit b3e6b7e1b8

View File

@@ -23,13 +23,14 @@ class Gateway:
KEY_HUMIDITY = 0x02 KEY_HUMIDITY = 0x02
KEY_TEMP = 0x03 KEY_TEMP = 0x03
KEY_CO2 = 0x04 KEY_CO2 = 0x04
KEY_BATTERY = 0x05 # Battery level (1 byte, integer %)
# Sentinel value indicating sensor failure or not ready # Sentinel value indicating sensor failure or not ready
INVALID_VALUE = 0xFFFFFFFF INVALID_VALUE = 0xFFFFFFFF
# Expected payload size in bytes: # Expected payload size in bytes:
# 4 keys (1B each) + window(1B) + humidity(1B) + temp(2B) + co2(4B) = 12 bytes # 5 keys (1B each) + window(1B) + humidity(1B) + temp(2B) + co2(4B) + battery(1B) = 14 bytes
EXPECTED_PAYLOAD_SIZE = 12 EXPECTED_PAYLOAD_SIZE = 14
def __init__(self, config: dict): def __init__(self, config: dict):
self.gateway_id = config["gateway_id"] self.gateway_id = config["gateway_id"]
@@ -88,6 +89,7 @@ class Gateway:
0x02 : humidity (1 byte, integer %) 0x02 : humidity (1 byte, integer %)
0x03 : temperature (2 bytes big-endian, integer / 10 = degrees C) 0x03 : temperature (2 bytes big-endian, integer / 10 = degrees C)
0x04 : CO2 ppm (4 bytes big-endian, integer) 0x04 : CO2 ppm (4 bytes big-endian, integer)
0x05 : battery level (1 byte, integer %)
Values equal to 0xFFFFFFFF indicate sensor failure or not ready Values equal to 0xFFFFFFFF indicate sensor failure or not ready
and are discarded. and are discarded.
@@ -117,6 +119,9 @@ class Gateway:
else: else:
log.debug(f"CO2 sensor not ready — discarding value 0xFFFFFFFF") log.debug(f"CO2 sensor not ready — discarding value 0xFFFFFFFF")
i += 4 i += 4
elif key == self.KEY_BATTERY and i < len(data):
result["battery"] = data[i]
i += 1
else: else:
# Unknown key — likely a non-Thingy device, ignore silently # Unknown key — likely a non-Thingy device, ignore silently
log.debug(f"Unknown key 0x{key:02x} at offset {i-1}") log.debug(f"Unknown key 0x{key:02x} at offset {i-1}")
@@ -139,6 +144,8 @@ class Gateway:
payload["co2_ppm"] = data["co2_ppm"] payload["co2_ppm"] = data["co2_ppm"]
if "window_open" in data: if "window_open" in data:
payload["window_open"] = data["window_open"] payload["window_open"] = data["window_open"]
if "battery" in data:
payload["battery"] = data["battery"]
self.mqttc.publish(topic, json.dumps(payload)) self.mqttc.publish(topic, json.dumps(payload))
log.info(f"Published to {topic} : {payload}") log.info(f"Published to {topic} : {payload}")