blind_tilt.py 827 B

123456789101112131415161718192021222324252627
  1. """Library to handle connection with Switchbot."""
  2. from __future__ import annotations
  3. def process_woblindtilt(
  4. data: bytes | None, mfr_data: bytes | None, reverse: bool = False
  5. ) -> dict[str, bool | int]:
  6. """Process woBlindTilt services data."""
  7. if mfr_data is None:
  8. return {}
  9. device_data = mfr_data[6:]
  10. _tilt = max(min(device_data[2] & 0b01111111, 100), 0)
  11. _in_motion = bool(device_data[2] & 0b10000000)
  12. _light_level = (device_data[1] >> 4) & 0b00001111
  13. _calibrated = bool(device_data[1] & 0b00000001)
  14. return {
  15. "calibration": _calibrated,
  16. "battery": data[2] & 0b01111111 if data else None,
  17. "inMotion": _in_motion,
  18. "tilt": (100 - _tilt) if reverse else _tilt,
  19. "lightLevel": _light_level,
  20. "sequence_number": device_data[0],
  21. }