Read the container level and dose schedule of dosing pumps #1

Merged
r3pek merged 2 commits from gulaschcowboy/dose-container into main 2026-11-12 08:41:36 +00:00 AGit
Contributor

Library side of r3pek/ha-mobius#5, as requested in the review there: the dosing pump (DoseV1, Versa) protocol code moves from the integration into python-mobius.

  • FullPollResult.dose (DoseInfo): remaining volume, container size, dispensed today, container monitor and sensor type, dosing halt state, and the daily target and expected volume from the dose schedule.
  • The dose attributes are part of the regular poll batch. Container size, monitor type and sensor type are in STATIC_POLL_ATTRIBUTES, so they're only read at the static refresh interval.
  • The dose schedule goes through the configuration path and is cached in the device's ConfigurationCache by its checksum, like the other schedules.
  • get_dose_info(), set_container_volume() (also resets the remaining volume, like the app) and refill_container().
  • Tests (tests/test_dose.py), a CHANGELOG entry, and README/docs updates (DoseV1 is no longer listed as not implemented).

The 7 BLE connection tests (test_connection_cleanup, test_serial_identity, test_write_size) that fail locally also fail on main here, so they're unrelated.

Library side of r3pek/ha-mobius#5, as requested in the review there: the dosing pump (DoseV1, Versa) protocol code moves from the integration into python-mobius. - `FullPollResult.dose` (`DoseInfo`): remaining volume, container size, dispensed today, container monitor and sensor type, dosing halt state, and the daily target and expected volume from the dose schedule. - The dose attributes are part of the regular poll batch. Container size, monitor type and sensor type are in `STATIC_POLL_ATTRIBUTES`, so they're only read at the static refresh interval. - The dose schedule goes through the configuration path and is cached in the device's `ConfigurationCache` by its checksum, like the other schedules. - `get_dose_info()`, `set_container_volume()` (also resets the remaining volume, like the app) and `refill_container()`. - Tests (`tests/test_dose.py`), a CHANGELOG entry, and README/docs updates (DoseV1 is no longer listed as not implemented). The 7 BLE connection tests (`test_connection_cleanup`, `test_serial_identity`, `test_write_size`) that fail locally also fail on `main` here, so they're unrelated.
DoseV1 pumps (Versa) get FullPollResult.dose (DoseInfo): remaining volume, container size, dispensed today, monitor/sensor type, halt state, and the daily target and expected volume from the dose schedule. The schedule uses the checksum cache, the container size, monitor and sensor type are static attributes. Adds get_dose_info(), set_container_volume() (resets the remaining volume like the app) and refill_container().
Owner

Thanks for moving this into the library, it fits in nicely. A few things before I merge:

  1. Reuse the shared schedule point header. DoseSchedulePoint.parse re-parses the start time and flags itself instead of using _PointBase._parse_header, which the light and pump points use. Could it subclass _PointBase like they do, with time_minutes/flags fields and the decoded dose primitive? That keeps the three point types consistent and gives a dose schedule writer something to build on later.

  2. Pass the dose attributes by name. _decode_dose_from_batch relies on _DOSE_REQUESTS listing the attributes in the same order as decode_dose_info's arguments. Reordering the list, or adding an attribute in the middle, would silently swap fields. Passing them by name (e.g. current_volume_raw=first(C2Attribute.ContainerCurrentVolume), ...) avoids that.

  3. Attribute reference. Please add the new attributes to documentation/14-attribute-reference.md: DailyDosage (2301), DoseSensorType (2305), ContainerMonitor (2310), ContainerVolume (2312), ContainerCurrentVolume (2313) and DoseHaltState (2322), with their encodings.

  4. Booleans pass the volume check, and the helper's name. _dose_volume_bytes uses isinstance(volume_ul, int), and bool is a subclass of int, so set_container_volume(True) writes a 1 µl container instead of raising. Something like isinstance(volume_ul, bool) or not isinstance(volume_ul, int) fixes it. The minimum=0 default can go too, since both callers pass 1. Could you also rename it to _container_volume_to_bytes? It only packs container volumes, and "dose" in the name suggests it sets an amount to dispense.

  5. Require exactly one dose primitive per schedule point. DoseSchedulePoint.parse accepts any length of 3 + n × 9 bytes but only decodes the first primitive. A point should always be 12 bytes, so anything else should be rejected (len(data) != 3 + DOSE_PRIMITIVE_SIZE) rather than half-read. Otherwise an unexpected format would silently produce a wrong daily target instead of no target.

Once these are in I'll merge and release, so the ha-mobius PR can go in after it.

Thanks for moving this into the library, it fits in nicely. A few things before I merge: 1. **Reuse the shared schedule point header.** `DoseSchedulePoint.parse` re-parses the start time and flags itself instead of using `_PointBase._parse_header`, which the light and pump points use. Could it subclass `_PointBase` like they do, with `time_minutes`/`flags` fields and the decoded dose primitive? That keeps the three point types consistent and gives a dose schedule writer something to build on later. 2. **Pass the dose attributes by name.** `_decode_dose_from_batch` relies on `_DOSE_REQUESTS` listing the attributes in the same order as `decode_dose_info`'s arguments. Reordering the list, or adding an attribute in the middle, would silently swap fields. Passing them by name (e.g. `current_volume_raw=first(C2Attribute.ContainerCurrentVolume), ...`) avoids that. 3. **Attribute reference.** Please add the new attributes to `documentation/14-attribute-reference.md`: `DailyDosage` (2301), `DoseSensorType` (2305), `ContainerMonitor` (2310), `ContainerVolume` (2312), `ContainerCurrentVolume` (2313) and `DoseHaltState` (2322), with their encodings. 4. **Booleans pass the volume check, and the helper's name.** `_dose_volume_bytes` uses `isinstance(volume_ul, int)`, and `bool` is a subclass of `int`, so `set_container_volume(True)` writes a 1 µl container instead of raising. Something like `isinstance(volume_ul, bool) or not isinstance(volume_ul, int)` fixes it. The `minimum=0` default can go too, since both callers pass 1. Could you also rename it to `_container_volume_to_bytes`? It only packs container volumes, and "dose" in the name suggests it sets an amount to dispense. 5. **Require exactly one dose primitive per schedule point.** `DoseSchedulePoint.parse` accepts any length of 3 + n × 9 bytes but only decodes the first primitive. A point should always be 12 bytes, so anything else should be rejected (`len(data) != 3 + DOSE_PRIMITIVE_SIZE`) rather than half-read. Otherwise an unexpected format would silently produce a wrong daily target instead of no target. Once these are in I'll merge and release, so the ha-mobius PR can go in after it.
DoseSchedulePoint subclasses _PointBase (time_minutes, flags, DosePrimitiveValue with parse/encode) and only accepts exactly one 9-byte dose primitive. The dose attributes are passed to decode_dose_info() by name. _container_volume_to_bytes() rejects bools and always requires at least 1 ul. Adds the dosing pump attributes and enums to the attribute reference.
Author
Contributor
  1. Shared point header. DoseSchedulePoint now subclasses _PointBase (time_minutes, flags) and holds a DosePrimitiveValue (duration_s, volume_ul, flags) with parse()/encode(), like the light and pump points. start_minute, duration_minutes, volume_ul and constant are properties, so the target calculations didn't change.
  2. Dose attributes by name. _decode_dose_from_batch passes each attribute to decode_dose_info() by keyword; _DOSE_REQUESTS is only the request list now.
  3. Attribute reference. Added 2301, 2305, 2310, 2312, 2313 and 2322 with their encodings, plus the DoseSensorType, ContainerMonitorType and DoseHaltState enum tables.
  4. Booleans and the helper. Renamed to _container_volume_to_bytes(), rejects bool, and always requires at least 1 µl (the minimum parameter is gone). There's a test for set_container_volume(True).
  5. Exactly one primitive. Anything other than 3 + 9 bytes is rejected now, with tests for two primitives and a cut-off one. I checked this against my own two pumps running the build: their schedule points are 12 bytes, and the daily target still shows.

Same 7 BLE connection tests fail locally as on main; everything else passes.

1. **Shared point header.** `DoseSchedulePoint` now subclasses `_PointBase` (`time_minutes`, `flags`) and holds a `DosePrimitiveValue` (`duration_s`, `volume_ul`, `flags`) with `parse()`/`encode()`, like the light and pump points. `start_minute`, `duration_minutes`, `volume_ul` and `constant` are properties, so the target calculations didn't change. 2. **Dose attributes by name.** `_decode_dose_from_batch` passes each attribute to `decode_dose_info()` by keyword; `_DOSE_REQUESTS` is only the request list now. 3. **Attribute reference.** Added 2301, 2305, 2310, 2312, 2313 and 2322 with their encodings, plus the `DoseSensorType`, `ContainerMonitorType` and `DoseHaltState` enum tables. 4. **Booleans and the helper.** Renamed to `_container_volume_to_bytes()`, rejects `bool`, and always requires at least 1 µl (the `minimum` parameter is gone). There's a test for `set_container_volume(True)`. 5. **Exactly one primitive.** Anything other than 3 + 9 bytes is rejected now, with tests for two primitives and a cut-off one. I checked this against my own two pumps running the build: their schedule points are 12 bytes, and the daily target still shows. Same 7 BLE connection tests fail locally as on `main`; everything else passes.
r3pek merged commit 5a04b5f03d into main 2026-11-12 08:41:36 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
r3pek/python-mobius!1
No description provided.