Repository: mampfes/hacs_waste_collection_schedule

Issue:
[Bug]: portenf_sa_gov_au reporting incorrect dates
### I Have A Problem With:

A specific source

### What's Your Problem

The portenf_sa_gov_au sensor has been reporting incorrectly since it updated itself on 24 December 2023 (I can see this from HA logs). It appears that when there is 1 week or less left in the month "main-month" switches to the coming month and "other-month" becomes the current month.

Because of this, the integration reports the current collection next month and the next collections as in the past (and hides them).

The fix in #1110 by @5ila5 partly addresses the problem but it was not foreseeable to him that EOM would be treated this way. @5ila5 also noted that this might be an issue in that closed issue.

### Source (if relevant)

portenf_sa_gov_au

### Logs

```Shell
Output of test_sources.py:

Testing source portenf_sa_gov_au ...
  found 8 entries for Broadview, Regency Road, 565
    2024-01-26 : general-waste bin [mdi:trash-can]
    2024-01-26 : recycling bin [mdi:recycle]
    2023-12-02 : general-waste bin [mdi:trash-can]
    2023-12-02 : organics bin [mdi:leaf]
    2023-12-09 : general-waste bin [mdi:trash-can]
    2023-12-09 : recycling bin [mdi:recycle]
    2023-12-16 : general-waste bin [mdi:trash-can]
    2023-12-16 : organics bin [mdi:leaf]
  found 8 entries for 48 Floriedale Rd
    2024-01-26 : general-waste bin [mdi:trash-can]
    2024-01-26 : recycling bin [mdi:recycle]
    2023-12-02 : general-waste bin [mdi:trash-can]
    2023-12-02 : organics bin [mdi:leaf]
    2023-12-09 : general-waste bin [mdi:trash-can]
    2023-12-09 : recycling bin [mdi:recycle]
    2023-12-16 : general-waste bin [mdi:trash-can]
    2023-12-16 : organics bin [mdi:leaf]
  found 8 entries for 24 Margaret Terrace
    2024-01-28 : general-waste bin [mdi:trash-can]
    2024-01-28 : organics bin [mdi:leaf]
    2023-12-04 : general-waste bin [mdi:trash-can]
    2023-12-04 : recycling bin [mdi:recycle]
    2023-12-11 : general-waste bin [mdi:trash-can]
    2023-12-11 : organics bin [mdi:leaf]
    2023-12-18 : general-waste bin [mdi:trash-can]
    2023-12-18 : recycling bin [mdi:recycle]
  found 8 entries for Addison Road 91 with unit
    2024-01-28 : general-waste bin [mdi:trash-can]
    2024-01-28 : organics bin [mdi:leaf]
    2023-12-04 : general-waste bin [mdi:trash-can]
    2023-12-04 : recycling bin [mdi:recycle]
    2023-12-11 : general-waste bin [mdi:trash-can]
    2023-12-11 : organics bin [mdi:leaf]
    2023-12-18 : general-waste bin [mdi:trash-can]
    2023-12-18 : recycling bin [mdi:recycle]
```


### Relevant Configuration

_No response_

### Checklist Source Error

- [X] Use the example parameters for your source (often available in the documentation) (don't forget to restart Home Assistant after changing the configuration)
- [X] Checked that the website of your service provider is still working
- [X] Tested my attributes on the service provider website (if possible)
- [X] I have tested with the latest version of the integration (master) (for HACS in the 3 dot menu of the integration click on "Redownload" and choose master as version)

### Checklist Sensor Error

- [X] Checked in the Home Assistant Calendar tab if the event names match the types names (if types argument is used)

### Required

- [X] I have searched past (closed AND opened) issues to see if this bug has already been reported, and it hasn't been.
- [X] I understand that people give their precious time for free, and thus I've done my very best to make this problem as easy as possible to investigate.


Discussion:
Fix will be part of release 1.45.0 or is available now as master (git) version (HACS: 3-dot menu -> redownload -> change version to master)
@5ila5 looks like the logic is broken somewhere:

2024-01-17 05:57:30.836 ERROR (SyncWorker_21) [waste_collection_schedule.source_shell] fetch failed for source Port Adelaide Enfield, South Australia:

Traceback (most recent call last):

File "/config/custom_components/waste_collection_schedule/waste_collection_schedule/source_shell.py", line 134, in fetch

entries = self._source.fetch()

^^^^^^^^^^^^^^^^^^^^

File "/config/custom_components/waste_collection_schedule/waste_collection_schedule/source/portenf_sa_gov_au.py", line 198, in fetch

date = datetime.strptime(f"{year}-{month}-{day}", "%Y-%B-%d").date()

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/_strptime.py", line 568, in _strptime_datetime

tt, fraction, gmtoff_fraction = _strptime(data_string, format)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/_strptime.py", line 534, in _strptime

julian = datetime_date(year, month, day).toordinal() - \

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ValueError: day is out of range for month

I'll dive into the code and look for myself when I have some time. 

Resolving patch:
diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/portenf_sa_gov_au.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/portenf_sa_gov_au.py
--- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/portenf_sa_gov_au.py
+++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/portenf_sa_gov_au.py
@@ -163,12 +163,14 @@ def fetch(self):
             from_month = from_month.split(" ")[0]
 
         today_div = soup.find("table", id="cal").find("td", class_="today")
-        print(today_div)
 
         # if other-month is to_month
         if (
-            "other-month" in today_div.attrs
-            and datetime.now().strftime("%B") != to_month
+            "other-month" in today_div.attrs["class"]
+            and datetime.now().strftime("%B") == to_month
+        ) or (
+            "main-month" in today_div.attrs["class"]
+            and datetime.now().strftime("%B") == from_month
         ):
             main_month, other_month = from_month, to_month
             main_year, other_year = from_year, to_year


Test patch: