From feb756bf00fd68aa2afbf49104a0e05811f7ced0 Mon Sep 17 00:00:00 2001 From: mampfes Date: Sun, 19 Apr 2020 09:36:34 +0200 Subject: [PATCH] add source for AWBKoeln.de --- README.md | 1 + .../package/source/awbkoeln_de.py | 41 ++++++++++++++ .../package/wizard/awbkoeln_de.py | 53 +++++++++++++++++++ doc/source/awbkoeln_de.md | 41 ++++++++++++++ info.md | 1 + 5 files changed, 137 insertions(+) create mode 100644 custom_components/waste_collection_schedule/package/source/awbkoeln_de.py create mode 100755 custom_components/waste_collection_schedule/package/wizard/awbkoeln_de.py create mode 100644 doc/source/awbkoeln_de.md diff --git a/README.md b/README.md index 0ba72166..10138bec 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Currently the following service providers are supported: - [Abfall_Kreis_Tuebingen.de](./doc/source/abfall_kreis_tuebingen_de.md) - [AbfallNavi / RegioIT.de](./doc/source/regioit_de.md) - [AbfallPlus.de / Abfall.IO](./doc/source/abfall_io.md) +- [AWBKoeln.de](./doc/source/awbkoeln_de.md) - [Jumomind.de](./doc/source/jumomind_de.md) ## Configuration via configuration.yaml diff --git a/custom_components/waste_collection_schedule/package/source/awbkoeln_de.py b/custom_components/waste_collection_schedule/package/source/awbkoeln_de.py new file mode 100644 index 00000000..217911ec --- /dev/null +++ b/custom_components/waste_collection_schedule/package/source/awbkoeln_de.py @@ -0,0 +1,41 @@ +import requests +import json +import datetime +from collections import OrderedDict + +from ..helpers import CollectionAppointment + + +DESCRIPTION = "Source for AWB Koeln." +URL = "https://www.awbkoeln.de" +TEST_CASES = OrderedDict([("Koeln", {"street_code": 2, "building_number": 50})]) + + +class Source: + def __init__(self, street_code, building_number): + self._street_code = street_code + self._building_number = building_number + + def fetch(self): + args = { + "street_code": self._street_code, + "building_number": self._building_number, + } + + now = datetime.datetime.now() + args["start_year"] = now.year + args["start_month"] = now.month + args["end_year"] = now.year + 1 + args["end_month"] = now.month + + # get json file + r = requests.get(f"https://www.awbkoeln.de/api/calendar", params=args) + + data = json.loads(r.text) + + entries = [] + for d in data["data"]: + date = datetime.date(year=d["year"], month=d["month"], day=d["day"]) + entries.append(CollectionAppointment(date, d["type"])) + + return entries diff --git a/custom_components/waste_collection_schedule/package/wizard/awbkoeln_de.py b/custom_components/waste_collection_schedule/package/wizard/awbkoeln_de.py new file mode 100755 index 00000000..ec87a0b8 --- /dev/null +++ b/custom_components/waste_collection_schedule/package/wizard/awbkoeln_de.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 + +import inquirer +import requests +import json + + +def main(): + questions = [ + inquirer.Text("street_name", message="Enter street name (partial)"), + inquirer.Text("building_number", message="Enter building number (partial)"), + ] + answers = inquirer.prompt(questions) + + args = answers + args["form"] = "json" + r = requests.get(f"https://www.awbkoeln.de/api/streets", params=args) + + # "data":[{"street_name":"Bahnhofplatz","building_number":"5","building_number_plain":"5","building_number_addition":"","street_code":"4270", + # "district":"Gremberghoven","zipcode":"51149","district_code":"4","area_code":"7","user_street_name":"Bahnhofplatz","user_building_number":"1"} + data = json.loads(r.text) + if len(data["data"]) == 0: + print("no matching address found") + return + + choices = [] + for d in data["data"]: + value = { + "street_code": d["street_code"], + "building_number": d["building_number"], + } + choices.append( + ( + f"{d['user_street_name']} {d['user_building_number']}, {d['zipcode']} Köln - {d['district']}", + value, + ) + ) + + questions = [inquirer.List("data", choices=choices, message="Select address")] + answers = inquirer.prompt(questions) + + print("Copy the following statements into your configuration.yaml:\n") + print("# waste_collection_schedule source configuration") + print("waste_collection_schedule:") + print(" sources:") + print(" - name: awbkoeln_de") + print(" args:") + print(f" street_code: {answers['data']['street_code']}") + print(f" building_number: {answers['data']['building_number']}") + + +if __name__ == "__main__": + main() diff --git a/doc/source/awbkoeln_de.md b/doc/source/awbkoeln_de.md new file mode 100644 index 00000000..d8d3057b --- /dev/null +++ b/doc/source/awbkoeln_de.md @@ -0,0 +1,41 @@ +# AWBKoeln.de + +Add support for schedules provided by `AWBKoeln.de`. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: awbkoeln_de + args: + street_code: STREET_CODE + building_number: BUILDING_NUMBER +``` + +### Configuration Variables + +**street_code**
+*(string) (required)* + +**building_number**
+*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: awbkoeln_de + args: + street_code: 4272 + building_number: 10 +``` + +## 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/package/wizard/awbkoeln_de.py](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/custom_components/waste_collection_schedule/package/wizard/awbkoeln_de.py). + +Just run this script from a shell and answer the questions. diff --git a/info.md b/info.md index 9f057e70..e3f608af 100644 --- a/info.md +++ b/info.md @@ -31,4 +31,5 @@ Currently the following service providers are supported: - Abfall_Kreis_Tuebingen.de - AbfallNavi / RegioIT.de - AbfallPlus.de / Abfall.IO +- AWBKoeln.de - Jumomind.de