Skip to content

fix(coordinator): stop counting delivered packages as in transit - #1400

Open
johnbr wants to merge 1 commit into
moralmunky:devfrom
johnbr:fix/in-transit-rollup-counts-delivered
Open

fix(coordinator): stop counting delivered packages as in transit#1400
johnbr wants to merge 1 commit into
moralmunky:devfrom
johnbr:fix/in-transit-rollup-counts-delivered

Conversation

@johnbr

@johnbr johnbr commented Sep 2, 2026

Copy link
Copy Markdown

The bug

sensor.mail_packages_in_transit keeps 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 *_packages sensor to the rollup whenever *_delivering is absent or zero:

if key.endswith(("_delivering", "_packages")):
    transit += value
    shippers_counted.add(shipper)

Since #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 delivered packages.

Delivery is exactly where it breaks: the delivered email drops *_delivering to 0, the value <= 0 guard skips that key, and *_packages is still 1 because it now equals 0 delivering + 1 delivered.

Observed

One FedEx package, one day, nothing else moving:

local time event fedex_delivering fedex_delivered fedex_packages packages_in_transit
09:54 out-for-delivery email 0 → 1 0 0 → 1 0 → 1
11:32 delivered email 1 → 0 0 → 1 1 1

The dashboard read 1 transit and 1 delivered for 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 *_delivering feeds the total, which is what docs/architecture.md already documents the sensor to be — "total count of all packages across all carriers currently out for delivery today".

Unchanged:

  • Exceptions are still added for all shippers.
  • amazon_packages keeps its special case. AmazonShipper has its own process_batch and does not go through _compute_package_totals, so amazon_packages still means "total arriving" rather than a delivering+delivered rollup.
  • shippers_counted still does its job — it is what stops amazon_delivering being added on top of amazon_packages.

Tests

  • test_sum_transit_counts_packages_only covered a "packages-only shipper" that no longer exists after fix(shippers): standardize packages sensors as rollups and remove shipped subjects #1399 (hermes has hermes_delivering), so it is retargeted at the new invariant.
  • test_aggregate_package_counts's fedex entry gains the fedex_delivering: 1 its rollup of 3 implies; expected transit goes 13 → 11 and delivering 5 → 6.
  • New test_aggregate_transit_excludes_delivered_packages walks the single package through both states.

752 passed, coverage 99.81%, ruff clean.

_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-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.81%. Comparing base (8608d22) to head (9256cc2).
⚠️ Report is 240 commits behind head on dev.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files

Impacted file tree graph

@@            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     
Flag Coverage Δ
python 99.81% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
custom_components/mail_and_packages/coordinator.py 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@firstof9 firstof9 added the bugfix Pull request to fix a bug label Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Pull request to fix a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants