From 903378346b226b8316194449d0330989f6077a26 Mon Sep 17 00:00:00 2001 From: Keith Date: Mon, 27 Feb 2023 15:23:29 +1100 Subject: [PATCH] Added cardinia_vic_gov_au --- README.md | 1 + .../source/cardinia_vic_gov_au.py | 101 ++++++++++++++++++ doc/source/cardinia_vic_gov_au.md | 32 ++++++ info.md | 2 +- 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/cardinia_vic_gov_au.py create mode 100644 doc/source/cardinia_vic_gov_au.md diff --git a/README.md b/README.md index 19df96978..4378891f3 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Waste collection schedules in the following formats and countries are supported. - [Belmont City Council](/doc/source/belmont_wa_gov_au.md) / belmont.wa.gov.au - [Brisbane City Council](/doc/source/brisbane_qld_gov_au.md) / brisbane.qld.gov.au - [Campbelltown City Council](/doc/source/campbelltown_nsw_gov_au.md) / campbelltown.nsw.gov.au +- [Cardinia Shire Council](/doc/source/cardinia_vic_gov_au.md) / cardinia.vic.gov.au - [City of Canada Bay Council](/doc/source/canadabay_nsw_gov_au.md) / canadabay.nsw.gov.au - [City of Onkaparinga Council](/doc/source/onkaparingacity_com.md) / onkaparingacity.com - [Gold Coast City Council](/doc/source/goldcoast_qld_gov_au.md) / goldcoast.qld.gov.au diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/cardinia_vic_gov_au.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/cardinia_vic_gov_au.py new file mode 100644 index 000000000..e10fff144 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/cardinia_vic_gov_au.py @@ -0,0 +1,101 @@ +import time +from datetime import datetime, timedelta + +import requests +from waste_collection_schedule import Collection + +TITLE = "Cardinia Shire Council" +DESCRIPTION = "Source script for cardinia.vic.gov.au" +URL = "https://www.cardinia.vic.gov.au" +TEST_CASES = { + "1015 Manks Rd": {"address": "1015 Manks Rd, Dalmore Vic"}, # Monday + "6-8 Main St": {"address": "6-8 Main St, Nar Nar Goon Vic"}, # Tuesday + "875 Princes Hwy": {"address": "875 Princes Hwy, Pakenham Vic"}, # Thursday + "124 Main St": {"address": "124 Main St, Pakenham Vic"}, # Friday +} + +API_URL = "https://www.cardinia.vic.gov.au/info/20002/rubbish_and_recycling/385/bin_collection_days_and_putting_your_bins_out/2#section-2-check-your-bin-collection-days-online" +ICON_MAP = { + "Rubbish": "mdi:trash-can", + "Recycling": "mdi:recycle", + "Green Waste": "mdi:leaf", +} + + +class Source: + def __init__(self, address): + self._address = address + + def next_collection(self, collection_day, weeks, start_date): + collection_day = time.strptime(collection_day, "%A").tm_wday + days = (collection_day - datetime.now().date().weekday() + 7) % 7 + next_collect = datetime.now().date() + timedelta(days=days) + days = abs(next_collect-datetime.strptime(start_date, "%Y-%m-%d").date()).days + if ((days//7)%weeks): + next_collect = next_collect + timedelta(days=7) + return next_collect + + def fetch(self): + # Get latitude & longitude of address + url = "https://geocoder.cit.api.here.com/6.2/search.json" + + params = { + "gen": "9", + "app_id": "pYZXmzEqjmR2DG66DRIr", + "app_code": "T-Z-VT6e6I7IXGuqBfF_vQ", + "country": "AUS", + "state": "VIC", + "searchtext": self._address, + "bbox": "-37.86,145.36;-38.34,145.78", + } + + r = requests.get(url, params=params) + r.raise_for_status() + + lat_long = r.json()["Response"]["View"][0]["Result"][0]["Location"]["DisplayPosition"] + + # Get waste collection zone by longitude and latitude + url = "https://services3.arcgis.com/TJxZpUnYIJOvcYwE/arcgis/rest/services/WasteCollectionZones/FeatureServer/0/query" + + params ={ + "f": "geojson", + "outFields": "*", + "returnGeometry": "true", + "inSR": "4326", + "spatialRel": "esriSpatialRelIntersects", + "geometryType": "esriGeometryPoint", + "geometry": str(lat_long["Longitude"]) + "," + str(lat_long["Latitude"]), + } + + r = requests.get(url, params=params) + r.raise_for_status() + + waste_schedule = r.json()["features"][0]["properties"] + + entries = [] + + entries.append( + Collection( + date = self.next_collection(waste_schedule["rub_day"], waste_schedule["rub_weeks"], waste_schedule["rub_start"]), + t = "Rubbish", + icon = ICON_MAP.get("Waste Type"), + ) + ) + + entries.append( + Collection( + date = self.next_collection(waste_schedule["rec_day"], waste_schedule["rec_weeks"], waste_schedule["rec_start"]), + t = "Recycling", + icon = ICON_MAP.get("Waste Type"), + ) + ) + + entries.append( + Collection( + date = self.next_collection(waste_schedule["grn_day"], waste_schedule["grn_weeks"], waste_schedule["grn_start"]), + t = "Green Waste", + icon = ICON_MAP.get("Waste Type"), + ) + ) + + return entries \ No newline at end of file diff --git a/doc/source/cardinia_vic_gov_au.md b/doc/source/cardinia_vic_gov_au.md new file mode 100644 index 000000000..ae706bec8 --- /dev/null +++ b/doc/source/cardinia_vic_gov_au.md @@ -0,0 +1,32 @@ +# Cardinia Shire Council + +Waste collection schedules provided by [Cardinia Shire Council](https://www.cardinia.vic.gov.au/). + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: cardinia_vic_gov_au + args: + address: ADDRESS # FORMATTING MUST BE EXACT, PLEASE SEE BELOW +``` + +### Configuration Variables + +**address** +*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: cardinia_vic_gov_au + args: + address: 6-8 Main St, Nar Nar Goon Vic +``` + +## How to get the correct address + +Search your address on [Cardinia Shire Council's Website](https://www.cardinia.vic.gov.au/info/20002/rubbish_and_recycling/385/bin_collection_days_and_putting_your_bins_out/2#check) to ensure you use the correct address format. Start typing the full address and the use autocomplete to search. After results have been found, copy the address exactly as it appears in the search box. \ No newline at end of file diff --git a/info.md b/info.md index 099bdef66..f59246af0 100644 --- a/info.md +++ b/info.md @@ -16,7 +16,7 @@ Waste collection schedules from service provider web sites are updated daily, de |--|--| | Generic | ICS / iCal files | | Static | User-defined dates or repeating date patterns | -| Australia | Banyule City Council, Belmont City Council, Brisbane City Council, Campbelltown City Council, City of Canada Bay Council, City of Onkaparinga Council, Gold Coast City Council, Hume City Council, Inner West Council (NSW), Ipswich City Council, Ku-ring-gai Council, Lake Macquarie City Council, Macedon Ranges Shire Council, Maribyrnong Council, Maroondah City Council, Melton City Council, Nillumbik Shire Council, North Adelaide Waste Management Authority, RecycleSmart, Stonnington City Council, The Hills Shire Council, Sydney, Whittlesea City Council, Wollongong City Council, Wyndham City Council, Melbourne | +| Australia | Banyule City Council, Belmont City Council, Brisbane City Council, Campbelltown City Council, Cardinia Shire Council, City of Canada Bay Council, City of Onkaparinga Council, Gold Coast City Council, Hume City Council, Inner West Council (NSW), Ipswich City Council, Ku-ring-gai Council, Lake Macquarie City Council, Macedon Ranges Shire Council, Maribyrnong Council, Maroondah City Council, Melton City Council, Nillumbik Shire Council, North Adelaide Waste Management Authority, RecycleSmart, Stonnington City Council, The Hills Shire Council, Sydney, Whittlesea City Council, Wollongong City Council, Wyndham City Council, Melbourne | | Austria | Burgenländischer Müllverband, infeo, Stadtservice Korneuburg, Umweltprofis, WSZ Moosburg | | Belgium | Hygea, Recycle! | | Canada | City of Toronto |