Dosing pump: container entities (remaining, level, days left, refill) #5

Merged
r3pek merged 3 commits from Gulaschcowboy/ha-mobius:pr/dosing-container into main 2026-11-12 08:51:43 +00:00
Contributor

Adds container tracking for dosing pumps (Versa), read from the device's own container attributes and dose schedule.

New entities (only created for devices that report the dosing attributes):

  • Sensors: container remaining (ml), container level (%), container days left, dosed today, daily dose target
  • Binary sensor: container empty
  • Button: refill container (needs a second press within a short window, with a persistent notification in between, so a single misclick does not reset the countdown)
  • Number: container size (ml)

Details

  • Days left = remaining volume / daily target. The daily target comes from Schedule1 (single doses plus constant-rate blocks). Without a readable schedule the sensor is unknown instead of guessing.
  • "Expected so far" is prorated within running blocks, including blocks that run past midnight.
  • The dose schedule is cached like the other static attributes, so the regular poll stays short.
  • Attributes are read in one batch request, with a fallback to single reads.
  • Fixes the dosed-today attribute (2301). The daily dose sensor has no state class (volume + measurement is not valid).

Tests

New tests/test_dose_container.py (17 tests: schedule decoding, container read incl. fallback and cache, entity values, refill confirmation, container size write). Full suite passes (531).

Adds container tracking for dosing pumps (Versa), read from the device's own container attributes and dose schedule. **New entities** (only created for devices that report the dosing attributes): - Sensors: container remaining (ml), container level (%), container days left, dosed today, daily dose target - Binary sensor: container empty - Button: refill container (needs a second press within a short window, with a persistent notification in between, so a single misclick does not reset the countdown) - Number: container size (ml) **Details** - Days left = remaining volume / daily target. The daily target comes from Schedule1 (single doses plus constant-rate blocks). Without a readable schedule the sensor is unknown instead of guessing. - "Expected so far" is prorated within running blocks, including blocks that run past midnight. - The dose schedule is cached like the other static attributes, so the regular poll stays short. - Attributes are read in one batch request, with a fallback to single reads. - Fixes the dosed-today attribute (2301). The daily dose sensor has no state class (volume + measurement is not valid). **Tests** New `tests/test_dose_container.py` (17 tests: schedule decoding, container read incl. fallback and cache, entity values, refill confirmation, container size write). Full suite passes (531).
Owner

Hi @Gulaschcowboy , thanks a lot for this! Dosing pump support has been missing, and I checked the attribute IDs, the encodings and the refill write against the Mobius app, and they all match. It applies cleanly and the tests pass.

Before I merge it, there are a few things I'd like changed:

  1. Protocol code belongs in python-mobius. The attribute IDs, byte decoding and dose schedule parsing should go into the library, which still lists DoseV1 as not implemented. The integration should only build entities from what the library returns. The changes would be a PR there and a smaller one here.
  2. Poll traffic. _read_dose_container() sends an extra request on every poll. For devices reached through a gateway that's an extra round trip each time. The dose attributes should be added to the library's poll batch instead, with the rarely changing ones (container size, monitoring, sensor type) read every 10 minutes like the other static attributes.
  3. Schedule read. get_attribute() only returns the first block of a reply, so a long schedule can come back incomplete. Please use get_attribute_raw_all(). For caching, please use the device's ConfigurationCache rather than a module-level dictionary, so reloads, "Refresh all data" and writes clear it like everything else. (I'm actually in the process of a little refactor in this area of the code to reduce duplicate stuff, so maybe wait a day or 2 so I push the refactored code)
  4. Refill button. Please make it a single press, without the confirmation notification and the language-specific text. HA buttons do one thing per press, like Reboot, and a dashboard can add a confirmation dialog if someone wants one.
  5. Container size number. native_value raises a TypeError when the container size couldn't be read on a poll. Also, the app resets the remaining volume when the size changes, and I'd rather match that.
  6. Smaller things:
    • "Dosing halt state" and "Container monitoring" should be enum sensors with translated states.
    • What does the pump report when container monitoring is set to "none"? If the remaining volume isn't meaningful then, the related entities should probably be unavailable.
    • Please add a short CHANGELOG entry and a README line.

If anything here is unclear, or you'd like help with the python-mobius side, just ask. Thanks again!

Hi @Gulaschcowboy , thanks a lot for this! Dosing pump support has been missing, and I checked the attribute IDs, the encodings and the refill write against the Mobius app, and they all match. It applies cleanly and the tests pass. Before I merge it, there are a few things I'd like changed: 1. **Protocol code belongs in python-mobius.** The attribute IDs, byte decoding and dose schedule parsing should go into the library, which still lists `DoseV1` as not implemented. The integration should only build entities from what the library returns. The changes would be a PR there and a smaller one here. 2. **Poll traffic.** `_read_dose_container()` sends an extra request on every poll. For devices reached through a gateway that's an extra round trip each time. The dose attributes should be added to the library's poll batch instead, with the rarely changing ones (container size, monitoring, sensor type) read every 10 minutes like the other static attributes. 3. **Schedule read.** `get_attribute()` only returns the first block of a reply, so a long schedule can come back incomplete. Please use `get_attribute_raw_all()`. For caching, please use the device's `ConfigurationCache` rather than a module-level dictionary, so reloads, "Refresh all data" and writes clear it like everything else. (I'm actually in the process of a little refactor in this area of the code to reduce duplicate stuff, so maybe wait a day or 2 so I push the refactored code) 4. **Refill button.** Please make it a single press, without the confirmation notification and the language-specific text. HA buttons do one thing per press, like Reboot, and a dashboard can add a confirmation dialog if someone wants one. 5. **Container size number.** `native_value` raises a `TypeError` when the container size couldn't be read on a poll. Also, the app resets the remaining volume when the size changes, and I'd rather match that. 6. **Smaller things:** - "Dosing halt state" and "Container monitoring" should be enum sensors with translated states. - What does the pump report when container monitoring is set to "none"? If the remaining volume isn't meaningful then, the related entities should probably be unavailable. - Please add a short CHANGELOG entry and a README line. If anything here is unclear, or you'd like help with the python-mobius side, just ask. Thanks again!
Author
Contributor

Hi @r3pek, thanks for your comments and suggestions.

I will take care of that, while I have to say, that I implemented the from-the-app-diverging behavior by purpose, as I dislike the Apps behavior. But I might come back later with a proposal.

Regarding the mentioned reboots and also gateway selection process: I would like to discuss this with you, as the mobius thread network is quite unstable at my side and it seems, that my dosing pumps sometimes loose their dosing history/statistics if they are rebooted at the wrong time.
Do you think we can have a chat somewhere besides this issue tracker?

Hi @r3pek, thanks for your comments and suggestions. I will take care of that, while I have to say, that I implemented the from-the-app-diverging behavior by purpose, as I dislike the Apps behavior. But I might come back later with a proposal. Regarding the mentioned reboots and also gateway selection process: I would like to discuss this with you, as the mobius thread network is quite unstable at my side and it seems, that my dosing pumps sometimes loose their dosing history/statistics if they are rebooted at the wrong time. Do you think we can have a chat somewhere besides this issue tracker?
Gulaschcowboy force-pushed pr/dosing-container from 184250ad98
Some checks failed
Test / test (pull_request) Has been cancelled
Test / frontend (pull_request) Has been cancelled
to 4ea17b495e
Some checks failed
Test / test (HA 2026.8) (pull_request) Has been cancelled
Test / test (HA latest) (pull_request) Has been cancelled
Test / frontend (pull_request) Has been cancelled
2026-11-06 10:44:48 +00:00
Compare
Address review: enum state sensors, container size None, monitoring-gated availability
Some checks failed
Test / test (HA 2026.8) (pull_request) Has been cancelled
Test / test (HA latest) (pull_request) Has been cancelled
Test / frontend (pull_request) Has been cancelled
84ecdbd78c
- Container monitoring and dosing halt state are enum sensors with fixed,
  translated options derived from the library enums.
- Container size returns None instead of raising a TypeError when the pump
  doesn't report it.
- Container remaining, level, days left, empty and the refill button are
  unavailable when the pump doesn't monitor its container.
- Document the dosing pump entities in README and CHANGELOG.
Author
Contributor

Hi @r3pek, I've pushed an update that covers your review:

  1. Protocol code is in python-mobius now. The attribute IDs, the decoding and the dose schedule parsing live in the library (mobius/dose.py, FullPollResult.dose / DoseInfo, get_dose_info(), set_container_volume(), refill_container()), see r3pek/python-mobius#1 (r3pek/python-mobius#1), with tests, a CHANGELOG entry and a docs update. This PR only builds the entities from what the library returns, so it needs that one merged and released first.
  2. Poll traffic. There's no separate _read_dose_container() request anymore. The dose attributes are part of the library's poll batch, and container size, monitor type and sensor type are in STATIC_POLL_ATTRIBUTES, so they're read at the static refresh interval like the other static attributes.
  3. Schedule read. The dose schedule now goes through the same configuration path as the other schedules, so all blocks of the reply are read. It's cached in the device's ConfigurationCache by its checksum, with no module-level dictionary anymore, so reloads, "Refresh all data" and writes clear it like everything else. The branch is based on the current main (48feac7). I'll rebase onto your refactor once it's pushed.
  4. Refill button. It's a single press now: no confirmation notification, no language-specific text.
  5. Container size number. native_value returns None when the size couldn't be read, so there's no TypeError. Setting the size also resets the remaining volume, like the app (set_container_volume() in the library).
  6. Smaller things:
    • "Dosing halt state" and "Container monitoring" are enum sensors with translated states.
    • With container monitoring set to "none", the remaining volume isn't tracked, so the remaining volume, level, days left and container-empty entities are unavailable then.
    • I added a CHANGELOG entry and a README line.

All tests pass. Thanks again for the thorough review!

Hi @r3pek, I've pushed an update that covers your review: 1. **Protocol code is in python-mobius now.** The attribute IDs, the decoding and the dose schedule parsing live in the library (`mobius/dose.py`, `FullPollResult.dose` / `DoseInfo`, `get_dose_info()`, `set_container_volume()`, `refill_container()`), see r3pek/python-mobius#1 (https://code.r3pek.org/r3pek/python-mobius/pulls/1), with tests, a CHANGELOG entry and a docs update. This PR only builds the entities from what the library returns, so it needs that one merged and released first. 2. **Poll traffic.** There's no separate `_read_dose_container()` request anymore. The dose attributes are part of the library's poll batch, and container size, monitor type and sensor type are in `STATIC_POLL_ATTRIBUTES`, so they're read at the static refresh interval like the other static attributes. 3. **Schedule read.** The dose schedule now goes through the same configuration path as the other schedules, so all blocks of the reply are read. It's cached in the device's `ConfigurationCache` by its checksum, with no module-level dictionary anymore, so reloads, "Refresh all data" and writes clear it like everything else. The branch is based on the current `main` (48feac7). I'll rebase onto your refactor once it's pushed. 4. **Refill button.** It's a single press now: no confirmation notification, no language-specific text. 5. **Container size number.** `native_value` returns `None` when the size couldn't be read, so there's no `TypeError`. Setting the size also resets the remaining volume, like the app (`set_container_volume()` in the library). 6. **Smaller things:** - "Dosing halt state" and "Container monitoring" are enum sensors with translated states. - With container monitoring set to "none", the remaining volume isn't tracked, so the remaining volume, level, days left and container-empty entities are unavailable then. - I added a CHANGELOG entry and a README line. All tests pass. Thanks again for the thorough review!
Owner

@Gulaschcowboy wrote in #5 (comment):

Regarding the mentioned reboots and also gateway selection process: I would like to discuss this with you, as the mobius thread network is quite unstable at my side and it seems, that my dosing pumps sometimes loose their dosing history/statistics if they are rebooted at the wrong time. Do you think we can have a chat somewhere besides this issue tracker?

Sure. That really looks like a problem that needs to be solved 'cause I don't see it on my end (even with 5 devices, but no dosers)


In regards to the update, it addresses everything from my first review. A few things left (sorry for not being thorough the first time):

  1. Let the library read the container size on refill. The refill button passes container_volume_ul from the last poll, but the size is only read every 10 minutes, so it can be stale (e.g. right after changing the container in the app). Refill is a rare press, so calling refill_container() without an argument costs one read and always uses the size the pump has.

  2. State class of "Expected dosed so far". It's TOTAL_INCREASING, so Home Assistant keeps long-term sum statistics for it, but it's calculated from the schedule rather than measured, and it drops if the schedule changes during the day (which HA treats as a meter reset). MEASUREMENT, or no state class, fits better. "Dosed today" as TOTAL_INCREASING is fine.

  3. Explicit enum mapping. _enum_key maps any enum name starting with "no" to "none". That's right for NoSensor and NoMonitor, but a future value like Normal would also become "none". Could you use an explicit mapping (or just special-case the two No… members)?

  4. Unused _enabled_default in _DataKeySensor. It's added in sensor.py but only _DoseSensor reads it, so it can go.

  5. Rename _PumpBatteryNumber. Reusing it for ContainerSizeNumber makes sense, since nothing in it is battery-specific, but the name is now misleading. Could you rename it to something general like _DeviceDataNumber, with the battery numbers and the container size both subclassing it?

  6. Test the coordinator path. The new tests build the entities from hand-made data, but nothing runs a poll through _fetch_all to check that info["dose"] is filled and that dosing pumps no longer get the "no parser implemented" support note. One test using tests/fake_full_poll.py would cover it.

@Gulaschcowboy wrote in https://code.r3pek.org/r3pek/ha-mobius/pulls/5#issuecomment-51: > Regarding the mentioned reboots and also gateway selection process: I would like to discuss this with you, as the mobius thread network is quite unstable at my side and it seems, that my dosing pumps sometimes loose their dosing history/statistics if they are rebooted at the wrong time. Do you think we can have a chat somewhere besides this issue tracker? Sure. That really looks like a problem that needs to be solved 'cause I don't see it on my end (even with 5 devices, but no dosers) --- In regards to the update, it addresses everything from my first review. A few things left (sorry for not being thorough the first time): 1. **Let the library read the container size on refill.** The refill button passes `container_volume_ul` from the last poll, but the size is only read every 10 minutes, so it can be stale (e.g. right after changing the container in the app). Refill is a rare press, so calling `refill_container()` without an argument costs one read and always uses the size the pump has. 2. **State class of "Expected dosed so far".** It's `TOTAL_INCREASING`, so Home Assistant keeps long-term sum statistics for it, but it's calculated from the schedule rather than measured, and it drops if the schedule changes during the day (which HA treats as a meter reset). `MEASUREMENT`, or no state class, fits better. "Dosed today" as `TOTAL_INCREASING` is fine. 3. **Explicit enum mapping.** `_enum_key` maps any enum name starting with "no" to `"none"`. That's right for `NoSensor` and `NoMonitor`, but a future value like `Normal` would also become `"none"`. Could you use an explicit mapping (or just special-case the two `No…` members)? 4. **Unused `_enabled_default` in `_DataKeySensor`.** It's added in `sensor.py` but only `_DoseSensor` reads it, so it can go. 5. **Rename `_PumpBatteryNumber`.** Reusing it for `ContainerSizeNumber` makes sense, since nothing in it is battery-specific, but the name is now misleading. Could you rename it to something general like `_DeviceDataNumber`, with the battery numbers and the container size both subclassing it? 6. **Test the coordinator path.** The new tests build the entities from hand-made data, but nothing runs a poll through `_fetch_all` to check that `info["dose"]` is filled and that dosing pumps no longer get the "no parser implemented" support note. One test using `tests/fake_full_poll.py` would cover it.
Address review: refill reads the size, no sum statistics for expected dose, explicit enum mapping
Some checks failed
Test / test (HA 2026.8) (pull_request) Successful in 6m48s
Test / test (HA latest) (pull_request) Successful in 6m49s
Test / frontend (pull_request) Successful in 1m39s
Test / test (HA 2026.8) (push) Has been cancelled
Test / test (HA latest) (push) Has been cancelled
Test / frontend (push) Has been cancelled
9425271145
The refill button lets python-mobius read the current container size instead of passing the polled one. "Expected dosed so far" has no state class (it is calculated and can drop; MEASUREMENT isn't allowed with the VOLUME device class). Only NoMonitor and NoSensor map to "none". Renames _PumpBatteryNumber to _DeviceDataNumber, drops the unused _enabled_default of _DataKeySensor, and tests the poll path through _fetch_all with fake_full_poll.
Author
Contributor

Thanks, and no worries! All six are in:

  1. Refill reads the size. The button calls refill_container() without an argument, so the library reads the container size from the pump.
  2. "Expected dosed so far". It has no state class now. MEASUREMENT isn't one of the allowed state classes for the volume device class (only total / total_increasing), so I went with none rather than switching the device class. "Dosed today" stays TOTAL_INCREASING.
  3. Explicit enum mapping. Only ContainerMonitorType.NoMonitor and DoseSensorType.NoSensor map to "none". They're matched by enum type and member name, not by value, because all three dose enums are IntEnums and NoMonitor, NoSensor and DoseHaltState.Running are all 0. A test checks that Running stays "running" and that a name like Normal isn't turned into "none".
  4. _enabled_default is gone from _DataKeySensor.
  5. _PumpBatteryNumber is now _DeviceDataNumber; the battery numbers and ContainerSizeNumber subclass it.
  6. Coordinator path. tests/fake_full_poll.py fills FullPollResult.dose for DoseV1 from get_dose_info(), and two new tests run _fetch_all(): one checks that info["dose"] is filled and there's no "no parser implemented" support note, the other that the note is still there when the pump returns no dose info.

The library changes for r3pek/python-mobius#1 are pushed there too. All tests pass.

Thanks, and no worries! All six are in: 1. **Refill reads the size.** The button calls `refill_container()` without an argument, so the library reads the container size from the pump. 2. **"Expected dosed so far".** It has no state class now. `MEASUREMENT` isn't one of the allowed state classes for the `volume` device class (only `total` / `total_increasing`), so I went with none rather than switching the device class. "Dosed today" stays `TOTAL_INCREASING`. 3. **Explicit enum mapping.** Only `ContainerMonitorType.NoMonitor` and `DoseSensorType.NoSensor` map to `"none"`. They're matched by enum type and member name, not by value, because all three dose enums are `IntEnum`s and `NoMonitor`, `NoSensor` and `DoseHaltState.Running` are all 0. A test checks that `Running` stays `"running"` and that a name like `Normal` isn't turned into `"none"`. 4. **`_enabled_default`** is gone from `_DataKeySensor`. 5. **`_PumpBatteryNumber`** is now `_DeviceDataNumber`; the battery numbers and `ContainerSizeNumber` subclass it. 6. **Coordinator path.** `tests/fake_full_poll.py` fills `FullPollResult.dose` for `DoseV1` from `get_dose_info()`, and two new tests run `_fetch_all()`: one checks that `info["dose"]` is filled and there's no "no parser implemented" support note, the other that the note is still there when the pump returns no dose info. The library changes for r3pek/python-mobius#1 are pushed there too. All tests pass.
r3pek merged commit 9425271145 into main 2026-11-12 08:51:43 +00:00
r3pek deleted branch pr/dosing-container 2026-11-12 08:51:43 +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/ha-mobius!5
No description provided.