leak.py 918 B

1234567891011121314151617181920212223242526272829
  1. """Leak detector adv parser."""
  2. def process_leak(data: bytes | None, mfr_data: bytes | None) -> dict[str, bool | int]:
  3. """Process SwitchBot Water Leak Detector advertisement data."""
  4. if data is None or len(data) < 3 or mfr_data is None or len(mfr_data) < 2:
  5. return {}
  6. water_leak_detected = None
  7. device_tampered = None
  8. battery_level = None
  9. low_battery = None
  10. # Byte 1: Event Flags
  11. event_flags = mfr_data[8]
  12. water_leak_detected = bool(event_flags & 0b00000001) # Bit 0
  13. device_tampered = bool(event_flags & 0b00000010) # Bit 1
  14. # Byte 2: Battery Info
  15. battery_info = mfr_data[7]
  16. battery_level = battery_info & 0b01111111 # Bits 0-6
  17. low_battery = bool(battery_info & 0b10000000) # Bit 7
  18. return {
  19. "leak": water_leak_detected,
  20. "tampered": device_tampered,
  21. "battery": battery_level,
  22. "low_battery": low_battery,
  23. }