From 367c5af7a6603e9cd832a93cc5f45e29542750cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20G=C3=A4rdebrand?= Date: Sat, 9 Jul 2022 12:32:32 +0000 Subject: [PATCH 1/3] Add SSAM source --- .../source/ssam_se.py | 62 +++++++++++++++++++ doc/source/ssam_se.md | 34 ++++++++++ 2 files changed, 96 insertions(+) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py create mode 100644 doc/source/ssam_se.md diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py new file mode 100644 index 000000000..f8cda4710 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py @@ -0,0 +1,62 @@ +# coding: utf-8 +from datetime import datetime +import json +from urllib.parse import urlencode + +import requests +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +TITLE = "Lerum Vatten och Avlopp" +DESCRIPTION = "Source for Lerum Vatten och Avlopp waste collection." +URL = "https://edpfuture.ssam.se/FutureWeb/SimpleWastePickup/SimpleWastePickup" +TEST_CASES = { + # "SSAM": {"street_address": "Stinavägen 3, Växjö"}, + # "Polisen": {"street_address": "Sandgärdsgatan 31, Växjö"}, + "Ekliden": {"street_address": "Kalvsvik Ekliden, Kalvsvik"}, +} + + +class Source: + def __init__(self, street_address): + self._street_address = street_address + + def fetch(self): + response = requests.post( + "https://edpfuture.ssam.se/FutureWeb/SimpleWastePickup/SearchAdress", + {"searchText": self._street_address}, + ) + + address_data = json.loads(response.text) + address = None + if address_data["Succeeded"] and address_data["Succeeded"] is True: + if address_data["Buildings"] and len(address_data["Buildings"]) > 0: + address = address_data["Buildings"][0] + + print(address) + + if not address: + return [] + + query_params = urlencode({"address": address}) + response = requests.get( + "https://edpfuture.ssam.se/FutureWeb/SimpleWastePickup/GetWastePickupSchedule?{}".format( + query_params + ) + ) + data = json.loads(response.text) + + entries = [] + for item in data["RhServices"]: + waste_type = item["WasteType"] + icon = "mdi:trash-can" + if waste_type == "Matavfall": + icon = "mdi:leaf" + next_pickup = item["NextWastePickup"] + try: + next_pickup_date = datetime.fromisoformat(next_pickup).date() + except ValueError as e: + next_pickup_date = datetime.strptime(next_pickup, "%b %Y").date() + + entries.append(Collection(date=next_pickup_date, t=waste_type, icon=icon)) + + return entries diff --git a/doc/source/ssam_se.md b/doc/source/ssam_se.md new file mode 100644 index 000000000..e452a6fa6 --- /dev/null +++ b/doc/source/ssam_se.md @@ -0,0 +1,34 @@ +# SSAM + +TODO! + +Support for schedules provided by [Lerum Vatten och Avlopp](https://vatjanst.lerum.se/FutureWeb/SimpleWastePickup/SimpleWastePickup), serving the municipality of Lerum, Sweden. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: lerum_se + args: + street_address: STREET_ADDRESS +``` + +### Configuration Variables + +**street_address**
+*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: lerum_se + args: + street_address: Götebordsvägen 16, Lerum +``` + +## 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://vatjanst.lerum.se/FutureWeb/SimpleWastePickup/SimpleWastePickup). From 4631d0017a6a7bfc5f35cd4785ec5fb2097cb34b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20G=C3=A4rdebrand?= Date: Sat, 9 Jul 2022 12:39:32 +0000 Subject: [PATCH 2/3] Add SSAM source --- .../waste_collection_schedule/source/ssam_se.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py index f8cda4710..789cc6c44 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py @@ -6,13 +6,12 @@ import requests from waste_collection_schedule import Collection # type: ignore[attr-defined] -TITLE = "Lerum Vatten och Avlopp" -DESCRIPTION = "Source for Lerum Vatten och Avlopp waste collection." +TITLE = "SSAM" +DESCRIPTION = "Source for SSAM waste collection." URL = "https://edpfuture.ssam.se/FutureWeb/SimpleWastePickup/SimpleWastePickup" TEST_CASES = { - # "SSAM": {"street_address": "Stinavägen 3, Växjö"}, - # "Polisen": {"street_address": "Sandgärdsgatan 31, Växjö"}, - "Ekliden": {"street_address": "Kalvsvik Ekliden, Kalvsvik"}, + "SSAM": {"street_address": "Stinavägen 3, Växjö"}, + "Polisen": {"street_address": "Sandgärdsgatan 31, Växjö"}, } From d222cc5046f84ab38a4a45ca8a132d7d57326ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20G=C3=A4rdebrand?= Date: Sat, 9 Jul 2022 18:09:56 +0000 Subject: [PATCH 3/3] Handle date references that are months, not days --- .../waste_collection_schedule/source/ssam_se.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py index 789cc6c44..9a3baffb6 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/ssam_se.py @@ -1,6 +1,7 @@ # coding: utf-8 from datetime import datetime import json +import logging from urllib.parse import urlencode import requests @@ -13,6 +14,7 @@ "SSAM": {"street_address": "Stinavägen 3, Växjö"}, "Polisen": {"street_address": "Sandgärdsgatan 31, Växjö"}, } +_LOGGER = logging.getLogger(__name__) class Source: @@ -51,10 +53,17 @@ def fetch(self): if waste_type == "Matavfall": icon = "mdi:leaf" next_pickup = item["NextWastePickup"] + try: next_pickup_date = datetime.fromisoformat(next_pickup).date() except ValueError as e: - next_pickup_date = datetime.strptime(next_pickup, "%b %Y").date() + # In some cases the date is just a month, so parse this as the + # first of the month to atleast get something close + try: + next_pickup_date = datetime.strptime(next_pickup, "%b %Y").date() + except ValueError as e: + _LOGGER.warn(f"Failed to parse date {next_pickup} {str(e)}") + continue entries.append(Collection(date=next_pickup_date, t=waste_type, icon=icon))