refactor awsh_de

Make wizard obsolete and allow to enter city and street directly
This commit is contained in:
mampfes
2021-10-08 16:42:11 +02:00
parent c7d635034d
commit cfc0546f36
7 changed files with 109 additions and 167 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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()

View File

@@ -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.

32
doc/source/awsh_de.md Normal file
View File

@@ -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**<br>
*(string) (required)*
**street**<br>
*(string) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: awsh_de
args:
city: Reinbek
street: Ahornweg
```

View File

@@ -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)