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:

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
@@ -1,6 +1,6 @@
-import datetime
 import logging
 import re
+from datetime import datetime
 
 import requests
 import urllib3
@@ -153,15 +153,28 @@ def fetch(self):
 
         soup = BeautifulSoup(r.text, "html.parser")
         cal_header = soup.find("th", {"class": "header-month"}).find("span").text
-        main_month = cal_header.split("-")[0].strip()
-
-        secondary_month = cal_header.split("-")[1].strip().split(" ")[0]
-        secondary_year = main_year = cal_header.split("-")[1].strip().split(" ")[1]
 
+        from_month = cal_header.split("-")[0].strip()
+        to_month = cal_header.split("-")[1].strip().split(" ")[0]
+        to_year = from_year = cal_header.split("-")[1].strip().split(" ")[1]
         # if main month contains a year, set it (maybe happens in december???)
-        if len(main_month.split(" ")) > 1:
-            main_year = main_month.split(" ")[1]
-            main_month = main_month.split(" ")[0]
+        if len(from_month.split(" ")) > 1:
+            from_year = from_month.split(" ")[1]
+            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
+        ):
+            main_month, other_month = from_month, to_month
+            main_year, other_year = from_year, to_year
+        else:  # if other-month is from_month
+            main_month, other_month = to_month, from_month
+            main_year, other_year = to_year, from_year
 
         entries = []
 
@@ -172,15 +185,9 @@ def fetch(self):
         ):
             parent_td = pickup.parent
             month = (
-                main_month
-                if "main-month" in parent_td.attrs["class"]
-                else secondary_month
-            )
-            year = (
-                main_year
-                if "main-month" in parent_td.attrs["class"]
-                else secondary_year
+                main_month if "main-month" in parent_td.attrs["class"] else other_month
             )
+            year = main_year if "main-month" in parent_td.attrs["class"] else other_year
             day = parent_td.find("div", {"class": "daynumber"}).text
 
             # Iterate over all pickup container types for this day
@@ -188,9 +195,7 @@ def fetch(self):
                 container_type = " ".join(container.find("div").attrs["class"])
                 container_icon = ICON_MAP.get(container_type)
 
-                date = datetime.datetime.strptime(
-                    f"{year}-{month}-{day}", "%Y-%B-%d"
-                ).date()
+                date = datetime.strptime(f"{year}-{month}-{day}", "%Y-%B-%d").date()
                 entries.append(
                     Collection(date=date, t=container_type, icon=container_icon)
                 )


Test patch: