1
0

meter.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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
  34. def process_wosensorth_c(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]:
  35. """Process woSensorTH/Temp sensor services data with CO2."""
  36. temp_data = None
  37. battery = None
  38. if mfr_data:
  39. temp_data = mfr_data[8:11]
  40. if data:
  41. if not temp_data:
  42. temp_data = data[3:6]
  43. battery = data[2] & 0b01111111
  44. if not temp_data:
  45. return {}
  46. _temp_sign = 1 if temp_data[1] & 0b10000000 else -1
  47. _temp_c = _temp_sign * (
  48. (temp_data[1] & 0b01111111) + ((temp_data[0] & 0b00001111) / 10)
  49. )
  50. _temp_f = (_temp_c * 9 / 5) + 32
  51. _temp_f = (_temp_f * 10) / 10
  52. humidity = temp_data[2] & 0b01111111
  53. if _temp_c == 0 and humidity == 0 and battery == 0:
  54. return {}
  55. _wosensorth_data = {
  56. # Data should be flat, but we keep the original structure for now
  57. "temp": {"c": _temp_c, "f": _temp_f},
  58. "temperature": _temp_c,
  59. "fahrenheit": bool(temp_data[2] & 0b10000000),
  60. "humidity": humidity,
  61. "battery": battery,
  62. }
  63. return _wosensorth_data