Repository: mampfes/hacs_waste_collection_schedule

Issue:
[Bug]: Abfallnavide is not working for Unna (Germany)
### I Have A Problem With:

A specific source

### What's Your Problem

I was trying to add the City of Unna (Germany) as a source to AbfallnaviDe. HA wasn't able to fetch the data. The service url seems to be wrong for this city.

If I change the service url in 'waste_collection_schedule/service/AbfallnaviDe.py' line 140
from "https://{service_domain}-abfallapp.regioit.de/abfall-app-{service_domain}/rest"
to "https://abfallapp.regioit.de/abfall-app-{service_domain}/rest"
everything is working as expected.

The service domain is 'unna'.

Here is a url to generate testdata, if needed
https://abfallnavi.de/unna/



### Source (if relevant)

AbfallnaviDe

### Logs

```Shell
2023-12-22 11:08:59.419 ERROR (SyncWorker_2) [waste_collection_schedule.source_shell] fetch failed for source AbfallNavi (RegioIT.de):
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/urllib3/util/connection.py", line 72, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/socket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno -2] Name does not resolve

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 715, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 404, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 1058, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.11/site-packages/urllib3/connection.py", line 363, in connect
    self.sock = conn = self._new_conn()
                       ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/urllib3/connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x7fdf9ae1c810>: Failed to establish a new connection: [Errno -2] Name does not resolve

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/requests/adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 799, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='unna-abfallapp.regioit.de', port=443): Max retries exceeded with url: /abfall-app-unna/rest/orte (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fdf9ae1c810>: Failed to establish a new connection: [Errno -2] Name does not resolve'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/config/custom_components/waste_collection_schedule/waste_collection_schedule/source_shell.py", line 134, in fetch
    entries = self._source.fetch()
              ^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/waste_collection_schedule/waste_collection_schedule/source/abfallnavi_de.py", line 56, in fetch
    dates = self._api.get_dates(self._ort, self._strasse, self._hausnummer)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/waste_collection_schedule/waste_collection_schedule/service/AbfallnaviDe.py", line 228, in get_dates
    city_id = self.get_city_id(city)
              ^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/waste_collection_schedule/waste_collection_schedule/service/AbfallnaviDe.py", line 160, in get_city_id
    cities = self.get_cities()
             ^^^^^^^^^^^^^^^^^
  File "/config/custom_components/waste_collection_schedule/waste_collection_schedule/service/AbfallnaviDe.py", line 152, in get_cities
    cities = self._fetch_json("orte")
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/waste_collection_schedule/waste_collection_schedule/service/AbfallnaviDe.py", line 148, in _fetch_json
    return json.loads(self._fetch(path, params=params))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/waste_collection_schedule/waste_collection_schedule/service/AbfallnaviDe.py", line 143, in _fetch
    r = requests.get(f"{self._service_url}/{path}", params=params)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/requests/adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='unna-abfallapp.regioit.de', port=443): Max retries exceeded with url: /abfall-app-unna/rest/orte (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fdf9ae1c810>: Failed to establish a new connection: [Errno -2] Name does not resolve'))
```


### Relevant Configuration

```YAML
waste_collection_schedule:
  sources:
    - name: abfallnavi_de
      args:
        service: unna
        ort: Unna
        strasse: XXXXstraße
        hausnummer: XX
```


### Checklist Source Error

- [X] Use the example parameters for your source (often available in the documentation) (don't forget to restart Home Assistant after changing the configuration)
- [X] Checked that the website of your service provider is still working
- [X] Tested my attributes on the service provider website (if possible)
- [X] I have tested with the latest version of the integration (master) (for HACS in the 3 dot menu of the integration click on "Redownload" and choose master as version)

### Checklist Sensor Error

- [X] Checked in the Home Assistant Calendar tab if the event names match the types names (if types argument is used)

### Required

- [X] I have searched past (closed AND opened) issues to see if this bug has already been reported, and it hasn't been.
- [X] I understand that people give their precious time for free, and thus I've done my very best to make this problem as easy as possible to investigate.


Discussion:

Resolving patch:
diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/service/AbfallnaviDe.py b/custom_components/waste_collection_schedule/waste_collection_schedule/service/AbfallnaviDe.py
--- a/custom_components/waste_collection_schedule/waste_collection_schedule/service/AbfallnaviDe.py
+++ b/custom_components/waste_collection_schedule/waste_collection_schedule/service/AbfallnaviDe.py
@@ -138,9 +138,16 @@ class AbfallnaviDe:
     def __init__(self, service_domain):
         self._service_domain = service_domain
         self._service_url = f"https://{service_domain}-abfallapp.regioit.de/abfall-app-{service_domain}/rest"
+        self._service_url_fallback = (
+            f"https://abfallapp.regioit.de/abfall-app-{service_domain}/rest"
+        )
 
     def _fetch(self, path, params=None):
-        r = requests.get(f"{self._service_url}/{path}", params=params)
+        try:
+            r = requests.get(f"{self._service_url}/{path}", params=params)
+        except requests.exceptions.ConnectionError:
+            self._service_url = self._service_url_fallback
+            r = requests.get(f"{self._service_url}/{path}", params=params)
         r.encoding = "utf-8"  # requests doesn't guess the encoding correctly
         return r.text
 
diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/abfallnavi_de.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/abfallnavi_de.py
--- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/abfallnavi_de.py
+++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/abfallnavi_de.py
@@ -38,6 +38,11 @@ def EXTRA_INFO():
         "ort": "Norderstedt",
         "strasse": "Distelweg",
     },
+    "una Bergkamen, Agnes-Miegel-Str.": {
+        "service": "unna",
+        "ort": "Bergkamen",
+        "strasse": "Agnes-Miegel-Str.",
+    },
 }
 
 


Test patch: