relay_switch.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Relay Switch adv parser."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from ..helpers import parse_power_data
  5. def process_relay_switch_common_data(
  6. data: bytes | None, mfr_data: bytes | None
  7. ) -> dict[str, Any]:
  8. """Process relay switch 1 and 1PM common data."""
  9. if mfr_data is None:
  10. return {}
  11. return {
  12. "switchMode": True, # for compatibility, useless
  13. "sequence_number": mfr_data[6],
  14. "isOn": bool(mfr_data[7] & 0b10000000),
  15. }
  16. def process_relay_switch_1pm(
  17. data: bytes | None, mfr_data: bytes | None
  18. ) -> dict[str, Any]:
  19. """Process Relay Switch 1PM services data."""
  20. if mfr_data is None:
  21. return {}
  22. common_data = process_relay_switch_common_data(data, mfr_data)
  23. common_data["power"] = parse_power_data(mfr_data, 10)
  24. return common_data
  25. def process_garage_door_opener(
  26. data: bytes | None, mfr_data: bytes | None
  27. ) -> dict[str, Any]:
  28. """Process garage door opener services data."""
  29. if mfr_data is None:
  30. return {}
  31. common_data = process_relay_switch_common_data(data, mfr_data)
  32. common_data["door_open"] = not bool(mfr_data[7] & 0b00100000)
  33. return common_data
  34. def process_relay_switch_2pm(
  35. data: bytes | None, mfr_data: bytes | None
  36. ) -> dict[int, dict[str, Any]]:
  37. """Process Relay Switch 2PM services data."""
  38. if mfr_data is None:
  39. return {}
  40. return {
  41. 1: {
  42. **process_relay_switch_common_data(data, mfr_data),
  43. "power": parse_power_data(mfr_data, 10),
  44. },
  45. 2: {
  46. "switchMode": True, # for compatibility, useless
  47. "sequence_number": mfr_data[6],
  48. "isOn": bool(mfr_data[7] & 0b01000000),
  49. "power": parse_power_data(mfr_data, 12),
  50. },
  51. "sequence_number": mfr_data[6],
  52. }