meter.py 1.1 KB

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