From e8a316f68a7d83c84e911c22741688cc8dbbcf6d Mon Sep 17 00:00:00 2001
From: 5ila5 <5ila5@users.noreply.github.com>
Date: Thu, 19 Sep 2024 15:18:18 +0200
Subject: [PATCH] ambervalley_gov_uk add predict argument, which tries to
predict collections further in the future
---
.../translations/de.json | 2 +
.../translations/en.json | 2 +
.../translations/it.json | 2 +
.../source/ambervalley_gov_uk.py | 80 +++++++++++++------
doc/source/ambervalley_gov_uk.md | 8 +-
5 files changed, 69 insertions(+), 25 deletions(-)
diff --git a/custom_components/waste_collection_schedule/translations/de.json b/custom_components/waste_collection_schedule/translations/de.json
index aee9582f..ddaa42e7 100644
--- a/custom_components/waste_collection_schedule/translations/de.json
+++ b/custom_components/waste_collection_schedule/translations/de.json
@@ -637,6 +637,7 @@
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/ambervalley_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
+ "predict": "Predict",
"uprn": "UPRN"
},
"data_description": {
@@ -649,6 +650,7 @@
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/ambervalley_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
+ "predict": "Predict",
"uprn": "UPRN"
},
"data_description": {
diff --git a/custom_components/waste_collection_schedule/translations/en.json b/custom_components/waste_collection_schedule/translations/en.json
index f1be3d4b..ad0d1f3b 100644
--- a/custom_components/waste_collection_schedule/translations/en.json
+++ b/custom_components/waste_collection_schedule/translations/en.json
@@ -637,6 +637,7 @@
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/ambervalley_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
+ "predict": "Predict",
"uprn": "UPRN"
},
"data_description": {
@@ -649,6 +650,7 @@
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/ambervalley_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
+ "predict": "Predict",
"uprn": "UPRN"
},
"data_description": {
diff --git a/custom_components/waste_collection_schedule/translations/it.json b/custom_components/waste_collection_schedule/translations/it.json
index c35289b2..34cecec3 100644
--- a/custom_components/waste_collection_schedule/translations/it.json
+++ b/custom_components/waste_collection_schedule/translations/it.json
@@ -5759,6 +5759,7 @@
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. Maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/ambervalley_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
+ "predict": "Predict",
"uprn": "UPRN"
},
"data_description": {
@@ -5771,6 +5772,7 @@
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. Per maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/ambervalley_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
+ "predict": "Predict",
"uprn": "UPRN"
},
"data_description": {
diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/ambervalley_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/ambervalley_gov_uk.py
index 92699ca9..53942def 100644
--- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/ambervalley_gov_uk.py
+++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/ambervalley_gov_uk.py
@@ -1,16 +1,18 @@
import json
-import requests
+from datetime import date, datetime, timedelta
-from datetime import datetime
+import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "Amber Valley Borough Council"
-DESCRIPTION = "Source for ambervalley.gov.uk services for Amber Valley Borough Council, UK."
+DESCRIPTION = (
+ "Source for ambervalley.gov.uk services for Amber Valley Borough Council, UK."
+)
URL = "https://ambervalley.gov.uk"
TEST_CASES = {
- "Test_001": {"uprn": "100030011612"},
+ "Test_001": {"uprn": "100030011612", "predict": True},
"Test_002": {"uprn": "100030011654"},
- "test_003": {"uprn": 100030041980}
+ "test_003": {"uprn": 100030041980, "predict": True},
}
ICON_MAP = {
@@ -21,12 +23,49 @@ ICON_MAP = {
"COMMUNAL RECYCLING": "mdi:recycle",
}
+WASTE_TYPES_DATE_KEY = {
+ "REFUSE": "refuseNextDate",
+ "RECYCLING": "recyclingNextDate",
+ "GREEN": "greenNextDate",
+ "COMMUNAL REFUSE": "communalRefNextDate",
+ "COMMUNAL RECYCLING": "communalRycNextDate",
+}
+
+WASTE_TYPE_FREQUENCY_KEY = {
+ "REFUSE": "weeklyCollection",
+ "RECYCLING": "weeklyCollection",
+ "GREEN": "weeklyCollection",
+ "COMMUNAL REFUSE": "communalRefWeekly",
+ "COMMUNAL RECYCLING": "communalRycWeekly",
+}
+
+
+def _get_date(date_string: str) -> date:
+ return datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S").date()
+
class Source:
- def __init__(self, uprn):
+ def __init__(self, uprn: str | int, predict: bool = False):
self._uprn = str(uprn)
+ self._predict = predict
- def fetch(self):
+ @staticmethod
+ def _construct_waste_dict(data: dict, predict: bool) -> dict[str, list[date]]:
+ to_return = {}
+ for bin, datge_key in WASTE_TYPES_DATE_KEY.items():
+ date_ = _get_date(data[datge_key])
+ if date_ == date(1900, 1, 1):
+ continue
+ to_return[bin] = [date_]
+ if predict:
+ weekly: bool = data[WASTE_TYPE_FREQUENCY_KEY[bin]]
+ day_offset = 7 if weekly else 14
+ for i in range(1, 365 // day_offset):
+ to_return[bin].append(date_ + timedelta(days=day_offset * i))
+
+ return to_return
+
+ def fetch(self) -> list[Collection]:
# get json file
r = requests.get(
f"https://info.ambervalley.gov.uk/WebServices/AVBCFeeds/WasteCollectionJSON.asmx/GetCollectionDetailsByUPRN?uprn={self._uprn}"
@@ -34,26 +73,19 @@ class Source:
# extract data from json
data = json.loads(r.text)
- wasteDict = {
- "REFUSE": data["refuseNextDate"],
- "RECYCLING": data["recyclingNextDate"],
- "GREEN": data["greenNextDate"],
- "COMMUNAL REFUSE": data["communalRefNextDate"],
- "COMMUNAL RECYCLING": data["communalRycNextDate"],
- }
+ wasteDict = self._construct_waste_dict(data, self._predict)
entries = []
- for item in wasteDict:
- if wasteDict[item] != "1900-01-01T00:00:00":
- entries.append(
- Collection(
- date=datetime.strptime(
- wasteDict[item], "%Y-%m-%dT%H:%M:%S").date(),
- t=item,
- icon=ICON_MAP.get(item),
+ for bin_type, dates in wasteDict.items():
+ for date_ in dates:
+ if date_ != "1900-01-01T00:00:00":
+ entries.append(
+ Collection(
+ date=date_,
+ t=bin_type,
+ icon=ICON_MAP.get(bin_type),
+ )
)
- )
return entries
-
diff --git a/doc/source/ambervalley_gov_uk.md b/doc/source/ambervalley_gov_uk.md
index e0654479..a5dfe13c 100644
--- a/doc/source/ambervalley_gov_uk.md
+++ b/doc/source/ambervalley_gov_uk.md
@@ -10,6 +10,7 @@ waste_collection_schedule:
- name: ambervalley_gov_uk
args:
uprn: UPRN_CODE
+ predict: PREDICT (optional) True or False
```
### Configuration Variables
@@ -17,6 +18,11 @@ waste_collection_schedule:
**uprn**
*(string) (required)*
+**predict**
+*(boolean) (optional|defaults to false)*
+
+If `predict` is set to `True`, the source will try to predict the next collection date based on the previous collection dates and collection frequency. By default this source only provides the next collection date.
+
## Example
```yaml
@@ -29,4 +35,4 @@ waste_collection_schedule:
## How to get the source argument
-An easy way to discover your Unique Property Reference Number (UPRN) is by going to and entering in your address details.
\ No newline at end of file
+An easy way to discover your Unique Property Reference Number (UPRN) is by going to and entering in your address details.