From 2aa453438b349d0124cdea5074e3b0676184c173 Mon Sep 17 00:00:00 2001 From: Justas Malinauskas Date: Mon, 21 Nov 2022 22:00:20 +0200 Subject: [PATCH] =?UTF-8?q?Add=20support=20for=20grafikai.svara.lt=20(UAB?= =?UTF-8?q?=20Kauno=20=C5=A1vara)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 + .../source/grafikai_svara_lt.py | 112 ++++++++++++++++++ doc/source/grafikai_svara_lt.md | 63 ++++++++++ 3 files changed, 179 insertions(+) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/grafikai_svara_lt.py create mode 100644 doc/source/grafikai_svara_lt.md diff --git a/README.md b/README.md index 8bf478ac..30f6739f 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,10 @@ Currently the following service providers are supported: - [Umweltbetrieb Stadt Bielefeld](./doc/source/bielefeld_de.md) - [WAS Wolfsburg](./doc/source/was_wolfsburg_de.md) +### Lithuania + +- [Kauno švara](./doc/source/grafikai_svara_lt.md) + ### Netherlands - [Ximmio](./doc/source/ximmio_nl.md) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/grafikai_svara_lt.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/grafikai_svara_lt.py new file mode 100644 index 00000000..6992518d --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/grafikai_svara_lt.py @@ -0,0 +1,112 @@ +import json +from datetime import datetime + +import requests +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +TITLE = "Grafikai.svara.lt" +DESCRIPTION = "Source for UAB \"Kauno švara\"." +URL = "http://grafikai.svara.lt" +TEST_CASES = { + "Demokratų g. 7, Kaunas": { + "region": "Kauno m. sav.", + "street": "Demokratų g.", + "house_number": "7", + "waste_object_ids": [101358, 100858, 100860] + }, + + "Alytaus g. 2, Išlaužo k., Išlaužo sen. Prienų r. sav.": { + "region": "Prienų r. sav.", + "street": "Alytaus g.", + "house_number": "2", + "district": "Išlaužo sen.", + }, +} + +ICONS = { + "mišrių atliekų": "mdi:trash-can", + "antrinių žaliavų (popierius/plastikas)": "mdi:recycle", + "antrinių žaliavų (stiklas)": "mdi:glass-fragile", + "žaliųjų atliekų": "mdi:leaf", +} + + +class Source: + API_URL = "http://grafikai.svara.lt/api/" + + def __init__(self, region, street, house_number, district=None, waste_object_ids=None): + if waste_object_ids is None: + waste_object_ids = [] + self._region = region + self._street = street + self._house_number = house_number + self._district = district + self._waste_object_ids = waste_object_ids + + def fetch(self): + + address_query = { + "pageSize": 20, + "pageIndex": 0, + "address": self._street, + "region": self._region, + "houseNumber": self._house_number, + "subDistrict": self._district, + "matchHouseNumber": "true", + } + r = requests.get( + self.API_URL + "contracts", + params=address_query, + ) + + data = json.loads(r.text) + + self.check_for_error_status(data) + + entries = [] + + for collection in data["data"]: + try: + type = collection["descriptionPlural"].casefold() + if self.check_if_waste_object_defined(collection['wasteObjectId']): + waste_object_query = { + "wasteObjectId": collection['wasteObjectId'] + } + + rwo = requests.get( + self.API_URL + "schedule", + params=waste_object_query, + ) + data_waste_object = json.loads(rwo.text) + self.check_for_error_status(data_waste_object) + + for collection_waste_object in data_waste_object: + print(collection_waste_object["date"]) + entries.append( + Collection( + date=datetime.strptime( + collection_waste_object["date"], "%Y-%m-%dT%H:%M:%S" + ).date(), + t=collection["descriptionFmt"].title(), + icon=ICONS.get(type, "mdi:trash-can"), + ) + ) + except ValueError: + pass # ignore date conversion failure for not scheduled collections + + return entries + + def check_if_waste_object_defined(self, waste_object_id): + if len(self._waste_object_ids) <= 0: + return True + if waste_object_id in self._waste_object_ids: + return True + return False + + def check_for_error_status(self, collection): + if "status" in collection: + raise Exception( + "Error: failed to fetch get data, got status: {}".format( + collection['status'] + ) + ) diff --git a/doc/source/grafikai_svara_lt.md b/doc/source/grafikai_svara_lt.md new file mode 100644 index 00000000..3b256b08 --- /dev/null +++ b/doc/source/grafikai_svara_lt.md @@ -0,0 +1,63 @@ +# Kauno švara + +Support for schedules provided by [Kauno švara](https://svara.lt). + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: grafikai_svara_lt + args: + region: CITY + street: STREET_NAME + house_number: HOUSE_NUMBER + district: DISTRICT + waste_object_ids: + - WASTE_OBJECT_ID +``` + +### Configuration Variables + +**region**
+*(string) (required)* + +**street**
+*(string) (required)* + +**house_number**
+*(string) (required)* + +**district**
+*(string) (optional)* + +**waste_object_ids**
+*(list) (optional)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: grafikai_svara_lt + args: + region: Kauno m. sav. + street: Demokratų g. + house_number: 7 + district: DISTRICT + waste_object_ids: + - 101358 + - 100858 + - 100860 +``` + +## How to get the source arguments + +Visit the [Grafikai](http://grafikai.svara.lt) page and search for your address. The arguments should exactly match the following table below. To include waste object id's at search results page, copy url from "Atsisiųsti" button and take "wasteObjectId" parameter. + +| Parameter name in grafikai.svara.lt | Argument in yaml | +|-------------------------------------|------------------| +| Regionas | region | +| Gatvė | street | +| Namo nr. | house_number | +| Seniūnija | district |