diff --git a/README.md b/README.md index f60ce203..b5f5321e 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ If your service provider is not listed, feel free to open a [source request issu - [Townsville](/doc/source/townsville_qld_gov_au.md) / townsville.qld.gov.au - [Unley City Council (SA)](/doc/source/unley_sa_gov_au.md) / unley.sa.gov.au - [Wellington Shire Council](/doc/source/impactapps_com_au.md) / wellington.vic.gov.au +- [Whitehorse City Counfil](/doc/source/whitehorse_vic_gov_au.md) / whitehorse.vic.gov.au - [Whittlesea City Council](/doc/source/whittlesea_vic_gov_au.md) / whittlesea.vic.gov.au/My-Neighbourhood - [Wollondilly Shire Council](/doc/source/wollondilly_nsw_gov_au.md) / wollondilly.nsw.gov.au - [Wollongong City Council](/doc/source/wollongongwaste_com_au.md) / wollongongwaste.com diff --git a/custom_components/waste_collection_schedule/sources.json b/custom_components/waste_collection_schedule/sources.json index a8425c41..ae0464e8 100644 --- a/custom_components/waste_collection_schedule/sources.json +++ b/custom_components/waste_collection_schedule/sources.json @@ -390,6 +390,11 @@ "service": "Wellington Shire Council" } }, + { + "title": "Whitehorse City Counfil", + "module": "whitehorse_vic_gov_au", + "default_params": {} + }, { "title": "Whittlesea City Council", "module": "whittlesea_vic_gov_au", diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/whitehorse_vic_gov_au.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/whitehorse_vic_gov_au.py new file mode 100644 index 00000000..bb466aed --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/whitehorse_vic_gov_au.py @@ -0,0 +1,164 @@ +import logging +import re +from datetime import date, datetime, timedelta + +import requests +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +_LOGGER = logging.getLogger(__name__) + +TITLE = "Whitehorse City Counfil" +DESCRIPTION = "Source for Whitehorse City Counfil." +URL = "https://www.whitehorse.vic.gov.au" +TEST_CASES = { + "17 Main Street BLACKBURN": {"address": "17 Main Street BLACKBURN"}, + "6/16 Ashted Road": {"address": "6/16 Ashted Road"}, +} + +ICON_MAP = { + "Household": "mdi:trash-can", + "GOBS": "mdi:leaf", + "Recycle": "mdi:recycle", +} + + +SEARCH_URL = "https://map.whitehorse.vic.gov.au/weave/services/v1/index/search" +BIN_REQUEST_URL = ( + "https://map.whitehorse.vic.gov.au/weave/services/v1/feature/getFeaturesByIds" +) + +# opening or closing tag +HTML_TAG_REGEX = re.compile(r"") + + +class Source: + def __init__(self, address: str): + self._address: str = address.lower().strip() + self._address_id = None + + def _matches_address(self, address: str) -> bool: + return self._address == re.sub(HTML_TAG_REGEX, "", address).lower().strip() + + def _fetch_address_id(self) -> None: + args: dict[str, str | int] = { + "start": 0, + "limit": 1000, + "indexes": "index.property", + "type": "EXACT", + "crs": "EPSG:3857", + "query": self._address, + } + + # get json file + r = requests.get(SEARCH_URL, params=args) + r.raise_for_status() + + data = r.json() + + if "results" not in data or not data["results"]: + raise ValueError("Could not find address") + + self._address_id = None + if len(data["results"]) == 1: + self._address_id = data["results"][0]["id"] + else: + for address in data["results"]: + if self._matches_address(address["display1"]): + self._address_id = address["id"] + break + + if not self._address_id: + raise ValueError( + "Could not find address, maybe try one one of the following: " + + ", ".join( + [ + re.sub(HTML_TAG_REGEX, "", address["display1"]) + for address in data["results"] + ] + ) + ) + + def fetch(self) -> list[Collection]: + """Get address ID if not set and fetch collections, tries to get address ID again if using old ID and get_collections fails.""" + fresh_id = False + if not self._address_id: + self._fetch_address_id() + fresh_id = True + + try: + return self._get_collections() + except Exception: + if fresh_id: + raise + self._fetch_address_id() + return self._get_collections() + + def _get_house_hold_waste(self, weekday: str) -> list[Collection]: + today = date.today() + + next_match: date | None = None + for i in range(7): + if (today + timedelta(days=i)).strftime("%A").lower() == weekday.lower(): + next_match = today + timedelta(days=i) + if not next_match: + raise ValueError("Invalid weekday") + + return [ + Collection( + date=next_match + timedelta(weeks=i), + t="Household", + icon=ICON_MAP.get("Household"), + ) + for i in range(10) + ] + + def _get_collections(self) -> list[Collection]: + if not self._address_id: + raise ValueError("Address ID is not set") + + args2: dict[str, str | list[str]] = { + "entityId": "lyr_vicmap_property", + "datadefinition": [ + "dd_whm_property_waste", + ], + "ids": self._address_id, + "outCrs": "EPSG:3857", + "returnCentroid": "false", + } + r = requests.get(BIN_REQUEST_URL, params=args2) + r.raise_for_status() + data = r.json() + entries = [] + for features in data["features"]: + waste_list: dict[str, str] = features.get("properties", {}).get( + "dd_whm_property_waste", [] + ) + if not waste_list: + continue + for waste_map in waste_list: + try: + entries.extend( + self._get_house_hold_waste(waste_map.get("collectionDay", "")) + ) + except ValueError: + _LOGGER.warning( + "Could not get household waste for weekday '%s'", + waste_map.get("collectionDay", ""), + ) + for key, value in waste_map.items(): + if not key.lower().startswith("next"): + continue + bin_type = key.removeprefix("next").strip() + + try: + # value format like: 12 Aug 2024 + date_ = datetime.strptime(value, "%d %b %Y").date() + except ValueError: + _LOGGER.warning("Could not parse date %s", value) + continue + + icon = ICON_MAP.get(bin_type) + + entries.append(Collection(date=date_, t=bin_type, icon=icon)) + + return entries diff --git a/doc/source/whitehorse_vic_gov_au.md b/doc/source/whitehorse_vic_gov_au.md new file mode 100644 index 00000000..ce4eb2a1 --- /dev/null +++ b/doc/source/whitehorse_vic_gov_au.md @@ -0,0 +1,34 @@ +# Whitehorse City Counfil + +Support for schedules provided by [Whitehorse City Counfil](https://www.whitehorse.vic.gov.au), serving Whitehorse, Australia. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: whitehorse_vic_gov_au + args: + address: ADDRESS + +``` + +### Configuration Variables + +**address** +*(String) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: whitehorse_vic_gov_au + args: + address: 17 Main Street BLACKBURN + +``` + +## How to get the source argument + +Find the parameter of your address using [https://map.whitehorse.vic.gov.au/index.html?entity=lyr_waste](https://map.whitehorse.vic.gov.au/index.html?entity=lyr_waste) and write them exactly like on the web page. diff --git a/info.md b/info.md index ac43b5d3..ea8745f9 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 | Armadale (Western Australia), Australian Capital Territory (ACT), Banyule City Council, Baw Baw Shire Council, Bayside City Council, Bega Valley Shire Council, Belmont City Council, Blacktown City Council (NSW), Blue Mountains City Council, Brisbane City Council, Burwood City Council, Campbelltown City Council (NSW), Cardinia Shire Council, City of Ballarat, City of Canada Bay Council, City of Cockburn, City of Darebin, City of Greater Geelong, City of Kingston, City of Onkaparinga Council, Cowra Council, Cumberland Council (NSW), Forbes Shire Council, Frankston City Council, Gold Coast City Council, Gwydir Shire Council, Hobsons Bay City Council, Hornsby Shire Council, Hume City Council, Impact Apps, Inner West Council (NSW), Ipswich City Council, Knox City Council, Ku-ring-gai Council, Lake Macquarie City Council, Lithgow City Council, Livingstone Shire Council, Loddon Shire Council, Logan City Council, Macedon Ranges Shire Council, Mansfield Shire Council, Maribyrnong Council, Maroondah City Council, Melton City Council, Merri-bek City Council, Moira Shire Council, Moree Plains Shire Council, Moreton Bay, Mosman Council, Nillumbik Shire Council, North Adelaide Waste Management Authority, Penrith City Council, Port Adelaide Enfield, South Australia, Port Macquarie Hastings Council, Port Stephens Council, Queanbeyan-Palerang Regional Council, RecycleSmart, Redland City Council (QLD), Shellharbour City Council, Singleton Council, Snowy Valleys Council, South Burnett Regional Council, Stirling, Stonnington City Council, The Hawkesbury City Council, Sydney, The Hills Shire Council, Sydney, Town of Victoria Park, Townsville, Unley City Council (SA), Wellington Shire Council, Whittlesea City Council, Wollondilly Shire Council, Wollongong City Council, Wyndham City Council, Melbourne, Yarra Ranges Council | +| Australia | Armadale (Western Australia), Australian Capital Territory (ACT), Banyule City Council, Baw Baw Shire Council, Bayside City Council, Bega Valley Shire Council, Belmont City Council, Blacktown City Council (NSW), Blue Mountains City Council, Brisbane City Council, Burwood City Council, Campbelltown City Council (NSW), Cardinia Shire Council, City of Ballarat, City of Canada Bay Council, City of Cockburn, City of Darebin, City of Greater Geelong, City of Kingston, City of Onkaparinga Council, Cowra Council, Cumberland Council (NSW), Forbes Shire Council, Frankston City Council, Gold Coast City Council, Gwydir Shire Council, Hobsons Bay City Council, Hornsby Shire Council, Hume City Council, Impact Apps, Inner West Council (NSW), Ipswich City Council, Knox City Council, Ku-ring-gai Council, Lake Macquarie City Council, Lithgow City Council, Livingstone Shire Council, Loddon Shire Council, Logan City Council, Macedon Ranges Shire Council, Mansfield Shire Council, Maribyrnong Council, Maroondah City Council, Melton City Council, Merri-bek City Council, Moira Shire Council, Moree Plains Shire Council, Moreton Bay, Mosman Council, Nillumbik Shire Council, North Adelaide Waste Management Authority, Penrith City Council, Port Adelaide Enfield, South Australia, Port Macquarie Hastings Council, Port Stephens Council, Queanbeyan-Palerang Regional Council, RecycleSmart, Redland City Council (QLD), Shellharbour City Council, Singleton Council, Snowy Valleys Council, South Burnett Regional Council, Stirling, Stonnington City Council, The Hawkesbury City Council, Sydney, The Hills Shire Council, Sydney, Town of Victoria Park, Townsville, Unley City Council (SA), Wellington Shire Council, Whitehorse City Counfil, Whittlesea City Council, Wollondilly Shire Council, Wollongong City Council, Wyndham City Council, Melbourne, Yarra Ranges Council | | Austria | Abfallverband Hollabrunn, Abfallverband Korneuburg, Abfallverband Schwechat, Abfallwirtschaft der Stadt St. Pölten, Abfallwirtschaft Stadt Krems, Abfallwirtschaft Stadt St Pölten, Afritz am See, Alpbach, Altenmarkt an der Triesting, Althofen, Andau, Angath, Apetlon, App CITIES, Arnoldstein, Aschau im Zillertal, AWV Neunkirchen, AWV Wr. Neustadt, Bad Blumau, Bad Gleichenberg, Bad Häring, Bad Kleinkirchheim, Bad Loipersdorf, Bad Radkersburg, Bad Tatzmannsdorf, Bad Waltersdorf, Baldramsdorf, Berg im Drautal, Berndorf bei Salzburg, Bernstein, Bildein, Brandenberg, Breitenbach am Inn, Breitenbrunn am Neusiedler See, Breitenstein, Bromberg, Bruckneudorf, Buch - St. Magdalena, Burgau, Burgauberg-Neudauberg, Burgenländischer Müllverband, Dechantskirchen, Dellach, Dellach im Drautal, Deutsch Goritz, Deutsch Jahrndorf, Deutsch Kaltenbrunn, Deutschkreutz, Die NÖ Umweltverbände, Dobl-Zwaring, Drasenhofen, Draßmarkt, Ebenthal in Kärnten, Eberau, Eberndorf, Ebersdorf, Eberstein, Edelsbach bei Feldbach, Eggenburg, Eggersdorf bei Graz, Eichgraben, Eisenstadt, Eugendorf, Fehring, Feistritz im Rosental, Feistritz ob Bleiburg, Feldbach, Feldkirchen in Kärnten, Feldkirchen in Kärnten, Ferlach, Ferndorf, Ferndorf, Finkenstein am Faaker See, Frankenau-Unterpullendorf, Frauenkirchen, Frauenstein, Freistadt, Fresach, Friedberg, Frohnleiten, Fürstenfeld, Gabersdorf, GABL, Gattendorf, GAUL Laa an der Thaya, GAUM Mistelbach, GDA Amstetten, Gemeindeverband Horn, Gitschtal, Gitschtal, Globasnitz, Gmünd in Kärnten, Gols, Grafendorf bei Hartberg, Grafenschachen, Grafenstein, Grafenstein, Gratkorn, Gratwein-Straßengel, Greifenburg, Großkirchheim, Großsteinbach, Großwarasdorf, Großwilfersdorf, Gutenberg, Guttaring, GV Gmünd, GV Krems, GV Zwettl, GVA Baden, GVA Baden, GVA Lilienfeld, GVA Mödling, GVA Tulln, GVA Waidhofen/Thaya, GVU Bezirk Gänserndorf, GVU Melk, GVU Scheibbs, GVU Scheibbs, GVU St. Pölten, Güssing, Hagenberg im Mühlkreis, Hannersdorf, Hartberg, Heiligenblut am Großglockner, Heiligenkreuz, Heiligenkreuz am Waasen, Heimschuh, Henndorf am Wallersee, Henndorf am Wallersee, Hermagor-Pressegger See, Hirm, Hofstätten an der Raab, Hopfgarten im Brixental, Horitschon, Horn, Hornstein, Hüttenberg, Ilz, infeo, Innsbrucker Kommunalbetriebe, Inzenhof, Irschen, Jabing, Jagerberg, Kaindorf, Kaisersdorf, Kalsdorf bei Graz, Kapfenstein, Kemeten, Keutschach am See, Kirchbach, Kirchbach-Zerlach, Kirchberg an der Raab, Kirchbichl, Kirchdorf in Tirol, Kittsee, Klagenfurt am Wörthersee, Kleblach-Lind, Kleinmürbisch, Klingenbach, Klosterneuburg, Klöch, Kobersdorf, Kohfidisch, Korneuburg, Krems in Kärnten, Krensdorf, Krumpendorf am Wörthersee, Kuchl, Kundl, Kössen, Köstendorf, Kötschach-Mauthen, Köttmannsdorf, Laa an der Thaya, Lackenbach, Lackendorf, Langau, Langenrohr, Leibnitz, Leithaprodersdorf, Lendorf, Leoben, Lesachtal, Leutschach an der Weinstraße, Lieboch, Linz AG, Litzelsdorf, Lockenhaus, Loipersbach im Burgenland, Ludmannsdorf, Lurnfeld, Magdalensberg, Mallnitz, Malta, Maria Rain, Maria Saal, Maria Wörth, Mariasdorf, Markt Hartmannsdorf, Markt Neuhodis, Marktgemeinde Edlitz, Marz, Mattersburg, Mattsee, Meiseldorf, Melk, Mettersdorf am Saßbach, Miesenbach, Millstatt, Mischendorf, Mistelbach, Mitterdorf an der Raab, Moosburg, Mureck, Mönchhof, Mörbisch am See, Mörtschach, Mühldorf, Müll App, Münster, Neudorf bei Parndorf, Neudörfl, Neufeld an der Leitha, Neumarkt am Wallersee, Neusiedl am See, Neustift bei Güssing, Nickelsdorf, Oberdrauburg, Oberndorf in Tirol, Oberpullendorf, Oberschützen, Obertrum am See, Oberwart, Oslip, Ottendorf an der Rittschein, Ottobrunn, Paldau, Pama, Pamhagen, Parndorf, Paternion, Payerbach, Peggau, Pernegg an der Mur, Pernegg im Waldviertel, Pfarrwerfen, Pilgersdorf, Pinggau, Pinkafeld, Podersdorf am See, Poggersdorf, Poggersdorf, Potzneusiedl, Poysdorf, Pöchlarn, Pörtschach am Wörther See, Raach am Hochgebirge, Raasdorf, Radenthein, Radfeld, Radmer, Ragnitz, Raiding, Ramsau im Zillertal, Rangersdorf, Reichenau, Reichenfels, Reith im Alpbachtal, Reißeck, Rennweg am Katschberg, Rohr bei Hartberg, Rohr im Burgenland, Rudersdorf, Rust, Saalfelden am Steinernen Meer, Sachsenburg, Sankt Georgen an der Stiefing, Sankt Gilgen, Sankt Oswald bei Plankenwarth, Schiefling am Wörthersee, Schleedorf, Schrattenberg, Schwadorf, Schwaz, Schwoich, Schäffern, Schützen am Gebirge, Seeboden, Seeham, Seekirchen am Wallersee, Seiersberg-Pirka, Siegendorf, Sigleß, Sigmundsherberg, Sinabelkirchen, Spittal an der Drau, St. Andrä, St. Andrä, St. Andrä am Zicksee, St. Anna am Aigen, St. Egyden am Steinfeld, St. Georgen an der Leys, St. Jakob im Rosental, St. Jakob im Rosental, St. Johann in der Haide, St. Johann in Tirol, St. Konrad, St. Lorenzen am Wechsel, St. Margareten im Rosental, St. Margarethen an der Raab, St. Margarethen im Burgenland, St. Peter - Freienstein, St. Peter am Ottersbach, St. Ruprecht an der Raab, St. Symvaro, St. Veit in der Südsteiermark, Stadt Salzburg, Stadtgemeinde Traiskirchen, Stadtservice Korneuburg, Stall, Stegersbach, Steinbrunn, Steinfeld, Steuerberg, Stinatz, Stiwoll, Stockenboi, Stockerau, Strass im Zillertal, Straß in Steiermark, Straßwalchen, Söchau, Söll, Tadten, Tattendorf, Techelsberg am Wörther See, Thal, Tieschen, Tobaj, Trebesing, Treffen am Ossiacher See, Tulln an der Donau, Umweltprofis, Umweltv, Unterfrauenhaid, Unterkohlstätten, Unterlamm, Unterwart, Vasoldsberg, Velden am Wörther See, Villach, Vordernberg, Völkermarkt, Völkermarkt, Walpersbach, Wattens, Weiden am See, Weitersfeld, Weiz, Weißensee, Weppersdorf, Werfenweng, Wies, Wiesen, Wiesfleck, Wiesmath, Wimpassing an der Leitha, Winden am See, Winklern, Wolfau, Wolfsberg, Wolfsberg, Wolkersdorf im Weinviertel, WSZ Moosburg, Wulkaprodersdorf, Wörterberg, Zagersdorf, Zelking-Matzleinsdorf, Zell, Zell am Ziller, Zellberg, Zillingtal, Zurndorf, Übelbach | | Belgium | Hygea, Limburg.net, Recycle! | | Canada | Aurora (ON), Calgary (AB), Calgary, AB, City of Edmonton, AB, City of Greater Sudbury, ON, City of Nanaimo, City of Peterborough, ON, City of Vancouver, County of Simcoe, ON, Halifax, NS, Kawartha Lakes (ON), London (ON), Montreal (QC), Ottawa, Canada, RM of Morris, MB, Strathcona County, ON, Toronto (ON), Vaughan (ON), Waste Wise APPS, Winnipeg (MB) |