hub3.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Air Purifier adv parser."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from ..const.hub3 import LIGHT_INTENSITY_MAP
  5. def process_hub3(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]:
  6. """Process hub3 sensor manufacturer data."""
  7. if mfr_data is None:
  8. return {}
  9. device_data = mfr_data[6:]
  10. seq_num = device_data[0]
  11. network_state = (device_data[6] & 0b11000000) >> 6
  12. sensor_inserted = not bool(device_data[6] & 0b00100000)
  13. light_level = device_data[6] & 0b00001111
  14. illuminance = calculate_light_intensity(light_level)
  15. temperature_alarm = bool(device_data[7] & 0b11000000)
  16. humidity_alarm = bool(device_data[7] & 0b00110000)
  17. temp_data = device_data[7:10]
  18. _temp_sign = 1 if temp_data[1] & 0b10000000 else -1
  19. _temp_c = _temp_sign * (
  20. (temp_data[1] & 0b01111111) + ((temp_data[0] & 0b00001111) / 10)
  21. )
  22. _temp_f = round(((_temp_c * 9 / 5) + 32), 1)
  23. humidity = temp_data[2] & 0b01111111
  24. motion_detected = bool(device_data[10] & 0b10000000)
  25. return {
  26. "sequence_number": seq_num,
  27. "network_state": network_state,
  28. "sensor_inserted": sensor_inserted,
  29. "lightLevel": light_level,
  30. "illuminance": illuminance,
  31. "temperature_alarm": temperature_alarm,
  32. "humidity_alarm": humidity_alarm,
  33. "temp": {"c": _temp_c, "f": _temp_f},
  34. "temperature": _temp_c,
  35. "humidity": humidity,
  36. "motion_detected": motion_detected,
  37. }
  38. def calculate_light_intensity(light_level: int) -> int:
  39. """
  40. Convert Hub 3 light level (1-10) to actual light intensity value
  41. Args:
  42. light_level: Integer from 1-10
  43. Returns:
  44. Corresponding light intensity value or 0 if invalid input
  45. """
  46. return LIGHT_INTENSITY_MAP.get(max(0, min(light_level, 10)), 0)