ambervalley_gov_uk add predict argument, which tries to predict collections further in the future

This commit is contained in:
5ila5
2024-09-19 15:18:18 +02:00
committed by 5ila5
parent 1aabc69c14
commit e8a316f68a
5 changed files with 69 additions and 25 deletions

View File

@@ -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": {

View File

@@ -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": {

View File

@@ -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": {

View File

@@ -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":
for bin_type, dates in wasteDict.items():
for date_ in dates:
if date_ != "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),
date=date_,
t=bin_type,
icon=ICON_MAP.get(bin_type),
)
)
return entries

View File

@@ -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