Repository: mampfes/hacs_waste_collection_schedule

Issue:
Error collecting data for Chesterfield.gov.uk
Hi

I'm getting the following error with the chesterfield.gov.uk integration...

fetch failed for source chesterfield.gov.uk: Traceback (most recent call last): File "/config/custom_components/waste_collection_schedule/waste_collection_schedule/scraper.py", line 122, in fetch entries = self._source.fetch() File "/config/custom_components/waste_collection_schedule/waste_collection_schedule/source/chesterfield_gov_uk.py", line 54, in fetch "message": "{\"actions\":[{\"id\":\"4;a\",\"descriptor\":\"aura://ApexActionController/ACTION$execute\",\"callingDescriptor\":\"UNKNOWN\",\"params\":{\"namespace\":\"\",\"classname\":\"CBC_VE_CollectionDays\",\"method\":\"getServicesByUPRN\",\"params\":{\"propertyUprn\":\"" + self._uprn +"\",\"executedFrom\":\"Main Website\"},\"cacheable\":false,\"isContinuation\":false}}]}", TypeError: can only concatenate str (not "int") to str

Is this config or code?
Kind regards


Discussion:
Probably the code. I had this error a couple of times when composing the post request but thought I'd resolved it as it didn't happen during the subsequent testing.

It may be because on your system it's interpreting the uprn as a number rather than a string, but will take another look. 

If anyone else has any bright ideas feel free to chip in...
Hi

I think I may have figured it out. 
It doesn't specify in the instructions but the scrape works if the uprn is in "".

Kind regards
Yep, that'd do it.
Easy enough to update the .md to include the quote marks and fix my typo.

Resolving patch:
diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/chesterfield_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/chesterfield_gov_uk.py
--- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/chesterfield_gov_uk.py
+++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/chesterfield_gov_uk.py
@@ -1,13 +1,13 @@
 import json
 import logging
-import requests
-
 from datetime import datetime
-from waste_collection_schedule import Collection
 
-# These lines are needed to supress the InsecureRequestWarning resulting from the POST verify=False option
+import requests
+# These lines are needed to suppress the InsecureRequestWarning resulting from the POST verify=False option
 # With verify=True the POST fails due to a SSLCertVerificationError.
 import urllib3
+from waste_collection_schedule import Collection
+
 urllib3.disable_warnings()
 # The following links may provide a better way of dealing with this, as using verify=False is not ideal:
 # https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
@@ -22,10 +22,10 @@
 URL = "https://www.chesterfield.gov.uk/"
 
 TEST_CASES = {
-    "Test_001": {"uprn": "74023685"},
+    "Test_001": {"uprn": 74023685},
     "Test_002": {"uprn": "74009625"},
     "Test_003": {"uprn": "74035689"},
-    "Test_004": {"uprn": "74020930"}
+    "Test_004": {"uprn": "74020930"},
 }
 
 
@@ -41,39 +41,49 @@
 
 class Source:
     def __init__(self, uprn=None):
-        self._uprn = uprn
+        self._uprn = str(uprn)
 
     def fetch(self):
 
         s = requests.Session()
-        r = s.get("https://www.chesterfield.gov.uk/bins-and-recycling/bin-collections/check-bin-collections.aspx")
+        r = s.get(
+            "https://www.chesterfield.gov.uk/bins-and-recycling/bin-collections/check-bin-collections.aspx"
+        )
 
         if self._uprn:
             # POST request returns schedule for matching uprn
             payload = {
-                "message": "{\"actions\":[{\"id\":\"4;a\",\"descriptor\":\"aura://ApexActionController/ACTION$execute\",\"callingDescriptor\":\"UNKNOWN\",\"params\":{\"namespace\":\"\",\"classname\":\"CBC_VE_CollectionDays\",\"method\":\"getServicesByUPRN\",\"params\":{\"propertyUprn\":\"" + self._uprn +"\",\"executedFrom\":\"Main Website\"},\"cacheable\":false,\"isContinuation\":false}}]}",
-                "aura.context": "{\"mode\":\"PROD\",\"fwuid\":\"QPQi8lbYE8YujG6og6Dqgw\",\"app\":\"c:cbc_VE_CollectionDaysLO\",\"loaded\":{\"APPLICATION@markup://c:cbc_VE_CollectionDaysLO\":\"RYJPh-YIBl_XNnF_aFwc8A\"},\"dn\":[],\"globals\":{},\"uad\":true}",
+                "message": '{"actions":[{"id":"4;a","descriptor":"aura://ApexActionController/ACTION$execute","callingDescriptor":"UNKNOWN","params":{"namespace":"","classname":"CBC_VE_CollectionDays","method":"getServicesByUPRN","params":{"propertyUprn":"'
+                + self._uprn
+                + '","executedFrom":"Main Website"},"cacheable":false,"isContinuation":false}}]}',
+                "aura.context": '{"mode":"PROD","fwuid":"QPQi8lbYE8YujG6og6Dqgw","app":"c:cbc_VE_CollectionDaysLO","loaded":{"APPLICATION@markup://c:cbc_VE_CollectionDaysLO":"RYJPh-YIBl_XNnF_aFwc8A"},"dn":[],"globals":{},"uad":true}',
                 "aura.pageURI": "/bins-and-recycling/bin-collections/check-bin-collections.aspx",
-                "aura.token": "null"
+                "aura.token": "null",
             }
-            r = s.post("https://myaccount.chesterfield.gov.uk/anonymous/aura?r=2&aura.ApexAction.execute=1", data = payload, verify = False)
+            r = s.post(
+                "https://myaccount.chesterfield.gov.uk/anonymous/aura?r=2&aura.ApexAction.execute=1",
+                data=payload,
+                verify=False,
+            )
             data = json.loads(r.content)
 
         entries = []
 
         # Extract waste types and dates from json
-        for item in data['actions'][0]['returnValue']['returnValue']['serviceUnits']:
-            waste_type = item['serviceTasks'][0]['taskTypeName']
-            waste_type = str(waste_type).replace('Collect ', '')
-            dt_zulu = (item['serviceTasks'][0]['serviceTaskSchedules'][0]['nextInstance']['currentScheduledDate'])
-            dt_utc = datetime.strptime(dt_zulu, '%Y-%m-%dT%H:%M:%S.%f%z')
+        for item in data["actions"][0]["returnValue"]["returnValue"]["serviceUnits"]:
+            waste_type = item["serviceTasks"][0]["taskTypeName"]
+            waste_type = str(waste_type).replace("Collect ", "")
+            dt_zulu = item["serviceTasks"][0]["serviceTaskSchedules"][0][
+                "nextInstance"
+            ]["currentScheduledDate"]
+            dt_utc = datetime.strptime(dt_zulu, "%Y-%m-%dT%H:%M:%S.%f%z")
             dt_local = dt_utc.astimezone(None)
             entries.append(
                 Collection(
                     date=dt_local.date(),
                     t=waste_type,
-                    icon=ICONS.get(waste_type.upper()),    
+                    icon=ICONS.get(waste_type.upper()),
                 )
             )
-            
+
         return entries


Test patch: