ams_humidity.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """Shared reading of an AMS unit's humidity.
  2. Bambu sends two humidity fields and they are not the same quantity.
  3. ``humidity_raw`` is relative humidity in percent. ``humidity`` is a 1-5 drop
  4. index, and it runs the other way: OpenBambuAPI's push_info sample carries both
  5. in one line -- ``ams0 temp:18.4;humidity:30%;humidity_idx:4`` -- so a high index
  6. means dry where a high percentage means wet.
  7. Falling back from one to the other therefore does not degrade, it inverts.
  8. Index 2 rendered as "2%" reads as the driest a unit can be while the unit is in
  9. fact the second-wettest of the five steps, and no index can ever exceed a
  10. percentage threshold, so the humidity alarm and auto-drying silently never fire
  11. for such a unit (#3140). A unit that reports no percentage has no percentage:
  12. this returns ``None``, which every caller already treats as "no reading" -- the
  13. card hides the indicator, the alarm and auto-drying skip the unit, and the
  14. history chart leaves a gap.
  15. Kept as a leaf module like ``ams_drying``: nothing here imports from the app.
  16. """
  17. from collections.abc import Mapping
  18. from typing import Any
  19. def ams_humidity_percent(ams_data: Any) -> float | None:
  20. """Relative humidity in percent for one AMS unit, or ``None``.
  21. ``None`` covers every case where the unit did not report a usable
  22. percentage, including the units that send only the 1-5 index -- which is
  23. deliberately never converted. See the module docstring.
  24. """
  25. if not isinstance(ams_data, Mapping):
  26. return None
  27. raw = ams_data.get("humidity_raw")
  28. if raw is None:
  29. return None
  30. try:
  31. return float(raw)
  32. except (TypeError, ValueError):
  33. return None # Unparseable reading — not a licence to use the index