contact.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """Contact sensor parser."""
  2. from __future__ import annotations
  3. def process_wocontact(
  4. data: bytes | None, mfr_data: bytes | None
  5. ) -> dict[str, bool | int]:
  6. """Process woContact Sensor services data."""
  7. if data is None and mfr_data is None:
  8. return {}
  9. battery = data[2] & 0b01111111 if data else None
  10. tested = bool(data[1] & 0b10000000) if data else None
  11. if mfr_data and len(mfr_data) >= 13:
  12. motion_detected = bool(mfr_data[7] & 0b10000000)
  13. contact_open = bool(mfr_data[7] & 0b00010000)
  14. contact_timeout = bool(mfr_data[7] & 0b00100000)
  15. button_count = mfr_data[12] & 0b00001111
  16. is_light = bool(mfr_data[7] & 0b01000000)
  17. else:
  18. motion_detected = bool(data[1] & 0b01000000)
  19. contact_open = bool(data[3] & 0b00000010)
  20. contact_timeout = bool(data[3] & 0b00000100)
  21. button_count = data[8] & 0b00001111
  22. is_light = bool(data[3] & 0b00000001)
  23. return {
  24. "tested": tested,
  25. "motion_detected": motion_detected,
  26. "battery": battery,
  27. "contact_open": contact_open or contact_timeout, # timeout still means its open
  28. "contact_timeout": contact_timeout,
  29. "is_light": is_light,
  30. "button_count": button_count,
  31. }