Skip to content

Commit

Permalink
add veldhoven and days_unit attr to the next_date sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram van Dartel committed Jan 8, 2024
1 parent c4ad47f commit aeeca12
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ _Component to integrate with the following providers._
| sudwestfryslan |
| suez |
| twentemilieu |
| veldhoven |
| venray |
| voorschoten |
| waalre |
Expand Down
28 changes: 17 additions & 11 deletions custom_components/afvalwijzer/collector/icalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ 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,
postal_code,
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:
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions custom_components/afvalwijzer/common/main_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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!")
5 changes: 3 additions & 2 deletions custom_components/afvalwijzer/const/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion custom_components/afvalwijzer/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
10 changes: 9 additions & 1 deletion custom_components/afvalwijzer/sensor_custom.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
Expand All @@ -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")
12 changes: 6 additions & 6 deletions custom_components/afvalwijzer/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit aeeca12

Please sign in to comment.