From 21c8269958f556e4f5316eecdf0c42e8471c34dc Mon Sep 17 00:00:00 2001 From: 5ila5 <5ila5@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:06:02 +0200 Subject: [PATCH] fix aha_region_de + add ladeort argument --- .../source/aha_region_de.py | 83 +++++++++++-------- doc/source/aha_region_de.md | 18 ++++ 2 files changed, 65 insertions(+), 36 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/aha_region_de.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/aha_region_de.py index 9b08782f..e4a686c5 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/aha_region_de.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/aha_region_de.py @@ -30,6 +30,12 @@ TEST_CASES = { "hnr": "10", "zusatz": "A", }, + "Mit Ladeort": { + "gemeinde": "Gehrden", + "strasse": "Kirchstr. / Gehrden", + "hnr": "1", + "ladeort": "Kirchstr. 6, Gehrden / Gehrden", + }, } ICON_MAP = { @@ -46,12 +52,18 @@ LOGGER = logging.getLogger(__name__) class Source: def __init__( - self, gemeinde: str, strasse: str, hnr: str | int, zusatz: str | int = "" + self, + gemeinde: str, + strasse: str, + hnr: str | int, + zusatz: str | int = "", + ladeort=None, ): self._gemeinde: str = gemeinde self._strasse: str = strasse self._hnr: str = str(hnr) self._zusatz: str = str(zusatz) + self._ladeort: str | None = ladeort self._ics = ICS() def fetch(self): @@ -94,44 +106,43 @@ class Source: r = requests.post(API_URL, data=args) r.raise_for_status() - soup = BeautifulSoup(r.text, "html.parser") - # find all ICAL download buttons - download_buttons = soup.find_all("button", {"name": "ical_apple"}) + ladeort_single = soup.find("input", {"id": "ladeort"}) - if not download_buttons: - raise Exception( - "Invalid response from server, check you configuration if it is correct." - ) + if not ladeort_single: + ladeort_select = soup.find("select", {"id": "ladeort"}) + if not ladeort_select: + raise Exception("No ladeort found") + ladeort_options = ladeort_select.find_all("option") + if not self._ladeort: + raise Exception( + f"Ladeort required for this address, use one of {[ladeort_option.text for ladeort_option in ladeort_options]}" + ) + for ladeort_option in ladeort_options: + if ladeort_option.text.lower().replace( + " ", "" + ) == self._ladeort.lower().replace(" ", ""): + ladeort_single = ladeort_option + break + if not ladeort_single: + raise Exception( + f"Ladeort not found: {self._ladeort}, use one of {[ladeort_option.text for ladeort_option in ladeort_options]}" + ) + del args["anzeigen"] + args["ladeort"] = ladeort_single["value"] + args["ical"] = "ICAL Jahresübersicht" + + r = requests.post(API_URL, data=args) + r.raise_for_status() + r.encoding = "utf-8" + try: + dates = self._ics.convert(r.text) + except ValueError as e: + raise Exception("got invalid ics file") from e entries = [] - - for button in download_buttons: - # get form data and request ICAL file for every waste type - args = {} - args["ical_apple"] = button["value"] - form = button.parent - for input in form.find_all("input"): - args[input["name"]] = input["value"] - - r = requests.post(API_URL, data=args) - r.encoding = "utf-8" - try: - dates = self._ics.convert(r.text) - except ValueError as e: - waste_type = ( - button["data-fraktionsname"] - if button.has_attr("data-fraktionsname") - else "unknown" - ) - waste_id = button["value"] - LOGGER.warning( - f"No data found for wastetype: {waste_type} with id {waste_id}. The Ical file is empty or corrupted. Failed with error: {e}" - ) - continue - - for d in dates: - bin_type = d[1].replace("Abfuhr", "").strip() - entries.append(Collection(d[0], bin_type, ICON_MAP.get(bin_type))) + for d in dates: + bin_type = d[1].replace("Abfuhr", "").strip() + entries.append(Collection(d[0], bin_type, ICON_MAP.get(bin_type))) return entries diff --git a/doc/source/aha_region_de.md b/doc/source/aha_region_de.md index a43d8c8b..d5ff4608 100644 --- a/doc/source/aha_region_de.md +++ b/doc/source/aha_region_de.md @@ -30,8 +30,13 @@ waste_collection_schedule: **zusatz** *(String | Integer) (optional)* +**ladeort** +*(String | Integer) (optional)* Only required if you need to specify a Abholpaltz on the website + ## Example +## Most addresses + ```yaml waste_collection_schedule: sources: @@ -44,6 +49,19 @@ waste_collection_schedule: ``` +## Addresses requirering Abholpaltz + +```yaml +waste_collection_schedule: + sources: + - name: aha_region_de + args: + gemeinde: Gehrden + strasse: Kirchstr. / Gehrden + hnr: "1" + ladeort: "Kirchstr. 6, Gehrden / Gehrden" +``` + ## How to get the source argument Find the parameter of your address using [https://www.aha-region.de/abholtermine/abfuhrkalender](https://www.aha-region.de/abholtermine/abfuhrkalender) and write them exactly like on the web page.