diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/tewkesbury_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/tewkesbury_gov_uk.py index a5ddc65e..8c46f228 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/tewkesbury_gov_uk.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/tewkesbury_gov_uk.py @@ -10,6 +10,7 @@ URL = "https://www.tewkesbury.gov.uk" TEST_CASES = { "Council Office": {"postcode": "GL20 5TT"}, "Council Office No Spaces": {"postcode": "GL205TT"}, + "UPRN example": {"uprn": 100120544973}, } API_URL = "https://api-2.tewkesbury.gov.uk/general/rounds/%s/nextCollection" @@ -23,14 +24,14 @@ ICON_MAP = { class Source: - def __init__(self, postcode: str = None): - self._postcode = postcode + def __init__(self, postcode: str = None, uprn: str = None): + self._post_or_uprn = str(uprn) if uprn else postcode def fetch(self): - if self._postcode is None: + if self._post_or_uprn is None: raise Exception("postcode not set") - encoded_postcode = urlquote(self._postcode) + encoded_postcode = urlquote(self._post_or_uprn) request_url = API_URL % encoded_postcode response = requests.get(request_url) @@ -39,16 +40,19 @@ class Source: entries = [] - if data["status"] == "OK": - schedule = data["body"] - for schedule_entry in schedule: - entries.append( - Collection( - date=datetime.strptime( - schedule_entry["NextCollection"], "%Y-%m-%d").date(), - t=schedule_entry["collectionType"], - icon=ICON_MAP.get(schedule_entry["collectionType"]) - ) + if data["status"] != "OK": + raise Exception(f"Error fetching data. \"{data['status']}\": \n {data['body']}") + + schedule = data["body"] + for schedule_entry in schedule: + entries.append( + Collection( + date=datetime.strptime( + schedule_entry["NextCollection"], "%Y-%m-%d").date(), + t=schedule_entry["collectionType"], + icon=ICON_MAP.get(schedule_entry["collectionType"]) ) + ) + return entries diff --git a/doc/source/tewkesbury_gov_uk.md b/doc/source/tewkesbury_gov_uk.md index 71ae38c1..64d843aa 100644 --- a/doc/source/tewkesbury_gov_uk.md +++ b/doc/source/tewkesbury_gov_uk.md @@ -12,10 +12,24 @@ waste_collection_schedule: postcode: POSTCODE ``` +```yaml +waste_collection_schedule: + sources: + - name: tewkesbury_gov_uk + args: + uprn: UNIQUE PROPERTY REFERENCE NUMBER +``` + ### Configuration Variables **POSTCODE** -*(string) (required)* +*(string) (optional)* +Not all addresses are supported by postcode. Then you have to provide a UPRN. + +**UPRN** +*(string) (optional)* + +Either `postcode` or `uprn` must be provided. ## Example @@ -26,3 +40,15 @@ waste_collection_schedule: args: postcode: "GL20 5TT" ``` + +```yaml +waste_collection_schedule: + sources: + - name: tewkesbury_gov_uk + args: + uprn: 100120544973 +``` + +## How to find your `UPRN` + +An easy way to find your Unique Property Reference Number (UPRN) is by going to and entering your address details.