motion.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """Motion sensor parser."""
  2. from __future__ import annotations
  3. def process_wopresence(
  4. data: bytes | None, mfr_data: bytes | None
  5. ) -> dict[str, bool | int]:
  6. """Process WoPresence Sensor services data."""
  7. if data is None and mfr_data is None:
  8. return {}
  9. tested = None
  10. battery = None
  11. led = None
  12. iot = None
  13. sense_distance = None
  14. light_intensity = None
  15. is_light = None
  16. motion_detected = None
  17. if data and len(data) >= 6:
  18. tested = bool(data[1] & 0b10000000)
  19. motion_detected = bool(data[1] & 0b01000000)
  20. battery = data[2] & 0b01111111
  21. led = (data[5] & 0b00100000) >> 5
  22. iot = (data[5] & 0b00010000) >> 4
  23. sense_distance = (data[5] & 0b00001100) >> 2
  24. light_intensity = data[5] & 0b00000011
  25. is_light = bool(data[5] & 0b00000010)
  26. if mfr_data and len(mfr_data) >= 8:
  27. motion_detected = bool(mfr_data[7] & 0b01000000)
  28. is_light = bool(mfr_data[7] & 0b00100000)
  29. return {
  30. "tested": tested,
  31. "motion_detected": motion_detected,
  32. "battery": battery,
  33. "led": led,
  34. "iot": iot,
  35. "sense_distance": sense_distance,
  36. "light_intensity": light_intensity,
  37. "is_light": is_light,
  38. }