Skip to content

Commit

Permalink
Update to Hawkesbury NSW GOV AU Integration (#2232)
Browse files Browse the repository at this point in the history
* Fixed Integration to work with recent api chagnes

* Documentation Update

* removed print statement

* reformatting

---------

Co-authored-by: Zach Herberstein <Zach.herberstein@pernod-ricard.com>
Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 8, 2024
1 parent a4176e2 commit 51488d5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@
"interval": "Interval",
"bill_number": "Bill Number",
"values": "Values",
"property_no": "Property No"
"property_no": "Property No",
"postCode": "Post Code"
},
"data_description": {
"calendar_title": "A more readable, or user-friendly, name for the waste calendar. If nothing is provided, the name returned by the source will be used."
Expand Down Expand Up @@ -500,7 +501,8 @@
"interval": "Interval",
"bill_number": "Bill Number",
"values": "Values",
"property_no": "Property No"
"property_no": "Property No",
"postCode": "Post Code"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@
"suburb": "south windsor",
"street": "George Street",
"houseNo": 539,
"postCode": 2756,
},
"Windsor, catherine street 7": {
"suburb": "Windsor",
"street": "catherine st",
"houseNo": 7,
"postCode": 2756,
},
"Kurrajong, Bells Line Of Road 1052 ": {
"suburb": "Kurrajong HILLS",
"street": "Bells Line Of Road",
"houseNo": 1052,
}
"postCode": 2758,
},
}
API_URL = "https://data.hawkesbury.nsw.gov.au/api"
_LOGGER = logging.getLogger(__name__)
ICON_MAP = { # Optional: Dict of waste types and suitable mdi icons
ICON_MAP = { # Optional: Dict of waste types and suitable mdi icons
"DOMESTIC": "mdi:trash-can",
"RECYCLE": "mdi:recycle",
"ORGANIC": "mdi:leaf",
Expand All @@ -52,21 +55,24 @@


class Source:
def __init__(self, suburb, street, houseNo):
def __init__(self, suburb, street, houseNo, postCode):
self._suburb = suburb.upper()
self._street = street
self._houseNo = str(houseNo)
self._url = API_URL
self._postCode = postCode

def get_data(self, bin_prefix: str, fields) -> list[Collection]:
entries: list[Collection] = []

frequency = int(fields.get(f'{bin_prefix}_schedule'))
frequency = int(fields.get(f"{bin_prefix}_schedule", 0))
if frequency == 0:
return entries

base_string = fields.get(f'{bin_prefix}_week1', dt.datetime.min.strftime('%Y-%m-%d'))
basedate = dt.datetime.strptime(base_string, "%Y-%m-%d")
base_string = fields.get(
f"{bin_prefix}_week1", dt.datetime.min.strftime("%Y-%m-%d")
)
basedate = parse_date_field(base_string)

# Get number of days between basedate and a year from now
days = ((dt.datetime.now() + dt.timedelta(days=365)) - basedate).days
Expand All @@ -81,27 +87,44 @@ def fetch(self):
# check address values are not abbreviated
address = self._street
for key in STREETNAMES.keys():
regex = r"\b{}\b".format(key.lower())
regex = rf"\b{key.lower()}\b"
address = re.sub(
pattern=regex,
repl=STREETNAMES[key],
string=address.lower())
pattern=regex, repl=STREETNAMES[key], string=address.lower()
)

# get list of suburbs
r = requests.get(
f"{self._url}/records/1.0/search/?sort=gisaddress&refine.gisaddress={self._houseNo} {address.title()} {self._suburb}&rows=1&dataset=bin-collection-days&timezone=Australia/Sydney&lang=en")
f"{self._url}/records/1.0/search/",
params={
"sort": "gisaddress",
"refine.gisaddress": f"{self._houseNo} {address.title()} {self._suburb} NSW {self._postCode}",
"rows": 1,
"dataset": "bin-collection-days",
"timezone": "Australia/Sydney",
"lang": "en",
},
)
data = json.loads(r.text)

# Check if house record was found
if len(data['records']) == 0:
if len(data["records"]) == 0:
raise Exception(f"House not found: {self._houseNo}")

# get collection schedule
record = data['records'][-1]
garbagebin_entries = self.get_data('garbagebin', record['fields'])
recyclebin_entries = self.get_data('recyclebin', record['fields'])
organicbin_entries = self.get_data('organicbin', record['fields'])
record = data["records"][-1]
garbagebin_entries = self.get_data("garbagebin", record["fields"])
recyclebin_entries = self.get_data("recyclebin", record["fields"])
organicbin_entries = self.get_data("organicbin", record["fields"])
entries = garbagebin_entries + recyclebin_entries + organicbin_entries

return entries



def parse_date_field(date_string):
formats = ["%Y-%m-%d %H:%M:%S", "%Y-%m-%d"]
for fmt in formats:
try:
return dt.datetime.strptime(date_string, fmt)
except ValueError:
continue
raise ValueError(f"Unrecognized date format: {date_string}")
5 changes: 5 additions & 0 deletions doc/source/hawkesbury_nsw_gov_au.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ waste_collection_schedule:
suburb: SUBURB
street: STREET
houseNo: HOUSENO
postCode: POSTCODE
```
### Configuration Variables
Expand All @@ -24,6 +25,9 @@ waste_collection_schedule:
**houseNo**
*(string) (required)*
**postCode**
*(string) (required)*
## Example
```yaml
Expand All @@ -34,6 +38,7 @@ waste_collection_schedule:
suburb: South Windsor
street: George Street
houseNo: 539
postCode: 2756
```
## How to get the source arguments
Expand Down

0 comments on commit 51488d5

Please sign in to comment.