From 53d28c35598503d53fe5957e4c19c3ac3fda1088 Mon Sep 17 00:00:00 2001 From: bbr111 Date: Fri, 30 Dec 2022 11:42:01 +0100 Subject: [PATCH] Add source for Breckland Council, UK - update Add ICON_Map Add support for uprn Update documentation --- .../source/breckland_gov_uk.py | 69 ++++++++++++------- doc/source/breckland_gov_uk.md | 20 +++++- 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/breckland_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/breckland_gov_uk.py index 3968fd7a..6ddc420a 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/breckland_gov_uk.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/breckland_gov_uk.py @@ -8,43 +8,56 @@ DESCRIPTION = "Source for breckland.gov.uk" URL = "https://www.breckland.gov.uk/mybreckland" TEST_CASES = { "test1" : {"postcode":"IP22 2LJ","address":"glen travis" }, + "test2" : {"uprn":"10011977093"}, +} + +API_URL = "https://www.breckland.gov.uk/apiserver/ajaxlibrary" +ICON_MAP = { +"Refuse Collection Service": "mdi:trash-can", +"Recycling Collection Service": "mdi:recycle", +"Garden Waste Service": "mdi:leaf", } _LOGGER = logging.getLogger(__name__) +headers = {'referer': URL} + class Source: - def __init__(self,postcode,address): + def __init__(self,postcode=None,address=None,uprn=None): self._postcode = postcode self._address = address + self._uprn = uprn def fetch(self): - json_data = { - "jsonrpc":"2.0", - "id":"", - "method":"Breckland.Whitespace.JointWasteAPI.GetSiteIDsByPostcode", - "params":{ - "postcode":self._postcode, - "environment":"live" - } - } - - headers = {'referer': 'https://www.breckland.gov.uk/mybreckland'} - url = 'https://www.breckland.gov.uk/apiserver/ajaxlibrary' - r = requests.post(url, json=json_data,headers=headers) - - if r.status_code != 200: - _LOGGER.error("Error querying calender data") + if self._postcode == None and self._address == None and self._uprn == None: + _LOGGER.error("no attributes - specify postcode and address or just uprn") return [] - json_response = r.json() + if self._uprn == None: + json_data = { + "jsonrpc":"2.0", + "id":"", + "method":"Breckland.Whitespace.JointWasteAPI.GetSiteIDsByPostcode", + "params":{ + "postcode":self._postcode, + "environment":"live" + } + } - uprn = "" - for key in json_response['result']: - if self._address.lower() in key['name'].lower(): - uprn = (key['uprn']) + r = requests.post(API_URL, json=json_data,headers=headers) - if uprn == "": + if r.status_code != 200: + _LOGGER.error("Error querying calender data") + return [] + + json_response = r.json() + + for key in json_response['result']: + if self._address.lower() in key['name'].lower(): + self._uprn = (key['uprn']) + + if self._uprn == None: _LOGGER.error("Error querying calender data") return [] @@ -53,12 +66,12 @@ class Source: "id":"", "method":"Breckland.Whitespace.JointWasteAPI.GetBinCollectionsByUprn", "params":{ - "uprn":uprn, + "uprn":self._uprn, "environment":"live" } } - r = requests.post(url, json=json_data,headers=headers) + r = requests.post(API_URL, json=json_data,headers=headers) if r.status_code != 200: _LOGGER.error("Error querying calender data") @@ -72,6 +85,10 @@ class Source: for data in waste: print(data['collectiontype']) print(data['nextcollection']) - entries.append(Collection(datetime.strptime(data['nextcollection'],'%d/%m/%Y %H:%M:%S').date(),data['collectiontype'])) + entries.append(Collection( + datetime.strptime(data['nextcollection'],'%d/%m/%Y %H:%M:%S').date(), + data['collectiontype'], + ICON_MAP.get(data['collectiontype']) + )) return entries \ No newline at end of file diff --git a/doc/source/breckland_gov_uk.md b/doc/source/breckland_gov_uk.md index ba13913e..ff3439c3 100644 --- a/doc/source/breckland_gov_uk.md +++ b/doc/source/breckland_gov_uk.md @@ -11,16 +11,20 @@ waste_collection_schedule: args: postcode: POSTCODE address: ADDRESS + uprn: UPRN ``` ### Configuration Variables **POSTCODE** -*(string) (required)* +*(string) (optional)* **ADDRESS** -*(string) (required)* +*(string) (optional)* + +**UPRN** +*(string) (optional)* ## Examples @@ -33,8 +37,20 @@ waste_collection_schedule: address: "glen travis" ``` +```yaml +waste_collection_schedule: + sources: + - name: breckland_gov_uk + args: + uprn: "10011977093" +``` + +If uprn is provided, only uprn is used. Otherwise postcode and address are required. + You can find all relevant informations at [Breckland Council](https://www.breckland.gov.uk/mybreckland) homepage use the POSTCODE -> click find address. Choose your address. Please only use the first part of your address. If you got an error, use less charecters from address. +## How to find your UPRN +An easy way to discover your Unique Property Reference Number (UPRN) is by going to [Find My Address](https://www.findmyaddress.co.uk/) and providng your address details.