curtain.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. def process_wocurtain(
  4. data: bytes | None, mfr_data: bytes | None, reverse: bool = True
  5. ) -> dict[str, bool | int]:
  6. """Process woCurtain/Curtain services data."""
  7. if mfr_data and len(mfr_data) >= 13: # Curtain 3
  8. device_data = mfr_data[8:11]
  9. battery_data = mfr_data[12]
  10. elif mfr_data and len(mfr_data) >= 11:
  11. device_data = mfr_data[8:11]
  12. battery_data = data[2] if data else None
  13. elif data:
  14. device_data = data[3:6]
  15. battery_data = data[2]
  16. else:
  17. return {}
  18. _position = max(min(device_data[0] & 0b01111111, 100), 0)
  19. _in_motion = bool(device_data[0] & 0b10000000)
  20. _light_level = (device_data[1] >> 4) & 0b00001111
  21. _device_chain = device_data[1] & 0b00000111
  22. return {
  23. "calibration": bool(data[1] & 0b01000000) if data else None,
  24. "battery": battery_data & 0b01111111 if battery_data is not None else None,
  25. "inMotion": _in_motion,
  26. "position": (100 - _position) if reverse else _position,
  27. "lightLevel": _light_level,
  28. "deviceChain": _device_chain,
  29. }