diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/dudley_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/dudley_gov_uk.py index f8f399499..d20780b0a 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/dudley_gov_uk.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/dudley_gov_uk.py @@ -15,7 +15,19 @@ "Test_004": {"uprn": 90092621}, } ICON_MAP = {"RECYCLING": "mdi:recycle", "GARDEN": "mdi:leaf", "REFUSE": "mdi:trash-can"} -REGEX = r"(\d+ \w{3})" +REGEX = { + "DATES": r"(\d+ \w{3})", + "DAYS": r"every: (Monday|Tuesday|Wednesday|Thursday|Friday)", +} +DAYS = { + "Monday": 0, + "Tuesday": 1, + "Wednesday": 2, + "Thursday": 3, + "Friday": 4, + "Saturday": 5, + "Sunday": 6, +} class Source: @@ -35,12 +47,6 @@ def check_date(self, d: str, t: datetime, y: int): return date.date() def append_entries(self, d: datetime, w: str, e: list) -> list: - """ - Append provided entry and Refuse entry for the same day. - - Refuse is collected on the same dates as alternating Recycling/Garden collections, - so create two entries for each date Refuse & Recycling, or Refuse & Garden - """ e.append( Collection( date=d, @@ -48,17 +54,9 @@ def append_entries(self, d: datetime, w: str, e: list) -> list: icon=ICON_MAP.get(w.upper()), ) ) - e.append( - Collection( - date=d, - t="Refuse", - icon=ICON_MAP.get("REFUSE"), - ) - ) return e def fetch(self): - today = datetime.now() today = today.replace(hour=0, minute=0, second=0, microsecond=0) yr = int(today.year) @@ -71,22 +69,30 @@ def fetch(self): panel = soup.find("div", {"aria-label": "Refuse and Recycling Collection"}) panel_data = panel.find("div", {"class": "atPanelData"}) - panel_data = panel_data.text.split("Next")[ + waste_data = panel_data.text.split("Next")[ 1: ] # remove first element it just contains general info entries = [] - for item in panel_data: + # Deal with Recycling and Garden collections + for item in waste_data: text = item.replace("\r\n", "").strip() if "recycling" in text: - dates = re.findall(REGEX, text) + dates = re.findall(REGEX["DATES"], text) for dt in dates: dt = self.check_date(dt, today, yr) self.append_entries(dt, "Recycling", entries) elif "garden" in text: - dates = re.findall(REGEX, text) + dates = re.findall(REGEX["DATES"], text) for dt in dates: dt = self.check_date(dt, today, yr) self.append_entries(dt, "Garden", entries) + # Refuse collections only have a DAY not a date, so work out dates for the next few collections + refuse_day = re.findall(REGEX["DAYS"], panel_data.text)[0] + refuse_date = today + timedelta((int(DAYS[refuse_day]) - today.weekday()) % 7) + for i in range(0, 4): + temp_date = refuse_date + timedelta(days=7 * i) + self.append_entries(temp_date.date(), "Refuse", entries) + return entries diff --git a/tests/bandit.yaml b/tests/bandit.yaml index ebd284eaa..50adb7744 100644 --- a/tests/bandit.yaml +++ b/tests/bandit.yaml @@ -12,6 +12,6 @@ tests: - B318 - B319 - B320 - - B325 +# - B325 - B602 - B604