hub2.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """Hub2 parser."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from ..const.hub2 import LIGHT_INTENSITY_MAP
  5. from ..helpers import celsius_to_fahrenheit
  6. def process_wohub2(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]:
  7. """Process woHub2 sensor manufacturer data."""
  8. temp_data = None
  9. if mfr_data:
  10. status = mfr_data[12]
  11. temp_data = mfr_data[13:16]
  12. if not temp_data:
  13. return {}
  14. _temp_sign = 1 if temp_data[1] & 0b10000000 else -1
  15. _temp_c = _temp_sign * (
  16. (temp_data[1] & 0b01111111) + ((temp_data[0] & 0b00001111) / 10)
  17. )
  18. _temp_f = celsius_to_fahrenheit(_temp_c)
  19. _temp_f = (_temp_f * 10) / 10
  20. humidity = temp_data[2] & 0b01111111
  21. light_level = status & 0b11111
  22. if _temp_c == 0 and humidity == 0:
  23. return {}
  24. return {
  25. # Data should be flat, but we keep the original structure for now
  26. "temp": {"c": _temp_c, "f": _temp_f},
  27. "temperature": _temp_c,
  28. "fahrenheit": bool(temp_data[2] & 0b10000000),
  29. "humidity": humidity,
  30. "lightLevel": light_level,
  31. "illuminance": calculate_light_intensity(light_level),
  32. }
  33. def calculate_light_intensity(light_level: int) -> int:
  34. """
  35. Convert Hub 2 light level (1-21) to actual light intensity value
  36. Args:
  37. light_level: Integer from 1-21
  38. Returns:
  39. Corresponding light intensity value or 0 if invalid input
  40. """
  41. if not light_level:
  42. return 0
  43. return LIGHT_INTENSITY_MAP.get(max(0, min(light_level, 22)), 0)