contact.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. has_full_mfr = mfr_data is not None and len(mfr_data) >= 13
  10. has_full_data = data is not None and len(data) >= 9
  11. if not has_full_mfr and not has_full_data:
  12. return {}
  13. battery = data[2] & 0b01111111 if data and len(data) >= 3 else None
  14. tested = bool(data[1] & 0b10000000) if data and len(data) >= 2 else None
  15. if has_full_mfr:
  16. motion_detected = bool(mfr_data[7] & 0b10000000)
  17. contact_open = bool(mfr_data[7] & 0b00010000)
  18. contact_timeout = bool(mfr_data[7] & 0b00100000)
  19. button_count = mfr_data[12] & 0b00001111
  20. is_light = bool(mfr_data[7] & 0b01000000)
  21. else:
  22. motion_detected = bool(data[1] & 0b01000000)
  23. contact_open = bool(data[3] & 0b00000010)
  24. contact_timeout = bool(data[3] & 0b00000100)
  25. button_count = data[8] & 0b00001111
  26. is_light = bool(data[3] & 0b00000001)
  27. return {
  28. "tested": tested,
  29. "motion_detected": motion_detected,
  30. "battery": battery,
  31. "contact_open": contact_open or contact_timeout, # timeout still means its open
  32. "contact_timeout": contact_timeout,
  33. "is_light": is_light,
  34. "button_count": button_count,
  35. }