hub2.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """Hub2 parser."""
  2. from __future__ import annotations
  3. from typing import Any
  4. def process_wohub2(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]:
  5. """Process woHub2 sensor manufacturer data."""
  6. temp_data = None
  7. if mfr_data:
  8. status = mfr_data[12]
  9. temp_data = mfr_data[13:16]
  10. if not temp_data:
  11. return {}
  12. _temp_sign = 1 if temp_data[1] & 0b10000000 else -1
  13. _temp_c = _temp_sign * (
  14. (temp_data[1] & 0b01111111) + ((temp_data[0] & 0b00001111) / 10)
  15. )
  16. _temp_f = (_temp_c * 9 / 5) + 32
  17. _temp_f = (_temp_f * 10) / 10
  18. humidity = temp_data[2] & 0b01111111
  19. light_level = status & 0b11111
  20. if _temp_c == 0 and humidity == 0:
  21. return {}
  22. _wohub2_data = {
  23. # Data should be flat, but we keep the original structure for now
  24. "temp": {"c": _temp_c, "f": _temp_f},
  25. "temperature": _temp_c,
  26. "fahrenheit": bool(temp_data[2] & 0b10000000),
  27. "humidity": humidity,
  28. "lightLevel": light_level,
  29. }
  30. return _wohub2_data