From f60ab4129d526b756577c06b2637e831c52ee4c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20B=C3=A4cklund?= Date: Tue, 26 Mar 2024 11:42:25 +0100 Subject: [PATCH 1/8] =?UTF-8?q?Initial=20commit=20adding=20support=20for?= =?UTF-8?q?=20Kristianstad=20Renh=C3=A5llning=20Sweden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../source/krab_se.py | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py new file mode 100644 index 000000000..d580e3a06 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py @@ -0,0 +1,59 @@ +import json +from datetime import datetime + +import requests +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +TITLE = "Kristianstad Renhållning" +DESCRIPTION = "Source for Kristianstad Renhållning waste collection." +URL = "https://renhallningen-kristianstad.se" +TEST_CASES = { + "Case_0": {"street_address": "Östra Boulevarden 1"}, + "Case_1": {"street_address": "Grenadjärvägen 1"}, + "Case_2": {"street_address": "Skogsvägen 18"}, + "Case_3": {"street_address": "Önnestadsvägen 7"}, +} + + +class Source: + def __init__(self, street_address): + self._street_address = street_address + + def fetch(self): + params = [{"query": self._street_address}] + headers = {"Module": "universal", "Accept": "application/json, text/plain, */*", "Unit": "dd905ce7-b16d-4422-be36-564169af4035"} + response = requests.get( + "https://api-universal.appbolaget.se/waste/addresses/search", + headers=headers, + params=urlencode(params), + ) + + building_data = json.loads(response.text)["data"] + building_id = None + if building_data and len(building_data) > 0: + building_id = building_data[0]["uuid"] + + if not building_id: + return [] + + params = [building_id]; + headers = {"Module": "universal", "Accept": "application/json, text/plain, */*", "Unit": "dd905ce7-b16d-4422-be36-564169af4035"} + response = requests.get( + "https://api-universal.appbolaget.se/waste/addresses", + headers=headers, + params=urlencode(params), + ) + + data = json.loads(response.text)["services"] + + entries = [] + for item in data: + waste_type = item["type"] + icon = "mdi:trash-can" + if waste_type == "Trädgårdsavfall": + icon = "mdi:leaf" + next_pickup = item["collection_at"] + next_pickup_date = datetime.fromisoformat(next_pickup).date() + entries.append(Collection(date=next_pickup_date, t=waste_type, icon=icon)) + + return entries From d3de65ac06eb08aceb6d16425cbed33e4802b67b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20B=C3=A4cklund?= Date: Tue, 26 Mar 2024 11:42:39 +0100 Subject: [PATCH 2/8] =?UTF-8?q?Initial=20commit=20adding=20documentation?= =?UTF-8?q?=20for=20Kristianstad=20Renh=C3=A5llning=20Sweden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/source/krab_se.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 doc/source/krab_se.md diff --git a/doc/source/krab_se.md b/doc/source/krab_se.md new file mode 100644 index 000000000..290ab8071 --- /dev/null +++ b/doc/source/krab_se.md @@ -0,0 +1,32 @@ +# Kristianstad Renhållning Sophämntning + +Support for schedules provided by [Kristianstad Renhållning](https://renhallningen-kristianstad.se/villa-fritidshus/tomning-av-sopkarl/tomningschema/), serving the municipality of Kristianstad Sweden. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: krab_se + args: + street_address: STREET_ADDRESS +``` + +### Configuration Variables + +**street_address** +*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: krab_se + args: + street_address: Östra Boulevarden 1 +``` + +## How to get the source argument + +The source argument is the address to the house with waste collection. The address can be tested [here](https://renhallningen-kristianstad.se/villa-fritidshus/tomning-av-sopkarl/tomningschema/). From f7e82b67440ddae20dd5de77ca9e00ed7ad3cff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20B=C3=A4cklund?= Date: Mon, 23 Sep 2024 14:23:11 +0200 Subject: [PATCH 3/8] =?UTF-8?q?Updated=20readme.md=20with=20Renh=C3=A5llni?= =?UTF-8?q?ngen=20Kristianstad=20(Sweden)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0f57d3fa5..465e1edbc 100644 --- a/README.md +++ b/README.md @@ -1500,6 +1500,7 @@ If your service provider is not listed, feel free to open a [source request issu - [Gästrike Återvinnare](/doc/source/gastrikeatervinnare_se.md) / gastrikeatervinnare.se - [Jönköping - June Avfall & Miljö](/doc/source/juneavfall_se.md) / juneavfall.se - [Kretslopp Sydost](/doc/source/edpevent_se.md) / kretsloppsydost.se +- [Renhållningen Kristianstad](/doc/source/krab_se.md) / renhallningen-kristianstad.se/ - [Landskrona - Svalövs Renhållning](/doc/source/lsr_nu.md) / lsr.nu - [Lerum Vatten och Avlopp](/doc/source/lerum_se.md) / vatjanst.lerum.se - [Linköping - Tekniska Verken](/doc/source/tekniskaverken_se.md) / tekniskaverken.se From a95c895b238b79a4f5123c55c4ec0a14377aabac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20B=C3=A4cklund?= Date: Mon, 23 Sep 2024 14:30:46 +0200 Subject: [PATCH 4/8] Fix error with urlencode --- .../waste_collection_schedule/source/krab_se.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py index d580e3a06..1503d401b 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py @@ -1,4 +1,5 @@ import json +import urllib from datetime import datetime import requests @@ -25,7 +26,7 @@ def fetch(self): response = requests.get( "https://api-universal.appbolaget.se/waste/addresses/search", headers=headers, - params=urlencode(params), + params=urllib.parse.urlencode(params), ) building_data = json.loads(response.text)["data"] @@ -41,7 +42,7 @@ def fetch(self): response = requests.get( "https://api-universal.appbolaget.se/waste/addresses", headers=headers, - params=urlencode(params), + params=urllib.parse.urlencode(params), ) data = json.loads(response.text)["services"] From a80b3a7703ff7e39ea64e3117da2bfd679de067d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20B=C3=A4cklund?= Date: Wed, 25 Sep 2024 16:55:31 +0200 Subject: [PATCH 5/8] Fixed "Lints" --- .../waste_collection_schedule/source/krab_se.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py index 1503d401b..f054418ce 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py @@ -22,7 +22,9 @@ def __init__(self, street_address): def fetch(self): params = [{"query": self._street_address}] - headers = {"Module": "universal", "Accept": "application/json, text/plain, */*", "Unit": "dd905ce7-b16d-4422-be36-564169af4035"} + headers = {"Module": "universal", + "Accept": "application/json, text/plain, */*", + "Unit": "dd905ce7-b16d-4422-be36-564169af4035"} response = requests.get( "https://api-universal.appbolaget.se/waste/addresses/search", headers=headers, @@ -37,8 +39,10 @@ def fetch(self): if not building_id: return [] - params = [building_id]; - headers = {"Module": "universal", "Accept": "application/json, text/plain, */*", "Unit": "dd905ce7-b16d-4422-be36-564169af4035"} + params = [building_id] + headers = {"Module": "universal", + "Accept": "application/json, text/plain, */*", + "Unit": "dd905ce7-b16d-4422-be36-564169af4035"} response = requests.get( "https://api-universal.appbolaget.se/waste/addresses", headers=headers, From 7236cad22620db1dd66bf5d35defc7dc0d5d2b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20B=C3=A4cklund?= Date: Wed, 25 Sep 2024 18:12:48 +0200 Subject: [PATCH 6/8] Major fix due to diff in running code and repo --- .../waste_collection_schedule/source/krab_se.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py index f054418ce..2df807b1f 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py @@ -21,14 +21,13 @@ def __init__(self, street_address): self._street_address = street_address def fetch(self): - params = [{"query": self._street_address}] headers = {"Module": "universal", "Accept": "application/json, text/plain, */*", "Unit": "dd905ce7-b16d-4422-be36-564169af4035"} + query_street_address = urllib.parse.quote(self._street_address) response = requests.get( - "https://api-universal.appbolaget.se/waste/addresses/search", + f"https://api-universal.appbolaget.se/waste/addresses/search?query={query_street_address}", headers=headers, - params=urllib.parse.urlencode(params), ) building_data = json.loads(response.text)["data"] @@ -39,17 +38,15 @@ def fetch(self): if not building_id: return [] - params = [building_id] headers = {"Module": "universal", "Accept": "application/json, text/plain, */*", "Unit": "dd905ce7-b16d-4422-be36-564169af4035"} response = requests.get( - "https://api-universal.appbolaget.se/waste/addresses", + f"https://api-universal.appbolaget.se/waste/addresses/{building_id}", headers=headers, - params=urllib.parse.urlencode(params), ) - data = json.loads(response.text)["services"] + data = json.loads(response.text)["data"]["services"] entries = [] for item in data: @@ -61,4 +58,4 @@ def fetch(self): next_pickup_date = datetime.fromisoformat(next_pickup).date() entries.append(Collection(date=next_pickup_date, t=waste_type, icon=icon)) - return entries + return entries \ No newline at end of file From c78b0457cd3c20b49772f32ab88ea61e648b591e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20B=C3=A4cklund?= Date: Wed, 25 Sep 2024 19:04:15 +0200 Subject: [PATCH 7/8] Changing returning empty result on unknown street address to raising exception --- .../waste_collection_schedule/source/krab_se.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py index 2df807b1f..8ffefaec0 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py @@ -36,7 +36,7 @@ def fetch(self): building_id = building_data[0]["uuid"] if not building_id: - return [] + raise ValueError('Unknown street address') headers = {"Module": "universal", "Accept": "application/json, text/plain, */*", From 10a6f0874af219f0330c41f234df19f8662c4ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20B=C3=A4cklund?= Date: Mon, 30 Sep 2024 12:40:02 +0200 Subject: [PATCH 8/8] Rename from krab_se to renhallningen_kristianstad_se --- .../source/{krab_se.py => renhallningen_kristianstad_se.py} | 0 doc/source/{krab_se.md => renhallningen_kristianstad_se.md} | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename custom_components/waste_collection_schedule/waste_collection_schedule/source/{krab_se.py => renhallningen_kristianstad_se.py} (100%) rename doc/source/{krab_se.md => renhallningen_kristianstad_se.md} (90%) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/renhallningen_kristianstad_se.py similarity index 100% rename from custom_components/waste_collection_schedule/waste_collection_schedule/source/krab_se.py rename to custom_components/waste_collection_schedule/waste_collection_schedule/source/renhallningen_kristianstad_se.py diff --git a/doc/source/krab_se.md b/doc/source/renhallningen_kristianstad_se.md similarity index 90% rename from doc/source/krab_se.md rename to doc/source/renhallningen_kristianstad_se.md index 290ab8071..8e5d444e4 100644 --- a/doc/source/krab_se.md +++ b/doc/source/renhallningen_kristianstad_se.md @@ -7,7 +7,7 @@ Support for schedules provided by [Kristianstad Renhållning](https://renhallnin ```yaml waste_collection_schedule: sources: - - name: krab_se + - name: renhallningen_kristianstad_se args: street_address: STREET_ADDRESS ``` @@ -22,7 +22,7 @@ waste_collection_schedule: ```yaml waste_collection_schedule: sources: - - name: krab_se + - name: renhallningen_kristianstad_se args: street_address: Östra Boulevarden 1 ```