From fae5394e75a90c466db6fffb4304d0cb13dd658c Mon Sep 17 00:00:00 2001 From: Milan Date: Tue, 17 Dec 2024 15:15:26 +0100 Subject: [PATCH] Initial RWM support (#298) --- README.md | 1 + .../afvalwijzer/collector/main_collector.py | 11 +++- .../afvalwijzer/collector/rwm.py | 56 +++++++++++++++++++ .../afvalwijzer/common/main_functions.py | 2 + custom_components/afvalwijzer/const/const.py | 5 ++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 custom_components/afvalwijzer/collector/rwm.py diff --git a/README.md b/README.md index 1bd140f..5a6da27 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ _Component to integrate with the following providers._ | rd4 | | rova | | rmn | +| rwm | | schouwenand | | spaarnelanden | | sudwestfryslan | diff --git a/custom_components/afvalwijzer/collector/main_collector.py b/custom_components/afvalwijzer/collector/main_collector.py index 8574ad7..5f1ea96 100644 --- a/custom_components/afvalwijzer/collector/main_collector.py +++ b/custom_components/afvalwijzer/collector/main_collector.py @@ -8,11 +8,11 @@ SENSOR_COLLECTORS_ICALENDAR, SENSOR_COLLECTORS_OPZET, SENSOR_COLLECTORS_RD4, - SENSOR_COLLECTORS_XIMMIO, + SENSOR_COLLECTORS_XIMMIO ) try: - from . import burgerportaal, circulus, deafvalapp, icalendar, mijnafvalwijzer, opzet, rd4, ximmio + from . import burgerportaal, circulus, deafvalapp, icalendar, mijnafvalwijzer, opzet, rd4, rwm, ximmio except ImportError as err: _LOGGER.error(f"Import error {err.args}") @@ -101,6 +101,13 @@ def __init__( self.street_number, self.suffix, ) + elif provider == "rwm": + waste_data_raw = rwm.get_waste_data_raw( + self.provider, + self.postal_code, + self.street_number, + self.suffix, + ) else: _LOGGER.error(f"Unknown provider: {provider}") raise ValueError(f"Unknown provider: {provider}") diff --git a/custom_components/afvalwijzer/collector/rwm.py b/custom_components/afvalwijzer/collector/rwm.py new file mode 100644 index 0000000..1e12040 --- /dev/null +++ b/custom_components/afvalwijzer/collector/rwm.py @@ -0,0 +1,56 @@ +from ..const.const import _LOGGER, SENSOR_COLLECTORS_RWM +from ..common.main_functions import _waste_type_rename +from datetime import datetime +import requests +from urllib3.exceptions import InsecureRequestWarning + +requests.packages.urllib3.disable_warnings(InsecureRequestWarning) + + +def get_waste_data_raw(provider, postal_code, street_number, suffix): + try: + if provider != "rwm": + raise ValueError(f"Invalid provider: {provider}, please verify") + + ########################################################################## + # First request: get bag id + ########################################################################## + url = SENSOR_COLLECTORS_RWM["getAddress"].format(postal_code, street_number) + + response = requests.get(url=url, timeout=60).json() + + if not response: + _LOGGER.error("Address not found!") + return [] + + bagId = response[0]["bagid"] + + ########################################################################## + # Second request: get the dates + ########################################################################## + url = SENSOR_COLLECTORS_RWM["getSchedule"].format(bagId) + + response = requests.get(url=url, timeout=60).json() + + if not response: + _LOGGER.error("Could not retrieve trash schedule!") + return [] + + waste_data_raw = [] + + for item in response: + if item["ophaaldatum"] is not None: + data = { + "type": _waste_type_rename(item["title"].strip().lower()), + "date": datetime.strptime(item["ophaaldatum"], "%Y-%m-%d").strftime("%Y-%m-%d"), + } + waste_data_raw.append(data) + + except requests.exceptions.RequestException as err: + raise ValueError(err) from err + + return waste_data_raw + + +if __name__ == "__main__": + print("Yell something at a mountain!") diff --git a/custom_components/afvalwijzer/common/main_functions.py b/custom_components/afvalwijzer/common/main_functions.py index ad9b3a6..868bb18 100644 --- a/custom_components/afvalwijzer/common/main_functions.py +++ b/custom_components/afvalwijzer/common/main_functions.py @@ -10,11 +10,13 @@ def _waste_type_rename(item_name): "gemengde plastics": "plastic", "gft & etensresten": "gft", "glass": "glas", + "gft afval": "gft", "green": "gft", "groene container": "gft", "groente": "gft", "groente-, fruit en tuinafval": "gft", "groente, fruit- en tuinafval": "gft", + "grof tuinafval": "takken", "grey": "restafval", "grijze container": "restafval", "kca": "chemisch", diff --git a/custom_components/afvalwijzer/const/const.py b/custom_components/afvalwijzer/const/const.py index ca4313c..901b20f 100644 --- a/custom_components/afvalwijzer/const/const.py +++ b/custom_components/afvalwijzer/const/const.py @@ -105,6 +105,11 @@ "deafvalapp": "https://dataservice.deafvalapp.nl/dataservice/DataServiceServlet?service=OPHAALSCHEMA&land=NL&postcode={0}&straatId=0&huisnr={1}&huisnrtoev={2}", } +SENSOR_COLLECTORS_RWM = { + "getAddress": "https://rwm.nl/adressen/{0}:{1}", + "getSchedule": "https://rwm.nl/rest/adressen/{0}/afvalstromen" +} + CONF_COLLECTOR = "provider" CONF_API_TOKEN = "api_token" CONF_POSTAL_CODE = "postal_code"