Skip to content

Conversation

@pooley182
Copy link

@pooley182 pooley182 commented Dec 24, 2025

Currently West Suffolk council does not display years for their bin collection days.
image

This causes next years dates to be in the past as it assumes all years are the current year.

2025-12-24 22:55:26.919 INFO (MainThread) [custom_components.uk_bin_collection] [UKBinCollection] Automatic refresh every 12 hour(s).
2025-12-24 22:55:26.920 DEBUG (MainThread) [custom_components.uk_bin_collection] [UKBinCollection] UKBinCollectionApp args: ['WestSuffolkCouncil', 'https://maps.westsuffolk.gov.uk/MyWestSuffolk.aspx', '--postcode=XXXX XXX', '--uprn=XXXXXXXXXXX']
2025-12-24 22:55:26.920 DEBUG (MainThread) [custom_components.uk_bin_collection] [UKBinCollection] UKBinCollectionApp initialized and arguments set.
2025-12-24 22:55:26.923 DEBUG (MainThread) [custom_components.uk_bin_collection] [UKBinCollection] HouseholdBinCoordinator __init__: name=Home, timeout=60, update_interval=12:00:00
2025-12-24 22:55:26.923 DEBUG (MainThread) [custom_components.uk_bin_collection] [UKBinCollection] HouseholdBinCoordinator initialized with update_interval=12:00:00.
2025-12-24 22:55:26.923 DEBUG (MainThread) [custom_components.uk_bin_collection] [UKBinCollection] _async_update_data called.
2025-12-24 22:55:26.923 INFO (MainThread) [custom_components.uk_bin_collection] [UKBinCollection] Fetching latest bin collection data with timeout=60
2025-12-24 22:55:35.356 DEBUG (MainThread) [custom_components.uk_bin_collection] [UKBinCollection] Raw data fetched from ukbcd.run(): {
    "bins": [
        {
            "type": "Black bin",
            "collectionDate": "29/12/2025"
        },
        {
            "type": "Blue bin",
            "collectionDate": "05/01/2025"
        }
    ]
}

Summary by CodeRabbit

  • Bug Fixes
    • Fixed incorrect year assignment for bin collection dates scheduled in January, ensuring accurate scheduling information.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 24, 2025

📝 Walkthrough

Walkthrough

Modified date parsing logic in WestSuffolkCouncil.py to handle year transitions. When parsing bin collection dates, if the current month is December and the parsed month is January, the code now increments the year to the next year before formatting, replacing the previous approach of always using the current calendar year.

Changes

Cohort / File(s) Summary
Date parsing logic adjustment
uk_bin_collection/uk_bin_collection/councils/WestSuffolkCouncil.py
Modified year inference for bin collection dates to detect month transitions from December to January and automatically increment to the next calendar year before formatting

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • #1743: Implements similar date-parsing/year-inference logic for handling year transitions when parsed month rolls from December into January for bin collection dates.

Suggested reviewers

  • dp247

Poem

🐰 Hop, hop, January's near!
A date fix for the changing year,
December into spring we leap,
No more calendar confusion deep,
West Suffolk bins now know their way,
Clean collections every single day!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 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 identifies the main change: handling end-of-year collection dates for West Suffolk Council, which directly addresses the core issue described in the PR objectives.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 83cce53 and ae46601.

📒 Files selected for processing (1)
  • uk_bin_collection/uk_bin_collection/councils/WestSuffolkCouncil.py
🧰 Additional context used
🪛 Ruff (0.14.10)
uk_bin_collection/uk_bin_collection/councils/WestSuffolkCouncil.py

66-66: datetime may be undefined, or defined from star imports

(F405)


68-68: datetime may be undefined, or defined from star imports

(F405)

Comment on lines +65 to +72
# if current month is December and next_collection month is January, set year to next year
year = datetime.datetime.now().year
if (
datetime.datetime.now().month == 12
and next_collection.month == 1
):
year += 1
next_collection = next_collection.replace(year=year)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Address the datetime import and consider a more robust approach.

The year adjustment logic correctly handles December→January transitions. However, there are a few concerns:

  1. Import issue: Static analysis correctly flags that datetime may be undefined due to the star import on line 6. While it likely works (assuming common.py imports datetime), this is fragile and violates Python best practices.

  2. Multiple datetime.now() calls: Lines 66 and 68 call datetime.now() separately, which is inefficient and could theoretically cause edge-case issues if executed exactly at midnight.

  3. Narrow logic: The December→January check works for the stated problem, but a more general approach would be to check if the parsed date is in the past (using today's date with the assumed current year), and if so, increment the year. This would handle additional edge cases like November→January or queries run in late December that include early January dates.

🔎 Proposed improvements

Fix 1: Address the import and cache the current datetime

+                    from datetime import datetime as dt
+                    
                     # Parse the date
                     next_collection = date_parse(collection_date)

                     # if current month is December and next_collection month is January, set year to next year
-                    year = datetime.datetime.now().year
+                    now = dt.now()
+                    year = now.year
                     if (
-                        datetime.datetime.now().month == 12
+                        now.month == 12
                         and next_collection.month == 1
                     ):
                         year += 1
                     next_collection = next_collection.replace(year=year)

Fix 2 (alternative - more general approach): Check if date is in the past

+                    from datetime import datetime as dt
+                    
                     # Parse the date
                     next_collection = date_parse(collection_date)

-                    # if current month is December and next_collection month is January, set year to next year
-                    year = datetime.datetime.now().year
-                    if (
-                        datetime.datetime.now().month == 12
-                        and next_collection.month == 1
-                    ):
-                        year += 1
-                    next_collection = next_collection.replace(year=year)
+                    # Adjust year if the parsed date (with current year) is in the past
+                    now = dt.now()
+                    next_collection = next_collection.replace(year=now.year)
+                    if next_collection.date() < now.date():
+                        next_collection = next_collection.replace(year=now.year + 1)

Fix 2 is more general and handles any case where the parsed date appears to be in the past.

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Ruff (0.14.10)

66-66: datetime may be undefined, or defined from star imports

(F405)


68-68: datetime may be undefined, or defined from star imports

(F405)

🤖 Prompt for AI Agents
In uk_bin_collection/uk_bin_collection/councils/WestSuffolkCouncil.py around
lines 65-72, replace the fragile star-import usage and duplicated datetime.now()
calls by explicitly importing the datetime module (or from datetime import
datetime) at the top, cache a single "today = datetime.now().date()" value, and
instead of the narrow December→January check set the parsed next_collection to
use the current year and if that date is before today increment the year by 1;
this removes multiple now() calls, fixes the undefined-import risk, and
generalizes the year-adjustment logic to handle any past-month parsed dates.

@pooley182
Copy link
Author

Just seen that #1787 has already been opened to address this.

@pooley182 pooley182 closed this Dec 24, 2025
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