-
-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Source: Joondalup, Australia (#3312)
* initial commit * corrections * GUI descriptions added * .md added * updates * filename typo * rename file * tidy-up * typos, update_docu_links * reformatting md + adding one typehint --------- Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
- Loading branch information
Showing
8 changed files
with
319 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
...ponents/waste_collection_schedule/waste_collection_schedule/source/joondalup_wa_gov_au.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import json | ||
from datetime import date, datetime, timedelta | ||
|
||
import requests | ||
from dateutil.rrule import FR, MO, SA, SU, TH, TU, WE, WEEKLY, rrule | ||
from requests.utils import requote_uri | ||
from waste_collection_schedule import Collection # type: ignore[attr-defined] | ||
|
||
TITLE = "City of Joondalup" | ||
DESCRIPTION = "Source for City of Joondalup (WA) waste collection." | ||
URL = "https://www.joondalup.wa.gov.au" | ||
TEST_CASES = { | ||
"test address": { | ||
"number": "2", | ||
"street": "Ashburton Drive", | ||
"suburb": "Heathridge", | ||
}, | ||
"test mapkey": { | ||
"mapkey": 785, | ||
}, | ||
} | ||
HEADERS: dict = { | ||
"user-agent": "Mozilla/5.0", | ||
"accept": "application/json, text/plain, */*", | ||
} | ||
DAYS: dict = { | ||
"MONDAY": MO, | ||
"TUESDAY": TU, | ||
"WEDNESDAY": WE, | ||
"THURSDAY": TH, | ||
"FRIDAY": FR, | ||
"SATURDAY": SA, | ||
"SUNDAY": SU, | ||
} | ||
ICON_MAP: dict = { | ||
"Recycling": "mdi:recycle", | ||
"Bulk Put Out": "mdi:tree", | ||
"Bulk Pick Up": "mdi:tree", | ||
"General Waste": "mdi:trash-can", | ||
"Green Waste": "mdi:leaf", | ||
} | ||
|
||
HOW_TO_GET_ARGUMENTS_DESCRIPTION = { # Optional dictionary to describe how to get the arguments, will be shown in the GUI configuration form above the input fields, does not need to be translated in all languages | ||
"en": "Your house number, street name, and suburb as they appear when searching for your collection schedule on the Joonalup website: https://www.joondalup.wa.gov.au/residents/waste-and-recycling/residential-bin-collections. Alternatively, you can use your mapkey, if you know it.", | ||
} | ||
|
||
PARAM_DESCRIPTIONS = { # Optional dict to describe the arguments, will be shown in the GUI configuration below the respective input field | ||
"en": { | ||
"number": "Your house number as it appears on the Joonalup website", | ||
"street": "Your stree name as it appears on the Joonalup website", | ||
"suburb": "Your suburb as it appears on the Joonalup website", | ||
"mapkey": "The unique identifier for your property used by the Joonalup website", | ||
}, | ||
} | ||
|
||
PARAM_TRANSLATIONS = { # Optional dict to translate the arguments, will be shown in the GUI configuration form as placeholder text | ||
"en": { | ||
"number": "Your house number as it appears on the Joonalup website", | ||
"street": "Your stree name as it appears on the Joonalup website", | ||
"suburb": "Your suburb as it appears on the Joonalup website", | ||
"mapkey": "The unique identifier for your property used by the Joonalup website", | ||
}, | ||
} | ||
|
||
# _LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class Source: | ||
def __init__( | ||
self, | ||
number=None, | ||
street=None, | ||
suburb=None, | ||
mapkey=None, | ||
): | ||
if mapkey is None: | ||
self._number = str(number) | ||
self._street = str(street) | ||
self._suburb = str(suburb).upper() | ||
self._mapkey = None | ||
else: | ||
self._number = None | ||
self._street = None | ||
self._suburb = None | ||
self._mapkey = str(mapkey) | ||
|
||
def format_date(self, s: str) -> date: | ||
dt = datetime.strptime(s, "%A %d/%m/%Y").date() | ||
return dt | ||
|
||
def generate_general_waste_date(self, s: datetime, d: int) -> date: | ||
rr = rrule(WEEKLY, dtstart=s, byweekday=d) | ||
dt = rr.after(s) | ||
return dt.date() | ||
|
||
def generate_recycle_dates(self, s: str) -> tuple: | ||
d: date = self.format_date(s) | ||
dt1: date = d + timedelta(days=-7) | ||
dt2: date = d + timedelta(days=7) | ||
return dt1, dt2 | ||
|
||
def fetch(self) -> list[Collection]: | ||
start_date = datetime.now() + timedelta(days=-1) | ||
|
||
s = requests.Session() | ||
|
||
if self._mapkey is None: | ||
# use address details to find the mapkey | ||
search_term = requote_uri(f"{self._street} {self._suburb}") | ||
r = s.get( | ||
f"https://www.joondalup.wa.gov.au/aapi/coj/propertylookup/{search_term}", | ||
headers=HEADERS, | ||
) | ||
properties = json.loads(r.content) | ||
for property in properties: | ||
if str(property["house_no"]) == self._number: | ||
self._mapkey = property["mapkey"] | ||
|
||
# use the mapkey to get the schedule | ||
r = s.get( | ||
f"https://www.joondalup.wa.gov.au/aapi/coj/bindatelookup/{self._mapkey}", | ||
headers=HEADERS, | ||
) | ||
pickups = json.loads(r.content)[0] | ||
|
||
# some waste types just state the collection day and frequency | ||
# so generate dates for those | ||
general = self.generate_general_waste_date( | ||
start_date, DAYS[pickups["Rubbish_Day"].upper().strip()] | ||
) | ||
recycle1, recycle2 = self.generate_recycle_dates(pickups["Next_Recycling_Date"]) | ||
|
||
schedule: dict = { | ||
"Recycling": self.format_date(pickups["Next_Recycling_Date"]), | ||
"Bulk Put Out": self.format_date(pickups["Bulk_Rubbish_Put_Out"]), | ||
"Bulk Pick Up": self.format_date(pickups["Bulk_Rubbish_Pick_Up"]), | ||
"General Waste": general, | ||
"Green Waste": recycle1, | ||
"Green Waste ": recycle2, # note the whitespace character to distinguish between keys | ||
} | ||
|
||
entries = [] | ||
|
||
for item in schedule: | ||
entries.append( | ||
Collection( | ||
date=schedule[item], | ||
t=item.strip(), | ||
icon=ICON_MAP.get(item.strip()), | ||
) | ||
) | ||
|
||
return entries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# City of Joondalup | ||
|
||
Support for schedules provided by [City of Joonalup](https://www.joondalup.wa.gov.au/residents/waste-and-recycling/residential-bin-collections). | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: joondalup_wa_gov_au | ||
args: | ||
number: NUMBER | ||
street: STREET | ||
suburb: SUBURB | ||
mapkey: MAPKEY | ||
``` | ||
### Configuration Variables | ||
**number** | ||
*(string) (optional)* | ||
Your house number as it appears on the Joondalup website. | ||
**street** | ||
*(string) (optional)* | ||
Your street name as it appears on the Joondalup website. | ||
**suburb** | ||
*(string) (optional)* | ||
Your suburb as it appears on the Joondalup website. | ||
**mapkey** | ||
*(string) (optional)* | ||
The unique identifier for your property used by the Joondalup website. | ||
## Example | ||
Your must provide either: | ||
- the number, street and suburb, or | ||
- the mapkey | ||
The following examples are equivalent | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: joondalup_wa_gov_au | ||
args: | ||
number: "2" | ||
street: "Ashburton Drive" | ||
suburb: "Heathridge" | ||
``` | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: joondalup_wa_gov_au | ||
args: | ||
mapkey: "785" | ||
``` | ||
## How to find your mapkey, if you want to use it | ||
Search for your collection schedule on the Joondalup website. Wait for the schedule to load and the property image to be displayed. Right-click on the property image and _copy image address_. Examine the copied url, your mapkey is shown at the end of the url. | ||
Oops, something went wrong.