1
0

hub3.py 1.8 KB

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