Dosing pump: container entities (remaining, level, days left, refill) #5
Loading…
Reference in a new issue
No description provided.
Delete branch "Gulaschcowboy/ha-mobius:pr/dosing-container"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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):
Details
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).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:
DoseV1as 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._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.get_attribute()only returns the first block of a reply, so a long schedule can come back incomplete. Please useget_attribute_raw_all(). For caching, please use the device'sConfigurationCacherather 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)native_valueraises aTypeErrorwhen 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.If anything here is unclear, or you'd like help with the python-mobius side, just ask. Thanks again!
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?
184250ad984ea17b495eHi @r3pek, I've pushed an update that covers your review:
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._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 inSTATIC_POLL_ATTRIBUTES, so they're read at the static refresh interval like the other static attributes.ConfigurationCacheby 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 currentmain(48feac7). I'll rebase onto your refactor once it's pushed.native_valuereturnsNonewhen the size couldn't be read, so there's noTypeError. Setting the size also resets the remaining volume, like the app (set_container_volume()in the library).All tests pass. Thanks again for the thorough review!
@Gulaschcowboy wrote in #5 (comment):
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):
Let the library read the container size on refill. The refill button passes
container_volume_ulfrom 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 callingrefill_container()without an argument costs one read and always uses the size the pump has.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" asTOTAL_INCREASINGis fine.Explicit enum mapping.
_enum_keymaps any enum name starting with "no" to"none". That's right forNoSensorandNoMonitor, but a future value likeNormalwould also become"none". Could you use an explicit mapping (or just special-case the twoNo…members)?Unused
_enabled_defaultin_DataKeySensor. It's added insensor.pybut only_DoseSensorreads it, so it can go.Rename
_PumpBatteryNumber. Reusing it forContainerSizeNumbermakes 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?Test the coordinator path. The new tests build the entities from hand-made data, but nothing runs a poll through
_fetch_allto check thatinfo["dose"]is filled and that dosing pumps no longer get the "no parser implemented" support note. One test usingtests/fake_full_poll.pywould cover it.Thanks, and no worries! All six are in:
refill_container()without an argument, so the library reads the container size from the pump.MEASUREMENTisn't one of the allowed state classes for thevolumedevice class (onlytotal/total_increasing), so I went with none rather than switching the device class. "Dosed today" staysTOTAL_INCREASING.ContainerMonitorType.NoMonitorandDoseSensorType.NoSensormap to"none". They're matched by enum type and member name, not by value, because all three dose enums areIntEnums andNoMonitor,NoSensorandDoseHaltState.Runningare all 0. A test checks thatRunningstays"running"and that a name likeNormalisn't turned into"none"._enabled_defaultis gone from_DataKeySensor._PumpBatteryNumberis now_DeviceDataNumber; the battery numbers andContainerSizeNumbersubclass it.tests/fake_full_poll.pyfillsFullPollResult.doseforDoseV1fromget_dose_info(), and two new tests run_fetch_all(): one checks thatinfo["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.