From aa489a858c35dfb2066491a9dcfabba910ee88ff Mon Sep 17 00:00:00 2001 From: 5ila5 <5ila5@users.noreply.github.com> Date: Tue, 20 Feb 2024 23:38:02 +0100 Subject: [PATCH] fix heilbronn_de --- .../source/heilbronn_de.py | 115 +++++++----------- doc/source/heilbronn_de.md | 2 +- 2 files changed, 44 insertions(+), 73 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/heilbronn_de.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/heilbronn_de.py index 112a1efa..5b1dca7c 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/heilbronn_de.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/heilbronn_de.py @@ -2,7 +2,6 @@ from datetime import datetime import requests from waste_collection_schedule import Collection # type: ignore[attr-defined] -from waste_collection_schedule.service.ICS import ICS TITLE = "Heilbronn Entsorgungsbetriebe" DESCRIPTION = "Source for city of Heilbronn, Germany." @@ -31,7 +30,7 @@ TEST_CASES = { "Rosenbergstraße 53": { "strasse": "Rosenbergstraße", "plz": "74074", - "hausnr": "53", + "hausnr": "50", }, "Rosenbergstraße 41": { "strasse": "Rosenbergstraße", @@ -41,90 +40,62 @@ TEST_CASES = { } ICON_MAP = { - "R": "mdi:trash-can", - "B": "mdi:leaf", - "GRÜN": "mdi:leaf", - "G": "mdi:recycle", - "A": "mdi:package-variant", - "BT": "mdi:package-variant", + "residual": "mdi:trash-can", + "bio": "mdi:leaf", + "green": "mdi:leaf", + "light-packaging": "mdi:recycle", + "paper": "mdi:package-variant", + "paper-bundle": "mdi:package-variant", + "christmastree": "mdi:pine-tree", } class Source: - def __init__(self, plz: int, strasse: str, hausnr: str | int): + def __init__(self, plz: int, strasse: str, hausnr: str | int | None = None): self._plz: str = str(plz) self._strasse: str = strasse - self._hausnr: str = str(hausnr) - self._ics = ICS() + self._hausnr: str | None = str(hausnr) if hausnr else None def fetch(self): r = requests.get( - "https://abfallratgeber.heilbronn.de/excel_hn/htdocs/street.json" + "https://api.heilbronn.de/garbage-calendar?method=get&datatype=districts" ) r.raise_for_status() - streets: dict = r.json() + data = r.json() - # all the data is spilt in to two files for different kinds of trash - year_response = requests.get( - "https://abfallratgeber.heilbronn.de/excel_hn/htdocs/yearObject.json" + street = data["data"][self._plz][self._strasse] + if not self._hausnr or "*" in street: + if "*" not in street: + raise ValueError( + f"Street {self._strasse} needs to be configured with a house number, available house numbers: {list(street.keys())}" + ) + districts: dict = street["*"] + else: + if self._hausnr not in street: + raise ValueError( + f"House number {self._hausnr} not found for street {self._strasse}, available house numbers: {list(street.keys())}" + ) + districts: dict = street[self._hausnr] + + # filter waste type + collection_keys = [ + value for key, value in districts.items() if key not in ("city", "district") + ] + + r = requests.get( + "https://api.heilbronn.de/garbage-calendar?method=get&datatype=pickupdates" ) - year_response.raise_for_status() - - yearB_response = requests.get( - "https://abfallratgeber.heilbronn.de/excel_hn/htdocs/yearBObject.json" - ) - yearB_response.raise_for_status() - - days_list: list[dict] = [year_response.json(), yearB_response.json()] - - keys = [] - - # get the values to search for in the json and save them in keys - for street in streets: - - if (street["route"] == self._strasse) and ( - street["postal_code"] == self._plz - ): - - if ("street_number" in street) and ( - self._hausnr not in street["street_number"] - ): - continue - keys = [] - for key in list({street["area"], street["g"], street["bt"]}): - keys.append(key) - if "-" in key: - keys.append(key.split("-")[0]) - break - - if keys == []: - raise Exception( - "no address found for " - + self._strasse - + " " - + self._hausnr - + " " - + self._plz - ) + r.raise_for_status() + pickupDates = r.json() entries = [] - # iterate over both json files and search for the keys - for days in days_list: - for day in days.values(): - if "date" not in day: - continue - - date: dict = datetime.strptime(day["date"], "%d.%m.%Y").date() - - for key in keys: - if key in day["districts"] and day["districts"][key] != []: - for entry in day["districts"][key]: - if entry in ICON_MAP: - icon = ICON_MAP.get(entry) - elif len(entry) >= 2 and entry[:2] == "BT": - icon = ICON_MAP.get("BT") - else: - icon = ICON_MAP.get(entry[0]) - entries.append(Collection(date, entry, icon)) + for valueDistrict in collection_keys: + value = pickupDates["data"][valueDistrict] + for collection_type, collection_dates in value.items(): + for value2 in collection_dates.values(): + date: dict = datetime.strptime(value2, "%Y-%m-%d").date() + entry = collection_type + icon = ICON_MAP.get(entry.split("_")[0].lower()) + entries.append(Collection(date, entry, icon)) return entries diff --git a/doc/source/heilbronn_de.md b/doc/source/heilbronn_de.md index bc13cc00..98f236ff 100644 --- a/doc/source/heilbronn_de.md +++ b/doc/source/heilbronn_de.md @@ -39,4 +39,4 @@ waste_collection_schedule: ## How to get the source arguments -Use your PLZ, street and house number. You can check if [Abfallratgeber Heilbronn](https://abfallratgeber.heilbronn.de/#!/calendar) shows correct values, then use exactly the same spelling for your configuration. +Use your PLZ, street and house number. You can check if [Abfuhrtermine Heilbronn](https://abfallwirtschaft.heilbronn.de/abfuhrtermine) shows correct values, then use exactly the same spelling for your configuration.