From cfc0546f3655aa334a8907e36cdec3223228a6e9 Mon Sep 17 00:00:00 2001 From: mampfes Date: Fri, 8 Oct 2021 16:42:11 +0200 Subject: [PATCH] refactor awsh_de Make wizard obsolete and allow to enter city and street directly --- README.md | 1 + .../waste_collection_schedule/source/awsh.py | 32 ------- .../source/awsh_de.py | 75 +++++++++++++++ .../waste_collection_schedule/wizard/awsh.py | 93 ------------------- doc/source/awsh.md | 42 --------- doc/source/awsh_de.md | 32 +++++++ info.md | 1 + 7 files changed, 109 insertions(+), 167 deletions(-) delete mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/awsh.py create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/awsh_de.py delete mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/wizard/awsh.py delete mode 100644 doc/source/awsh.md create mode 100644 doc/source/awsh_de.md diff --git a/README.md b/README.md index c06cccfd..bc2f3056 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ Currently the following service providers are supported: - [Abfall_Kreis_Tuebingen.de](./doc/source/abfall_kreis_tuebingen_de.md) - [AbfallNavi.de (RegioIT.de)](./doc/source/abfallnavi_de.md) - [Abfallwirtschaft Stuttgart](./doc/source/stuttgart_de.md) +- [Abfallwirtschaft Südholstein](./doc/source/awsh_de.md) - [Abfallwirtschaft Zollernalbkreis](./doc/source/abfall_zollernalbkreis_de.md) - [AWBKoeln.de](./doc/source/awbkoeln_de.md) - [AWIDO-online.de](./doc/source/awido_de.md) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/awsh.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/awsh.py deleted file mode 100644 index c96ea250..00000000 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/awsh.py +++ /dev/null @@ -1,32 +0,0 @@ -import requests -from waste_collection_schedule import Collection # type: ignore[attr-defined] -from waste_collection_schedule.service.ICS import ICS - -TITLE = "AWSH" -DESCRIPTION = "Source for Abfallwirtschaft Südstormarn" -URL = "https://www.awsh.de" -TEST_CASES = { - "Reinbek": {"ortId": 560, "strId": 860, "waste_types": "R02-B02-D02-P04"}, -} - -class Source: - def __init__(self, ortId, strId, waste_types=[]): - self.ortId = ortId - self.strId = strId - self.waste_types = waste_types - self.ics = ICS() - - def fetch(self): - - _waste_types = "-".join(map(lambda x: str(x), self.waste_types)) - # get ics file - r = requests.get(f"https://www.awsh.de/api_v2/collection_dates/1/ort/{self.ortId}/strasse/{self.strId}/hausnummern/0/abfallarten/{_waste_types}/kalender.ics") - return self.convert(r.text) - - def convert(self, data): - dates = self.ics.convert(data) - - entries = [] - for d in dates: - entries.append(Collection(d[0], d[1])) - return entries \ No newline at end of file diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/awsh_de.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/awsh_de.py new file mode 100644 index 00000000..ca9b61c3 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/awsh_de.py @@ -0,0 +1,75 @@ +import json +import logging + +import requests +from waste_collection_schedule import Collection # type: ignore[attr-defined] +from waste_collection_schedule.service.ICS import ICS + +TITLE = "AWSH" +DESCRIPTION = "Source for Abfallwirtschaft Südholstein" +URL = "https://www.awsh.de" +TEST_CASES = { + "Reinbek": {"city": "Reinbek", "street": "Ahornweg"}, +} + +_LOGGER = logging.getLogger(__name__) + + +class Source: + def __init__(self, city, street): + self._city = city + self._street = street + self._ics = ICS() + + def fetch(self): + # retrieve list of cities + r = requests.get("https://www.awsh.de/api_v2/collection_dates/1/orte") + cities = json.loads(r.text) + + # create city to id map from retrieved cities + city_to_id = { + city["ortsbezeichnung"]: city["ortsnummer"] for (city) in cities["orte"] + } + + if self._city not in city_to_id: + _LOGGER.error(f"city not found: {self._city}") + return [] + + cityId = city_to_id[self._city] + + # retrieve list of streets + r = requests.get( + f"https://www.awsh.de/api_v2/collection_dates/1/ort/{cityId}/strassen" + ) + streets = json.loads(r.text) + + # create street to id map from retrieved cities + street_to_id = { + street["strassenbezeichnung"]: street["strassennummer"] + for (street) in streets["strassen"] + } + + if self._street not in street_to_id: + _LOGGER.error(f"street not found: {self._street}") + return [] + + streetId = street_to_id[self._street] + + # retrieve list of waste types + r = requests.get( + f"https://www.awsh.de/api_v2/collection_dates/1/ort/{cityId}/abfallarten" + ) + waste_types = json.loads(r.text) + wt = "-".join([t["id"] for t in waste_types["abfallarten"]]) + + # get ics file + r = requests.get( + f"https://www.awsh.de/api_v2/collection_dates/1/ort/{cityId}/strasse/{streetId}/hausnummern/0/abfallarten/{wt}/kalender.ics" + ) + + dates = self._ics.convert(r.text) + + entries = [] + for d in dates: + entries.append(Collection(d[0], d[1])) + return entries diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/wizard/awsh.py b/custom_components/waste_collection_schedule/waste_collection_schedule/wizard/awsh.py deleted file mode 100644 index 6e0fd559..00000000 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/wizard/awsh.py +++ /dev/null @@ -1,93 +0,0 @@ -#!/usr/bin/env python3 - -import inquirer -import requests -import sys -import json - -def get_cities(): - r = requests.get("https://www.awsh.de/api_v2/collection_dates/1/orte") - return json.loads(r.text) - -def get_strassen(ort): - r = requests.get(f"https://www.awsh.de/api_v2/collection_dates/1/ort/{ort}/strassen") - return json.loads(r.text) - -def get_waste_types(ort): - r = requests.get(f"https://www.awsh.de/api_v2/collection_dates/1/ort/{ort}/abfallarten") - return json.loads(r.text) - -def main(): - ortsnummer = None - strassennummer = None - abfallarten = [] - - cities = get_cities() - choices = [] - for d in cities["orte"]: - value = { - "ortsnummer": d["ortsnummer"], - "ortsbezeichnung": d["ortsbezeichnung"], - "plz": d["plz"], - } - choices.append( - ( - f"{d['plz']} {d['ortsbezeichnung']}", - value, - ) - ) - questions = [inquirer.List("city", choices=choices, message="Select City")] - answers = inquirer.prompt(questions) - ortsnummer = answers["city"]["ortsnummer"] - strassen = get_strassen(ortsnummer) - choices.clear() - - for d in strassen["strassen"]: - value = { - "strassennummer": d["strassennummer"], - "strassenbezeichnung": d["strassenbezeichnung"], - } - choices.append( - ( - f"{d['strassennummer']} {d['strassenbezeichnung']}", - value, - ) - ) - - questions = [inquirer.List("address", choices=choices, message="Select address")] - answers = inquirer.prompt(questions) - strassennummer = answers["address"]["strassennummer"] - waste_types = get_waste_types(ortsnummer) - choices.clear() - - for d in waste_types["abfallarten"]: - value = { - "bezeichnung": d["bezeichnung"], - "zyklus": d["zyklus"], - "id": d["id"], - } - choices.append( - ( - f"{d['bezeichnung']} {d['zyklus']}", - value, - ) - ) - - questions = [inquirer.Checkbox("wastetypes", choices=choices, message="Select Types")] - answers = inquirer.prompt(questions) - for d in answers["wastetypes"]: - abfallarten.append(d["id"]) - - print("Copy the following statements into your configuration.yaml:\n") - print("# waste_collection_schedule source configuration") - print("waste_collection_schedule:") - print(" sources:") - print(" - name: awsh") - print(" args:") - print(f" ortId: {ortsnummer}") - print(f" strId: {strassennummer}") - for d in abfallarten: - print(f" waste_types: - {d}") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/doc/source/awsh.md b/doc/source/awsh.md deleted file mode 100644 index 8d8b6110..00000000 --- a/doc/source/awsh.md +++ /dev/null @@ -1,42 +0,0 @@ -# AWSH - -Support for schedules provided by [AWSH](https://www.awsh.de) - -## Configuration via configuration.yaml - -```yaml -waste_collection_schedule: - sources: - - name: awsh - args: - ortId: Ort ID - strId: Strassen ID - waste_types: - - R.. - - D.. - - B.. -``` - -## Example - -```yaml -waste_collection_schedule: - sources: - - name: awsh - args: - ortId: 560 - strId: 860 - waste_types: - - R02 - - B02 - - D02 - - P04 -``` - -## How to get the source arguments - -There is a script with an interactive command line interface which generates the required source configuration: - -[https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/custom_components/waste_collection_schedule/waste_collection_schedule/wizard/awsh.py](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/custom_components/waste_collection_schedule/waste_collection_schedule/wizard/awsh.py). - -Just run this script from a shell and answer the questions. \ No newline at end of file diff --git a/doc/source/awsh_de.md b/doc/source/awsh_de.md new file mode 100644 index 00000000..9a7e7da9 --- /dev/null +++ b/doc/source/awsh_de.md @@ -0,0 +1,32 @@ +# AWSH + +Support for schedules provided by [AWSH](https://www.awsh.de) + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: awsh_de + args: + city: CITY + street: STREET +``` +### Configuration Variables + +**city**
+*(string) (required)* + +**street**
+*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: awsh_de + args: + city: Reinbek + street: Ahornweg +``` \ No newline at end of file diff --git a/info.md b/info.md index afd6db69..8f0e7e7d 100644 --- a/info.md +++ b/info.md @@ -49,6 +49,7 @@ Currently the following service providers are supported: - [Abfall_Kreis_Tuebingen.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/abfall_kreis_tuebingen_de.md) - [AbfallNavi.de (RegioIT.de)](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/abfallnavi_de.md) - [Abfallwirtschaft Stuttgart](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/stuttgart_de.md) +- [Abfallwirtschaft Südholstein](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/awsh_de.md) - [Abfallwirtschaft Zollernalbkreis](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/abfall_zollernalbkreis_de.md) - [AWBKoeln.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/awbkoeln_de.md) - [AWIDO-online.de](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/awido_de.md)