Skip to content

fix: add rotation fallback for south kesteven calendar lookup#2119

Open
InertiaUK wants to merge 1 commit into
robbrad:masterfrom
InertiaUK:fix/south-kesteven-calendar-fallback
Open

fix: add rotation fallback for south kesteven calendar lookup#2119
InertiaUK wants to merge 1 commit into
robbrad:masterfrom
InertiaUK:fix/south-kesteven-calendar-fallback

Conversation

@InertiaUK

@InertiaUK InertiaUK commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Summary

  • When OCR dependencies are unavailable and the hardcoded fallback calendar table doesn't cover the requested date, the scraper raises ValueError instead of returning bin data
  • Adds a 3-week rotation calculation as a final fallback, anchored to a known data point (Oct 2025 week 2 = Silver)
  • Rotation order: Silver (Recycling) -> Black (General waste) -> Purple-lidded (Paper & Card)
  • Verified against the published 2026/27 calendar image from southkesteven.gov.uk/binday

Testing

  • NG31 8DL: previously raised ValueError: No bin type found for 08/06/2026 (Week 2 of 6/2026), now returns correct bin types
  • Cross-checked June and July 2026 output against the council's published calendar PNG - all dates match

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced reliability of bin collection schedules for South Kesteven District Council. The system now gracefully handles missing calendar data by applying a proven rotation pattern, ensuring residents always have access to their collection information without service disruptions or errors.

when ocr is unavailable and the fallback calendar table does not
cover the requested date, calculate the bin type using a 3-week
rotation anchored to a known collection (oct 2025 week 2 = silver).
verified against the published 2026/27 calendar png.
@codecov

codecov Bot commented Jun 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.67%. Comparing base (b65502c) to head (90cca3e).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2119   +/-   ##
=======================================
  Coverage   86.67%   86.67%           
=======================================
  Files           9        9           
  Lines        1141     1141           
=======================================
  Hits          989      989           
  Misses        152      152           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@coderabbitai

coderabbitai Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR modifies South Kesteven District Council's bin collection logic to handle missing calendar data gracefully. When a bin type cannot be found in the parsed calendar for a specific year/month/week, the code now computes a bin type deterministically using a 3-item rotation anchored to 2025-10-13 instead of raising an error.

Changes

Bin Type Fallback

Layer / File(s) Summary
3-week rotation fallback logic
uk_bin_collection/uk_bin_collection/councils/SouthKestevenDistrictCouncil.py
The get_bin_type_from_calendar function replaces the error-raising path with modulo arithmetic to compute a bin type from week offset relative to an anchor date of 2025-10-13.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • dp247

Poem

🐰 A rabbit hops through South Kesteven's bins,
No more crashes when the calendar spins!
Modulo three keeps the rotation in sight,
With 2025-10-13 anchored so tight. 🗑️✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding a rotation fallback for South Kesteven calendar lookup when OCR data is unavailable.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
uk_bin_collection/uk_bin_collection/councils/SouthKestevenDistrictCouncil.py (1)

1126-1137: ⚡ Quick win

Log when the rotation fallback path is taken.

This branch currently hides calendar coverage/OCR gaps silently. Add a warning with collection_date and computed week context so parser drift remains visible while preserving graceful degradation.

Proposed patch
             else:
                 # Fallback: 3-week rotation derived from known anchor
                 # Oct 2025 week 2 (day 8-14) = Silver, week 3 = Black, week 4 = Purple
                 # Anchor: 2025-10-13 (start of week 2) = Silver (index 0)
+                print(
+                    f"Calendar lookup miss for {collection_date} "
+                    f"(Week {week_of_month} of {month}/{year}); using rotation fallback."
+                )
                 ROTATION = [
                     "Silver bin (Recycling)",
                     "Black bin (General waste)",
                     "Purple-lidded bin (Paper & Card)",
                 ]
                 anchor = datetime(2025, 10, 13)
                 days_diff = (date_obj - anchor).days
                 weeks_diff = days_diff // 7
-                return ROTATION[weeks_diff % 3]
+                rotation_index = weeks_diff % 3
+                return ROTATION[rotation_index]

Based on learnings: In uk_bin_collection/**/*.py parsers consumed by Home Assistant, prefer defensive fallbacks with warning logs instead of raising exceptions.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@uk_bin_collection/uk_bin_collection/councils/SouthKestevenDistrictCouncil.py`
around lines 1126 - 1137, Add a warning log in the fallback 3-week rotation
branch so parser drift is visible: when using ROTATION with anchor, log that the
fallback was used and include the collection date (date_obj / collection_date),
the anchor value, and the computed days_diff and weeks_diff before returning
ROTATION[weeks_diff % 3]. Use the module/class logger (e.g., _LOGGER.warning or
self.logger.warning depending on this class’s logging convention) to emit a
concise warning containing those values and a short message like "using fallback
rotation due to missing calendar/OCR coverage".
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In
`@uk_bin_collection/uk_bin_collection/councils/SouthKestevenDistrictCouncil.py`:
- Around line 1126-1137: Add a warning log in the fallback 3-week rotation
branch so parser drift is visible: when using ROTATION with anchor, log that the
fallback was used and include the collection date (date_obj / collection_date),
the anchor value, and the computed days_diff and weeks_diff before returning
ROTATION[weeks_diff % 3]. Use the module/class logger (e.g., _LOGGER.warning or
self.logger.warning depending on this class’s logging convention) to emit a
concise warning containing those values and a short message like "using fallback
rotation due to missing calendar/OCR coverage".

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f2f4cd44-ad0e-444b-b854-54e3578958fb

📥 Commits

Reviewing files that changed from the base of the PR and between b65502c and 90cca3e.

📒 Files selected for processing (1)
  • uk_bin_collection/uk_bin_collection/councils/SouthKestevenDistrictCouncil.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant