contact.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  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. contact_timeout = data[3] & 0b00000100 == 0b00000100 if data else False
  12. if mfr_data and len(mfr_data) >= 13:
  13. motion_detected = bool(mfr_data[7] & 0b10000000)
  14. contact_open = bool(mfr_data[7] & 0b00010000)
  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 = data[3] & 0b00000010 == 0b00000010
  20. button_count = data[8] & 0b00001111
  21. is_light = bool(data[3] & 0b00000001)
  22. return {
  23. "tested": tested,
  24. "motion_detected": motion_detected,
  25. "battery": battery,
  26. "contact_open": contact_open or contact_timeout, # timeout still means its open
  27. "contact_timeout": contact_timeout,
  28. "is_light": is_light,
  29. "button_count": button_count,
  30. }