Repository: mampfes/hacs_waste_collection_schedule

Issue:
Support request for Breckland
https://www.breckland.gov.uk/mybreckland

 Post Code: IP22 2LJ

I'm running HA in VirtualBox and can't find a way to run the python script, but digging into the code this looks promising...

{"jsonrpc":"2.0","id":"1671645668056","method":"Breckland.Whitespace.JointWasteAPI.GetBinCollectionsByUprn","params":{"uprn":"100091308629","environment":"live"}}


Discussion:
@danwooller 

i got this response:

```
{
	"id": "",
	"result": [
		{
			"collectiontype": "Refuse Collection Service",
			"roundname": "BR6",
			"schedule": "FriFort1",
			"day": "Friday",
			"nextcollection": "03/01/2023 00:00:00"
		},
		{
			"collectiontype": "Garden Waste Service",
			"roundname": "BGarden2",
			"schedule": "GWFriFort2",
			"day": "Friday",
			"nextcollection": "09/01/2023 00:00:00"
		},
		{
			"collectiontype": "Recycling Collection Service",
			"roundname": "BR6",
			"schedule": "FriFort2",
			"day": "Friday",
			"nextcollection": "09/01/2023 00:00:00"
		},
		{
			"collectiontype": "Refuse Collection Service",
			"roundname": "BR6",
			"schedule": "FriFort1",
			"day": "Friday",
			"nextcollection": "14/01/2023 00:00:00"
		},
		{
			"collectiontype": "Garden Waste Service",
			"roundname": "BGarden2",
			"schedule": "GWFriFort2",
			"day": "Friday",
			"nextcollection": "20/01/2023 00:00:00"
		},
		{
			"collectiontype": "Recycling Collection Service",
			"roundname": "BR6",
			"schedule": "FriFort2",
			"day": "Friday",
			"nextcollection": "20/01/2023 00:00:00"
		}
	],
	"jsonrpc": "2.0"
}
```

Looks like a response of the next 2 schedules.
That's looks good.

I guess I need to build a system I can run python on.
@danwooller are the next to schedules ok? or shoud we try to make a future projection based of the informations?
I can try to write the code.
@bbr111 

That response is exactly what I was expecting, refuse one week then recycling and garden the following. That dates are a little skewed because of holidays, but those are the next collection days. Thanks.
I don't understand python, but got as far as a response:

Testing source breckland_gov_uk ...
  found 1 entries for house_uprn
    2020-04-11: Waste Type [None]

Well beyond my knowledge for the next bit...


`import datetime
from waste_collection_schedule import Collection

TITLE = "Breckland Council"
DESCRIPTION = "Source script for Breckland Council"
URL = "https://www.breckland.gov.uk/mybreckland"
TEST_CASES = {
    "house_uprn": {"uprn": "100091308629", "postcode": "IP222LJ"},
}

API_URL = "https://www.breckland.gov.uk/mybreckland"
ICON_MAP = {
    "Refuse Collection Service": "mdi:trash-can",
    "Recycling Collection Service": "mdi:recycle",
    "Garden Waste Service": "mdi:leaf",
}


class Source:
    def __init__(self, uprn, postcode):  # argX correspond to the args dict in the source configuration
        self._uprn = uprn
        self._postcode = postcode

    def fetch(self):

        #  replace this comment with
        #  api calls or web scraping required
        #  to capture waste collection schedules
        #  and extract date and waste type details

        entries = []  # List that holds collection schedule

        entries.append(
            Collection(
                date = datetime.date(2020, 4, 11),  # Collection date
                t = "Waste Type",  # Collection type
                icon = ICON_MAP.get("Waste Type"),  # Collection icon
            )
        )

        return entries`

Resolving patch:
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
new file mode 100644
--- /dev/null
+++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/breckland_gov_uk.py
@@ -0,0 +1,94 @@
+import logging
+import requests
+from datetime import datetime
+from waste_collection_schedule import Collection
+
+TITLE = "Breckland Council, UK"
+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=None,address=None,uprn=None):
+        self._postcode = postcode
+        self._address = address
+        self._uprn = uprn
+
+    def fetch(self):
+
+        if self._postcode == None and self._address == None and self._uprn == None:
+            _LOGGER.error("no attributes - specify postcode and address or just uprn")
+            return []
+
+        if self._uprn == None:
+            json_data = {
+                "jsonrpc":"2.0",
+                 "id":"",
+                 "method":"Breckland.Whitespace.JointWasteAPI.GetSiteIDsByPostcode",
+                 "params":{
+                     "postcode":self._postcode,
+                     "environment":"live"
+                     }
+                 }
+
+            r = requests.post(API_URL, json=json_data,headers=headers)
+
+            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 []
+
+        json_data = {
+            "jsonrpc":"2.0",
+            "id":"",
+            "method":"Breckland.Whitespace.JointWasteAPI.GetBinCollectionsByUprn",
+            "params":{
+                "uprn":self._uprn,
+                "environment":"live"
+                }
+            }
+
+        r = requests.post(API_URL, json=json_data,headers=headers)
+
+        if r.status_code != 200:
+            _LOGGER.error("Error querying calender data")
+            return []
+
+        waste = r.json()
+        waste = waste['result']
+
+        entries = []
+
+        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'],
+                             ICON_MAP.get(data['collectiontype'])
+                             ))
+
+        return entries
\ No newline at end of file


Test patch: