From aeeca12dce5739f39a18cb37c2f8037f313e6a73 Mon Sep 17 00:00:00 2001 From: Bram van Dartel Date: Mon, 8 Jan 2024 09:51:46 +0100 Subject: [PATCH] add veldhoven and days_unit attr to the next_date sensor --- README.md | 1 + .../afvalwijzer/collector/icalendar.py | 28 +++++++++++-------- .../afvalwijzer/common/main_functions.py | 9 +++--- custom_components/afvalwijzer/const/const.py | 5 ++-- custom_components/afvalwijzer/manifest.json | 2 +- .../afvalwijzer/sensor_custom.py | 10 ++++++- .../afvalwijzer/tests/test_module.py | 12 ++++---- 7 files changed, 42 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index e850410..a67583c 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ _Component to integrate with the following providers._ | sudwestfryslan | | suez | | twentemilieu | +| veldhoven | | venray | | voorschoten | | waalre | diff --git a/custom_components/afvalwijzer/collector/icalendar.py b/custom_components/afvalwijzer/collector/icalendar.py index f0acd0f..bea9b07 100644 --- a/custom_components/afvalwijzer/collector/icalendar.py +++ b/custom_components/afvalwijzer/collector/icalendar.py @@ -18,6 +18,8 @@ def get_waste_data_raw( DATE_PATTERN = re.compile(r"^\d{8}") + TODAY = datetime.now() + YEAR_CURRENT = TODAY.year try: url = SENSOR_COLLECTORS_ICALENDAR[provider].format( provider, @@ -25,6 +27,7 @@ def get_waste_data_raw( street_number, suffix, datetime.now().strftime("%Y-%m-%d"), + YEAR_CURRENT, ) raw_response = requests.get(url, timeout=60, verify=False) except requests.exceptions.RequestException as err: @@ -40,28 +43,31 @@ def get_waste_data_raw( return waste_data_raw = [] - date = None - type = None + waste_date = None + waste_type = None for line in response.splitlines(): - key, value = line.split(":", 2) + if provider == "veldhoven": + key, value = line.split(":", 1) + else: + key, value = line.split(":", 2) field = key.split(";")[0] if field == "BEGIN" and value == "VEVENT": - date = None - type = None + waste_date = None + waste_type = None elif field == "SUMMARY": - type = value.strip().lower() + waste_type = _waste_type_rename(value.strip().lower()) elif field == "DTSTART": if DATE_PATTERN.match(value): - date = f"{value[:4]}-{value[4:6]}-{value[6:8]}" + waste_date = f"{value[:4]}-{value[4:6]}-{value[6:8]}" else: - _LOGGER.debug(f"Unsupported date format: {value}") + _LOGGER.debug(f"Unsupported waste_date format: {value}") elif field == "END" and value == "VEVENT": - if date and type: - waste_data_raw.append({"type": type, "date": date}) + if waste_date and waste_type: + waste_data_raw.append({"type": waste_type, "date": waste_date}) else: _LOGGER.debug( - f"No date or type extracted from event: date={date}, type={type}" + f"No waste_date or waste_type extracted from event: waste_date={waste_date}, waste_type={waste_type}" ) return waste_data_raw diff --git a/custom_components/afvalwijzer/common/main_functions.py b/custom_components/afvalwijzer/common/main_functions.py index 626ff35..343b021 100644 --- a/custom_components/afvalwijzer/common/main_functions.py +++ b/custom_components/afvalwijzer/common/main_functions.py @@ -5,14 +5,17 @@ def _waste_type_rename(item_name): "best_bag": "best-tas", "bulklitter": "grofvuil", "bulkygardenwaste": "tuinafval", + "chemokar": "chemisch", "christmas_trees": "kerstbomen", "gemengde plastics": "plastic", "glass": "glas", "green": "gft", + "groene container": "gft", "groente": "gft", "groente-, fruit en tuinafval": "gft", "groente, fruit- en tuinafval": "gft", "grey": "restafval", + "grijze container": "restafval", "kca": "chemisch", "kerstb": "kerstboom", "kerstboom": "kerstbomen", @@ -22,11 +25,13 @@ def _waste_type_rename(item_name): "paper": "papier", "pdb": "pmd", "papier en karton": "papier", + "papierinzameling": "papier", "plastic": "plastic", "plastic, blik & drinkpakken arnhem": "pmd", "plastic, blik & drinkpakken overbetuwe": "pmd", "pmd": "pmd", "pmdrest": "pmd-restafval", + "pmd-zak": "pmd", "pruning_waste": "snoeiafval", "remainder": "restwagen", "residual_waste": "restafval", @@ -41,7 +46,3 @@ def _waste_type_rename(item_name): } return waste_mapping.get(item_name, item_name) - - -if __name__ == "__main__": - print("Yell something at a mountain!") diff --git a/custom_components/afvalwijzer/const/const.py b/custom_components/afvalwijzer/const/const.py index 0c3d13a..fe6355b 100755 --- a/custom_components/afvalwijzer/const/const.py +++ b/custom_components/afvalwijzer/const/const.py @@ -5,7 +5,7 @@ API = "api" NAME = "afvalwijzer" -VERSION = "2024.01.02" +VERSION = "2024.01.03" ISSUE_URL = "https://github.com/xirixiz/homeassistant-afvalwijzer/issues" SENSOR_COLLECTOR_TO_URL = { @@ -56,7 +56,8 @@ } SENSOR_COLLECTORS_ICALENDAR = { - "eemsdelta": "https://www.eemsdelta.nl/trash-calendar/download/{1}/{2}", + "eemsdelta": "[https://www.eemsdelta.nl/trash-calendar/download/{1}/{2}]", + "veldhoven": "[https://www.veldhoven.nl/afvalkalender/{5}/{1}-{2}.ics]", } SENSOR_COLLECTORS_AFVALWIJZER = [ diff --git a/custom_components/afvalwijzer/manifest.json b/custom_components/afvalwijzer/manifest.json index 1984dcc..612787c 100644 --- a/custom_components/afvalwijzer/manifest.json +++ b/custom_components/afvalwijzer/manifest.json @@ -10,5 +10,5 @@ "iot_class": "cloud_polling", "issue_tracker": "https://github.com/xirixiz/homeassistant-afvalwijzer/issues", "requirements": [], - "version": "2024.01.02" + "version": "2024.01.03" } \ No newline at end of file diff --git a/custom_components/afvalwijzer/sensor_custom.py b/custom_components/afvalwijzer/sensor_custom.py index fb07fc1..e1053cd 100755 --- a/custom_components/afvalwijzer/sensor_custom.py +++ b/custom_components/afvalwijzer/sensor_custom.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from datetime import datetime +from datetime import date, datetime import hashlib from homeassistant.helpers.entity import Entity @@ -9,6 +9,7 @@ _LOGGER, ATTR_LAST_UPDATE, ATTR_YEAR_MONTH_DAY_DATE, + ATTR_DAYS_UNTIL_COLLECTION_DATE, CONF_DEFAULT_LABEL, CONF_ID, CONF_POSTAL_CODE, @@ -30,6 +31,7 @@ def __init__(self, hass, waste_type, fetch_data, config): self._id_name = self.config.get(CONF_ID) self._default_label = self.config.get(CONF_DEFAULT_LABEL) self._last_update = None + self._days_until_collection_date = None self._name = ( SENSOR_PREFIX + (f"{self._id_name} " if len(self._id_name) > 0 else "") ) + self.waste_type @@ -65,6 +67,7 @@ def extra_state_attributes(self): return { ATTR_LAST_UPDATE: self._last_update, ATTR_YEAR_MONTH_DAY_DATE: self._year_month_day_date, + ATTR_DAYS_UNTIL_COLLECTION_DATE: self._days_until_collection_date, } else: return { @@ -89,6 +92,10 @@ async def async_update(self): collection_date_us = waste_data_custom[self.waste_type].date() self._year_month_day_date = str(collection_date_us) + # Add the days until the collection date + delta = collection_date_us - date.today() + self._days_until_collection_date = delta.days + # Add the NL date format as default state self._state = datetime.strftime( waste_data_custom[self.waste_type].date(), "%d-%m-%Y" @@ -102,5 +109,6 @@ async def async_update(self): except ValueError: _LOGGER.debug("ValueError AfvalwijzerCustomSensor - unable to set value!") self._state = self._default_label + self._days_until_collection_date = None self._year_month_day_date = None self._last_update = datetime.now().strftime("%d-%m-%Y %H:%M") diff --git a/custom_components/afvalwijzer/tests/test_module.py b/custom_components/afvalwijzer/tests/test_module.py index 28ab2e6..202f5dc 100644 --- a/custom_components/afvalwijzer/tests/test_module.py +++ b/custom_components/afvalwijzer/tests/test_module.py @@ -38,9 +38,9 @@ # street_number = "2" # Afvalwijzer -# provider = "mijnafvalwijzer" -# postal_code = "5146eg" -# street_number = "1" +provider = "mijnafvalwijzer" +postal_code = "5146eg" +street_number = "1" # Afvalwijzer # provider = "mijnafvalwijzer" @@ -105,9 +105,9 @@ # street_number = "11" # Circulus -provider = "circulus" -postal_code = "7421AC" -street_number = "1" +#provider = "circulus" +#postal_code = "7421AC" +#street_number = "1" # postal_code = postal_code.strip().upper()