meter.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Meter parser."""
  2. from __future__ import annotations
  3. import struct
  4. from typing import Any
  5. from ..helpers import celsius_to_fahrenheit
  6. CO2_UNPACK = struct.Struct(">H").unpack_from
  7. def process_wosensorth(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]:
  8. """Process woSensorTH/Temp sensor services data."""
  9. temp_data: bytes | None = None
  10. battery: bytes | None = None
  11. if mfr_data:
  12. temp_data = mfr_data[8:11]
  13. if data:
  14. if not temp_data:
  15. temp_data = data[3:6]
  16. battery = data[2] & 0b01111111
  17. if not temp_data:
  18. return {}
  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 = celsius_to_fahrenheit(_temp_c)
  24. _temp_f = (_temp_f * 10) / 10
  25. humidity = temp_data[2] & 0b01111111
  26. if _temp_c == 0 and humidity == 0 and battery == 0:
  27. return {}
  28. return {
  29. # Data should be flat, but we keep the original structure for now
  30. "temp": {"c": _temp_c, "f": _temp_f},
  31. "temperature": _temp_c,
  32. "fahrenheit": bool(temp_data[2] & 0b10000000),
  33. "humidity": humidity,
  34. "battery": battery,
  35. }
  36. def process_wosensorth_c(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]:
  37. """Process woSensorTH/Temp sensor services data with CO2."""
  38. _wosensorth_data = process_wosensorth(data, mfr_data)
  39. if _wosensorth_data and mfr_data and len(mfr_data) >= 15:
  40. co2_data = mfr_data[13:15]
  41. _wosensorth_data["co2"] = CO2_UNPACK(co2_data)[0]
  42. return _wosensorth_data