import asyncio import struct import bleak from bleak import BleakClient CONF_MAC = "ea:00:00:02:74:ec" def parse_data(sender, data: bytearray): probe_channels = [0] # Hardcoded probe channels # Lets decode the data if len(data) == 9 and data[0] == 0x00 and data[1] == 0x00: channel = data[2] d = data[3] * 0.03125 if d >= 2.0: battery = 100 elif d >= 1.7: battery = 51 elif d >= 1.5: battery = 26 else: battery = 20 # Default if d < 1.5 temp_bytes = data[4:6] temp = (struct.unpack(">H", temp_bytes)[0] * 0.0625) - 50.0625 rssi = data[8] print(f"Probe Data: Channel={channel}, d={d:.2f}, Temp={temp:.2f}, RSSI={rssi}, Battery={battery}") return {"type": "probe", "channel": channel, "d": d, "temp": temp, "rssi": rssi} elif len(data) == 8 and data[0] == 0x00 and data[1] == 0x01: voltage_bytes = data[2:4] voltage = struct.unpack(">H", voltage_bytes)[0] / 1000.0 status = data[4:] if voltage > 3.87: relay_battery = 100 elif voltage >= 3.7: relay_battery = 74 elif voltage >= 3.6: relay_battery = 49 else: relay_battery = 0 probe_statuses = {} for channel in probe_channels: if len(data) > 4: #check to avoid index out of range errors status_byte = data[4] # Directly access the 5th byte (index 4) probe_status_string = "Docked/Charging" if status_byte == 0 else "Undocked/Not Charging" probe_statuses[channel] = {"raw": status_byte, "status": probe_status_string} else: print("data length too short for probe status") print(f"Relay Data: Voltage={voltage:.3f}, Battery={relay_battery}, Probe Statuses={probe_statuses}") return {"type": "relay", "voltage": voltage, "status": status, "battery": relay_battery, "probe_statuses": probe_statuses} print(f"{sender}: {data}") async def main(address): print("Connecting...") async with BleakClient(address) as client: await client.start_notify("0000ff01-0000-1000-8000-00805f9b34fb", parse_data) while True: await asyncio.sleep(5) asyncio.run(main(CONF_MAC))