Repository: mampfes/hacs_waste_collection_schedule

Issue:
Multiple Dutch waste management services use the same API structure
The code for [hvcgroep_nl](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/custom_components/waste_collection_schedule/waste_collection_schedule/source/hvcgroep_nl.py) should work with very few changes for multiple other Dutch waste management services, including (but not limited to) https://www.mijnblink.nl/wanneer and https://afvalkalender.cyclusnv.nl/.

Unfortunately I won't be able myself to make a code contribution for this anytime soon, but I thought I'd put the knowledge out here anyways for someone else :)


Discussion:

Resolving patch:
diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/hvcgroep_nl.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/hvcgroep_nl.py
--- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/hvcgroep_nl.py
+++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/hvcgroep_nl.py
@@ -1,4 +1,3 @@
-import json
 import logging
 from datetime import datetime
 
@@ -8,13 +7,52 @@
 TITLE = "HVCGroep"
 DESCRIPTION = "Source for the Dutch HVCGroep waste management."
 URL = "https://www.hvcgroep.nl/zelf-regelen/afvalkalender"
-TEST_CASES = {"Tollebeek": {"postal_code": "8309AV", "house_number": "1"}}
+TEST_CASES = {
+    "Tollebeek": {"postal_code": "8309AV", "house_number": "1"},
+    "Hvgroep: Tollebeek": {
+        "postal_code": "8309AV",
+        "house_number": "1",
+        "service": "hvcgroep",
+    },
+    "Cyclus": {"postal_code": "2841ML", "house_number": "1090", "service": "cyclusnv"},
+    "Mjinblink": {
+        "postal_code": "5741BV",
+        "house_number": "76",
+        "service": "mjinblink",
+    },
+}
 
 _LOGGER = logging.getLogger(__name__)
 
+SERVICE_MAP = {
+    "alphenaandenrijn": "https://afvalkalender.alphenaandenrijn.nl",
+    "cranendonck": "https://afvalkalender.cranendonck.nl",
+    "cyclusnv": "https://afvalkalender.cyclusnv.nl",
+    "dar": "https://afvalkalender.dar.nl",
+    "denhaag": "https://huisvuilkalender.denhaag.nl",
+    "gad": "https://inzamelkalender.gad.nl",
+    "gemeenteberkelland": "https://afvalkalender.gemeenteberkelland.nl",
+    "hvcgroep": "https://inzamelkalender.hvcgroep.nl",
+    "lingewaard": "https://afvalwijzer.lingewaard.nl",
+    "middelburgvlissingen": "https://afvalwijzer.middelburgvlissingen.nl",
+    "mijnblink": "https://mijnblink.nl",
+    "peelenmaas": "https://afvalkalender.peelenmaas.nl",
+    "prezero": "https://inzamelwijzer.prezero.nl",
+    "purmerend": "https://afvalkalender.purmerend.nl",
+    "rmn": "https://inzamelschema.rmn.nl",
+    "schouwen-duiveland": "https://afvalkalender.schouwen-duiveland.nl",
+    "spaarnelanden": "https://afvalwijzer.spaarnelanden.nl",
+    "stadswerk072": "https://www.stadswerk072.nl",
+    "sudwestfryslan": "https://afvalkalender.sudwestfryslan.nl",
+    "venray": "https://afvalkalender.venray.nl",
+    "voorschoten": "https://afvalkalender.voorschoten.nl",
+    "waalre": "https://afvalkalender.waalre.nl",
+    "zrd": "https://afvalkalender.zrd.nl",
+}
+
 
 class Source:
-    def __init__(self, postal_code, house_number):
+    def __init__(self, postal_code, house_number, service="hvcgroep"):
         self.postal_code = postal_code
         self.house_number = house_number
         self.icons = {
@@ -23,15 +61,14 @@ def __init__(self, postal_code, house_number):
             "papier-en-karton": "mdi:archive",
             "restafval": "mdi:trash-can",
         }
+        self._url = SERVICE_MAP[service]
 
     def fetch(self):
-        bag_id = 0
 
         # Retrieve bagid (unique waste management identifier)
-        r = requests.get(
-            f"https://inzamelkalender.hvcgroep.nl/adressen/{self.postal_code}:{self.house_number}"
-        )
-        data = json.loads(r.text)
+        r = requests.get(f"{self._url}/adressen/{self.postal_code}:{self.house_number}")
+        r.raise_for_status()
+        data = r.json()
 
         # Something must be wrong, maybe the address isn't valid? No need to do the extra requests so just return here.
         if len(data) == 0:
@@ -41,16 +78,14 @@ def fetch(self):
         bag_id = data[0]["bagid"]
 
         # Retrieve the details about different waste management flows (for example, paper, plastic etc.)
-        r = requests.get(
-            f"https://inzamelkalender.hvcgroep.nl/rest/adressen/{bag_id}/afvalstromen"
-        )
-        waste_flows = json.loads(r.text)
+        r = requests.get(f"{self._url}/rest/adressen/{bag_id}/afvalstromen")
+        r.raise_for_status()
+        waste_flows = r.json()
 
         # Retrieve the coming pickup dates for waste.
-        r = requests.get(
-            f"https://inzamelkalender.hvcgroep.nl/rest/adressen/{bag_id}/ophaaldata"
-        )
-        data = json.loads(r.text)
+        r = requests.get(f"{self._url}/rest/adressen/{bag_id}/ophaaldata")
+        r.raise_for_status()
+        data = r.json()
 
         entries = []
 


Test patch: