fix(coordinator): stop counting delivered packages as in transit - #1400
Open
johnbr wants to merge 1 commit into
Open
fix(coordinator): stop counting delivered packages as in transit#1400johnbr wants to merge 1 commit into
johnbr wants to merge 1 commit into
Conversation
_sum_transit_counts() adds a shipper's *_packages sensor to the in-transit rollup whenever *_delivering is absent or zero. Since moralmunky#1399 standardised *_packages as a computed rollup of *_delivering + *_delivered (GenericShipper._compute_package_totals, and every *_packages entry in SENSOR_DATA is now an empty config with no IMAP search of its own), that fallback double-counts every delivered package. Delivery is exactly the moment it goes wrong: the delivered email drops *_delivering to 0, which makes the value <= 0 guard skip that key, and *_packages is still 1 because it now equals 0 delivering + 1 delivered. The package is then reported as both in transit and delivered until the counts reset at midnight. Observed with a single FedEx package: sensor.mail_packages_in_transit went to 1 when the out-for-delivery email arrived at 09:54 and stayed at 1 after the delivered email at 11:32, alongside sensor.mail_packages_delivered also reading 1, for the rest of the day. With two carriers it compounds — a delivered FedEx package plus a UPS package genuinely out for delivery reports 2 in transit. Only *_delivering feeds the total now, which is what docs/architecture.md already documents mail_packages_in_transit to be: "total count of all packages across all carriers currently out for delivery today". Exceptions are still added, and the amazon_packages special case is untouched — AmazonShipper has its own process_batch and does not go through _compute_package_totals, so amazon_packages keeps its own "total arriving" meaning. test_sum_transit_counts_packages_only covered a packages-only shipper that no longer exists after moralmunky#1399 (hermes has hermes_delivering), so it is retargeted at the new invariant, and the aggregate test's fedex entry gains the delivering count its rollup implies. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #1400 +/- ##
==========================================
+ Coverage 95.07% 99.81% +4.73%
==========================================
Files 8 25 +17
Lines 1482 4259 +2777
==========================================
+ Hits 1409 4251 +2842
+ Misses 73 8 -65
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
sensor.mail_packages_in_transitkeeps counting a package after it has been delivered, so the same package reads as both in transit and delivered until the counts reset at midnight._sum_transit_counts()adds a shipper's*_packagessensor to the rollup whenever*_deliveringis absent or zero:Since #1399 standardised
*_packagesas a computed rollup of*_delivering + *_delivered(GenericShipper._compute_package_totals, and every*_packagesentry inSENSOR_DATAis now an empty config with no IMAP search of its own), that fallback double-counts delivered packages.Delivery is exactly where it breaks: the delivered email drops
*_deliveringto 0, thevalue <= 0guard skips that key, and*_packagesis still 1 because it now equals 0 delivering + 1 delivered.Observed
One FedEx package, one day, nothing else moving:
fedex_deliveringfedex_deliveredfedex_packagespackages_in_transitThe dashboard read
1 transitand1 deliveredfor the rest of the day for a single package that was already on the porch.It compounds across carriers: a delivered FedEx package plus a UPS package genuinely out for delivery reports 2 in transit.
The fix
Only
*_deliveringfeeds the total, which is whatdocs/architecture.mdalready documents the sensor to be — "total count of all packages across all carriers currently out for delivery today".Unchanged:
amazon_packageskeeps its special case.AmazonShipperhas its ownprocess_batchand does not go through_compute_package_totals, soamazon_packagesstill means "total arriving" rather than a delivering+delivered rollup.shippers_countedstill does its job — it is what stopsamazon_deliveringbeing added on top ofamazon_packages.Tests
test_sum_transit_counts_packages_onlycovered a "packages-only shipper" that no longer exists after fix(shippers): standardize packages sensors as rollups and remove shipped subjects #1399 (hermeshashermes_delivering), so it is retargeted at the new invariant.test_aggregate_package_counts's fedex entry gains thefedex_delivering: 1its rollup of 3 implies; expected transit goes 13 → 11 and delivering 5 → 6.test_aggregate_transit_excludes_delivered_packageswalks the single package through both states.752 passed, coverage 99.81%, ruff clean.