relay_switch.py 1.9 KB

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