From 761b1c750f36225304e9f58b77d4052e4f67fc31 Mon Sep 17 00:00:00 2001 From: 5ila5 <5ila5@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:40:09 +0200 Subject: [PATCH] add Sammelkalender.ch --- README.md | 5 + .../waste_collection_schedule/sources.json | 33 +++ .../source/sammelkalender_ch.py | 280 ++++++++++++++++++ doc/source/sammelkalender_ch.md | 56 ++++ info.md | 2 +- 5 files changed, 375 insertions(+), 1 deletion(-) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/sammelkalender_ch.py create mode 100644 doc/source/sammelkalender_ch.md diff --git a/README.md b/README.md index 628c9cc4..932274c5 100644 --- a/README.md +++ b/README.md @@ -1355,9 +1355,11 @@ If your service provider is not listed, feel free to open a [source request issu - [Münchenstein](/doc/source/muenchenstein_ch.md) / muenchenstein.ch - [Münsingen BE, Switzerland](/doc/ics/muensingen_ch.md) / muensingen.ch - [Real Luzern](/doc/source/real_luzern_ch.md) / real-luzern.ch +- [Real Luzern](/doc/source/sammelkalender_ch.md) / realluzern.ch - [Rehetobel](/doc/source/a_region_ch.md) / a-region.ch - [Rorschach](/doc/source/a_region_ch.md) / a-region.ch - [Rorschacherberg](/doc/source/a_region_ch.md) / a-region.ch +- [Sammelkalender.ch](/doc/source/sammelkalender_ch.md) / info.sammelkalender.ch - [Schwellbrunn](/doc/source/a_region_ch.md) / a-region.ch - [Schönengrund](/doc/source/a_region_ch.md) / a-region.ch - [Speicher](/doc/source/a_region_ch.md) / a-region.ch @@ -1374,6 +1376,9 @@ If your service provider is not listed, feel free to open a [source request issu - [Waldstatt](/doc/source/a_region_ch.md) / a-region.ch - [Wittenbach](/doc/source/a_region_ch.md) / a-region.ch - [Wolfhalden](/doc/source/a_region_ch.md) / a-region.ch +- [ZAKU Entsorgung](/doc/source/sammelkalender_ch.md) / zaku.ch +- [Zeba](/doc/source/sammelkalender_ch.md) / zebazug.ch +- [ZKRI](/doc/source/sammelkalender_ch.md) / zkri.ch
diff --git a/custom_components/waste_collection_schedule/sources.json b/custom_components/waste_collection_schedule/sources.json index babcb8c8..3d19e4ee 100644 --- a/custom_components/waste_collection_schedule/sources.json +++ b/custom_components/waste_collection_schedule/sources.json @@ -7341,6 +7341,13 @@ "module": "real_luzern_ch", "default_params": {} }, + { + "title": "Real Luzern", + "module": "sammelkalender_ch", + "default_params": { + "service_provider": "real_luzern" + } + }, { "title": "Rehetobel", "module": "a_region_ch", @@ -7362,6 +7369,11 @@ "municipality": "Rorschacherberg" } }, + { + "title": "Sammelkalender.ch", + "module": "sammelkalender_ch", + "default_params": {} + }, { "title": "Schwellbrunn", "module": "a_region_ch", @@ -7473,6 +7485,27 @@ "default_params": { "municipality": "Wolfhalden" } + }, + { + "title": "ZAKU Entsorgung", + "module": "sammelkalender_ch", + "default_params": { + "service_provider": "zaku" + } + }, + { + "title": "Zeba", + "module": "sammelkalender_ch", + "default_params": { + "service_provider": "zeba" + } + }, + { + "title": "ZKRI", + "module": "sammelkalender_ch", + "default_params": { + "service_provider": "zkri" + } } ], "United Kingdom": [ diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/sammelkalender_ch.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/sammelkalender_ch.py new file mode 100644 index 00000000..1337a111 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/sammelkalender_ch.py @@ -0,0 +1,280 @@ +from datetime import datetime +from typing import Literal + +import requests +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +TITLE = "Sammelkalender.ch" +DESCRIPTION = "Source for Sammelkalender.ch." +URL = "https://info.sammelkalender.ch" +TEST_CASES = { + "zeba Baar Aberenrain 10": { + "service_provider": "zeba", + "municipality": "Baar", + "street": "Aberenrain", + "hnr": 10, + }, + "zkri Rothenthurm": { + "service_provider": "zkri", + "municipality": "Rothenthurm", + }, + "ZKRI, Schwyz Seewen": { + "service_provider": "zkri", + "municipality": "Schwyz", + "street": "Seewen", + }, + "Real Luzern, Meggen": { + "service_provider": "real_luzern", + "municipality": "Meggen", + }, + "Real Luzern, Malters, Bodenmättli": { + "service_provider": "real_luzern", + "municipality": "Malters", + "street": "Bodenmättli", + }, + "Zaku Altdorf dag": { + "service_provider": "zaku", + "municipality": "Altdorf", + "street": "dag", + }, +} + + +ICON_MAP = { + "Kehricht": "mdi:trash-can", + "Christbaum": "mdi:pine-tree", + "Grüngut": "mdi:leaf", + "Karton": "mdi:package-variant-closed", + "Papier": "mdi:newspaper", + "Alteisen/Metall": "mdi:screw-flat-top", +} + +SERVICES = { + "zeba": { + "title": "Zeba", + "url": "https://www.zebazug.ch", + "api_url_search": "https://www.zeba.sammelkalender.ch/kunden_WANN_auswahl_form.php", + "api_url_bin": "https://www.zeba.sammelkalender.ch/kunden_sammlung_ausw_cont.php", + }, + "zkri": { + "title": "ZKRI", + "url": "https://zkri.ch", + "api_url_search": "https://daten.zkri.ch/web/Sammelkalender/kunden_WANN_auswahl_form.php", + "api_url_bin": "https://daten.zkri.ch/web/Sammelkalender/kunden_sammlung_ausw_cont.php", + }, + "real_luzern": { + "title": "Real Luzern", + "url": "https://www.realluzern.ch", + "api_url_search": "https://www.real.sammelkalender.ch/app/appSelect.php", + "api_url_bin": "https://www.real.sammelkalender.ch/app/appSammeldaten.php", + }, + "zaku": { + "title": "ZAKU Entsorgung", + "url": "https://www.zaku.ch", + "api_url_search": "https://www.zaku.sammelkalender.ch/appSelect.php", + "api_url_bin": "https://www.zaku.sammelkalender.ch/appSammeldaten.php", + }, +} + +EXTRA_INFO = [ + { + "title": s["title"], + "url": s["url"], + "default_params": { + "service_provider": key, + }, + } + for key, s in SERVICES.items() +] + + +PROVIDER_LITERALS = Literal["zeba", "zkri", "real_luzern", "zaku"] + + +API_URL = "" + + +class Source: + def __init__( + self, + service_provider: PROVIDER_LITERALS, + municipality: str, + street: str | None = None, + hnr: str | int | None = None, + ) -> None: + service_provider_str = service_provider.lower() + if service_provider not in SERVICES: + raise ValueError(f"Invalid service provider: {service_provider_str}") + self._search_url = SERVICES[service_provider_str]["api_url_search"] + self._bin_url = SERVICES[service_provider_str]["api_url_bin"] + + self._municipality: str = municipality + self._street: str | None = street + self._hnr: str = str(hnr) + + self._municipality_id: str | None = None + self._address_id: str | None = None + + @staticmethod + def _compare(s1: str, s2: str | list[str]) -> bool: + if isinstance(s2, str): + s2 = [s2] + + for s in s2: + if s1.lower().replace(" ", "").replace("str.", "straße").replace( + "strasse", "straße" + ) == s.lower().replace(" ", "").replace("str.", "straße").replace( + "strasse", "straße" + ): + return True + return False + + def _get_streets(self, sage: bool = False) -> list[dict]: + params: dict[str, str | int | None] = { + "optGem": self._municipality_id, + "Jahr": None, + } + if sage: + params["fuerSage"] = 1 + else: + params["fuerStr"] = 1 + + r = requests.get(self._search_url, params=params) + r.raise_for_status() + streets = r.json() + + if len(streets) == 0: + return [] + return streets + + def _fetch_sage(self) -> None: + sages = self._get_streets(sage=True) + if not sages: + return + if len(sages) == 1: + self._address_id = sages[0]["SAGEid"] + return + for sage in sages: + if not self._street: + street_names = list( + {s.get("STRname") or s.get("SAGEname") for s in sages} + ) + raise ValueError(f"Street required, use one of {street_names}") + if self._compare(self._street, [sage["SAGEabk"], sage["SAGEname"]]): + self._address_id = sage["SAGEid"] + break + if not self._address_id: + raise ValueError( + f"Invalid street: {self._street}, use one of {list({s['SAGEname'] for s in sages})}" + ) + + def _fetch_street(self) -> None: + streets = self._get_streets() + if not streets: + self._fetch_sage() + return + + self._address_id = None + street_matches = [] + if len(streets) == 1: + street_matches = streets + else: + if not self._street: + street_names = list( + {s.get("STRname") or s.get("SAGEname") for s in streets} + ) + raise ValueError(f"Street required, use one of {street_names}") + for street in streets: + if self._compare(street["STRname"], self._street): + street_matches.append(street) + + if not street_matches: + raise ValueError( + f"Invalid street: {self._street}, use one of {list({s['STRname'] for s in streets})}" + ) + + for street in street_matches: + if street["STRhausnr"] and self._hnr is None: + raise ValueError( + f"House number required, use one of {list({s['STRhausnr'] for s in street_matches})}" + ) + if not street["STRhausnr"] or self._compare(street["STRhausnr"], self._hnr): + self._address_id = street["SAGEid"] + break + + if not self._address_id: + raise ValueError( + f"Invalid house number: {self._hnr}, use one of {list({s['STRhausnr'] for s in street_matches})}" + ) + + def _fetch_ids(self) -> None: + params = { + "optGem": "GEM", + "Jahr": None, + } + + # get json file + r = requests.get(self._search_url, params=params) + r.raise_for_status() + self._municipality_id = None + for mun in r.json(): + if self._compare(mun["GEMname"], self._municipality): + self._municipality_id = mun["GEMid"] + break + if not self._municipality_id: + raise ValueError( + f"Invalid municipality: {self._municipality}, use one of {[m['GEMname'] for m in r.json()]}" + ) + + self._fetch_street() + + def fetch(self) -> list[Collection]: + fresh_ids = False + if self._municipality_id is None: + fresh_ids = True + self._fetch_ids() + try: + return self._get_collections() + except Exception: + if fresh_ids: + raise + self._fetch_ids() + return self._get_collections() + + def _get_collections(self) -> list[Collection]: + today = datetime.now() + year = today.year + entries = self._get_collections_year(year) + if today.month == 12: + try: + entries += self._get_collections_year(year + 1) + except Exception: + pass + return entries + + def _get_collections_year(self, year: int) -> list[Collection]: + if self._municipality is None: + raise ValueError("Municipality required") + + params: dict[str, str | int | None] = { + "nGem": self._municipality_id, + "nAbar": "null", + "nSage": self._address_id, + "sDatum": "", + "delsel": "no", + "jahr1": year, + } + r = requests.get(self._bin_url, params=params) + r.raise_for_status() + + entries = [] + for d in r.json(): + date = datetime.strptime(d["DATUM"], "%Y-%m-%d").date() + bin_types = d["AbarOne"] + if bin_types is None: + continue + for bin_type in bin_types.split("-"): + icon = ICON_MAP.get(bin_type) # Collection icon + entries.append(Collection(date=date, t=bin_type, icon=icon)) + + return entries diff --git a/doc/source/sammelkalender_ch.md b/doc/source/sammelkalender_ch.md new file mode 100644 index 00000000..ec87d4f7 --- /dev/null +++ b/doc/source/sammelkalender_ch.md @@ -0,0 +1,56 @@ +# Sammelkalender.ch + +Support for schedules provided by [Sammelkalender.ch](https://info.sammelkalender.ch), serving multiple regions in Switzerland. + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: sammelkalender_ch + args: + service_provider: SERVICE PROVIDER + municipality: MUNICIPALITY (Gemeinde) + street: STREET (Straße) + hnr: HOUSE NUMBER (Hausnummer) +``` + +### Configuration Variables + +**service_provider** +*(String) (required)* + +**municipality** +*(String) (required)* + +**street** +*(String) (optional)* only required if the form asks for it + +**hnr** +*(String) (optional)* only required if the form asks for it + +Supported service providers are: + +- zeba: https://www.zebazug.ch +- zkri: https://zkri.ch +- real_luzern: https://www.realluzern.ch +- zaku: https://www.zaku.ch + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: sammelkalender_ch + args: + service_provider: zeba + municipality: Baar + street: Aberenrain +``` + +## How to get the source argument + +You can check if your parameters work by visiting the website of the service provider and entering your address. Or using the app: + +- [IOS AppStore](https://apps.apple.com/ch/app/sammelkalender/id1502137213?l) +- [Android PlayStore](https://play.google.com/store/apps/details?id=ch.sammelkalender.app2020) diff --git a/info.md b/info.md index 4378095f..2ac63bcb 100644 --- a/info.md +++ b/info.md @@ -35,7 +35,7 @@ Waste collection schedules from service provider web sites are updated daily, de | Poland | App Moje Odpady, Bydgoszcz Pronatura, Ecoharmonogram, Gmina Miękinia, Koziegłowy/Objezierze/Oborniki, Poznań, Warsaw, Wrocław | | Slovenia | Moji odpadki, Ljubljana | | Sweden | Affärsverken, Boden, Borås Energi och Miljö, EDPEvent - Multi Source, Gästrike Återvinnare, Jönköping - June Avfall & Miljö, Landskrona - Svalövs Renhållning, Lerum Vatten och Avlopp, Linköping - Tekniska Verken, Luleå, Lund Waste Collection, Mölndal, Norrtalje Vatten & Avfall, North / Middle Bohuslän - Rambo AB, Region Gotland, Ronneby Miljöteknik, Roslagsvatten, Samverkan Återvinning Miljö (SÅM), Skellefteå, SRV Återvinning, SSAM (Deprecated), SSAM Södra Smalånds Avfall & Miljö, Sysav Sophämntning, Uppsala Vatten, Uppsala Vatten och Avfall AB (Deprecated), VA Syd Sophämntning, VIVAB Sophämtning, Västervik Miljö & Energi | -| Switzerland | A-Region, Alchenstorf, Andwil, Appenzell, Berg, Bühler, Canton of Zürich, Eggersriet, Gais, Gaiserwald, Goldach, Grosswangen, Grub, Heiden, Herisau, Horn, Hundwil, Häggenschwil, Lindau, Lutzenberg, Muolen, Mörschwil, Münchenstein, Münsingen BE, Switzerland, Real Luzern, Rehetobel, Rorschach, Rorschacherberg, Schwellbrunn, Schönengrund, Speicher, Stein, Steinach, Teufen, Thal, Trogen, Tübach, Untereggen, Urnäsch, Wald, Waldkirch, Waldstatt, Wittenbach, Wolfhalden | +| Switzerland | A-Region, Alchenstorf, Andwil, Appenzell, Berg, Bühler, Canton of Zürich, Eggersriet, Gais, Gaiserwald, Goldach, Grosswangen, Grub, Heiden, Herisau, Horn, Hundwil, Häggenschwil, Lindau, Lutzenberg, Muolen, Mörschwil, Münchenstein, Münsingen BE, Switzerland, Real Luzern, Real Luzern, Rehetobel, Rorschach, Rorschacherberg, Sammelkalender.ch, Schwellbrunn, Schönengrund, Speicher, Stein, Steinach, Teufen, Thal, Trogen, Tübach, Untereggen, Urnäsch, Wald, Waldkirch, Waldstatt, Wittenbach, Wolfhalden, ZAKU Entsorgung, Zeba, ZKRI | | United Kingdom | Aberdeenshire Council, Adur & Worthing Councils, Allerdale Borough Council, Amber Valley Borough Council, Anglesey, Antrim and Newtownabbey, Apps by imactivate, Ards and North Down Borough Council, Arun District Council, Ashfield District Council, Ashford Borough Council, Aylesbury Vale District Council, Barnsley Metropolitan Borough Council, Basildon Council, Basingstoke and Deane Borough Council, Bath & North East Somerset Council, BCP Council, Bedford Borough Council, Binzone, Birmingham City Council, Blackburn with Darwen Borough Council, Blackpool Council, Borough Council of King's Lynn & West Norfolk, Borough of Broxbourne Council, Bracknell Forest Council, Bradford Metropolitan District Council, Braintree District Council, Breckland Council, Brent Council, Bristol City Council, Broadland District Council, Bromsgrove City Council, Broxtowe Borough Council, Buckinghamshire Waste Collection - Former Chiltern, South Bucks or Wycombe areas, Burnley Council, Bury Council, Cambridge City Council, Canterbury City Council, Cardiff Council, Central Bedfordshire Council, Charnwood, Cherwell District Council, Cheshire East Council, Cheshire West and Chester Council, Chesterfield Borough Council, Chichester District Council, City of Doncaster Council, City Of Lincoln Council, City of York Council, Colchester City Council, Conwy County Borough Council, Cornwall Council, Crawley Borough Council (myCrawley), Croydon Council, Darlington Borough Council, Denbighshire County Council, Derby City Council, Dudley Metropolitan Borough Council, Durham County Council, East Ayrshire Council, East Cambridgeshire District Council, East Devon District Council, East Herts Council, East Lothian, East Northamptonshire and Wellingborough, East Renfrewshire Council, East Riding of Yorkshire Council, Eastbourne Borough Council, Eastleigh Borough Council, Elmbridge Borough Council, Environment First, Exeter City Council, Falkirk, Fareham Borough Council, FCC Environment, Fenland, Fenland District Council, Fife Council, Flintshire, Fylde Council, Gateshead Council, Gedling Borough Council (unofficial), Glasgow City Council, Guildford Borough Council, Gwynedd, Harborough District Council, Haringey Council, Harlow Council, Hart District Council, Herefordshire City Council, High Peak Borough Council, Highland, Horsham District Council, Hull City Council, Huntingdonshire District Council, iTouchVision, Joint Waste Solutions, Kirklees Council, Lancaster City Council, Leeds, Leicester City Council, Lewes District Council, Lichfield District Council, Lisburn and Castlereagh City Council, Liverpool City Council, London Borough of Barking and Dagenham, London Borough of Bexley, London Borough of Bromley, London Borough of Camden, London Borough of Harrow, London Borough of Hounslow, London Borough of Lewisham, London Borough of Merton, London Borough of Newham, Luton, Maidstone Borough Council, Maldon District Council, Malvern Hills, Malvern Hills District Council, Manchester City Council, Mansfield District Council, Mendip District Council, Mid-Sussex District Council, Middlesbrough Council, Milton Keynes council, Moray Council, Newark & Sherwood District Council, Newcastle City Council, Newcastle Under Lyme Borough Council, Newport City Council, North Ayrshire Council, North Herts Council, North Kesteven District Council, North Lincolnshire Council, North Northamptonshire council, North Somerset Council, North West Leicestershire District Council, North Yorkshire Council - Hambleton, North Yorkshire Council - Harrogate, North Yorkshire Council - Scarborough, North Yorkshire Council - Selby, Nottingham City Council, Oxford City Council, Peterborough City Council, Portsmouth City Council, Reading Council, Redbridge Council, Reigate & Banstead Borough Council, Renfrewshire Council, Rhondda Cynon Taf County Borough Council, Richmondshire District Council, Rotherham, Rotherham Metropolitan Borough Council, Runnymede Borough Council, Rushcliffe Brough Council, Rushmoor Borough Council, Salford City Council, Sedgemoor District Council, Sheffield City Council, Shropshire Council, Solihull Council, Somerset Council, Somerset County Council, Somerset West & Taunton District Council, South Cambridgeshire District Council, South Derbyshire District Council, South Gloucestershire Council, South Hams District Council, South Holland District Council, South Kesteven District Council, South Norfolk Council, South Oxfordshire District Council, South Somerset District Council, South Tyneside Council, Southampton City Council, St Albans City & District Council, Stafford Borough Council, Stevenage Borough Council, Stirling.gov.uk, Stockport Council, Stockton-on-Tees Borough Council, Stoke-on-Trent, Stratford District Council, Stroud District Council, Surrey Heath Borough Council, Sutton Council, London, Swansea Council, Swindon Borough Council, Tameside Metropolitan Borough Council, Telford and Wrekin Council, Test Valley Borough Council, Tewkesbury Borough Council, The Royal Borough of Kingston Council, Tonbridge and Malling Borough Council, Tunbridge Wells, UK Bin Collection Schedule (UKBCD) project, Uttlesford District Council, Vale of Glamorgan Council, Vale of White Horse District Council, Walsall Council, Warrington Borough Council, Warwick District Council, Waverley Borough Council, Wealden District Council, Welwyn Hatfield Borough Council, West Berkshire Council, West Devon Borough Council, West Dunbartonshire Council, West Northamptonshire council, West Oxfordshire District Council, West Suffolk Council, Westmorland & Furness Council, Barrow area, Westmorland & Furness Council, South Lakeland area, Wigan Council, Wiltshire Council, Windsor and Maidenhead, Wirral Council, Woking Borough Council, Wokingham Borough Council, Worcester City, Wychavon, Wychavon District Council (Deprecated), Wyre Forest District Council | | United States of America | Albuquerque, New Mexico, USA, City of Austin, TX, City of Bloomington, City of Cambridge, City of Gastonia, NC, City of Georgetown, TX, City of McKinney, TX, City of Oklahoma City, City of Pittsburgh, Louisville, Kentucky, USA, Newark, Delaware, USA, Olympia, Washington, USA, ReCollect, Recycle Coach, Republic Services, Seattle Public Utilities, Tucson, Arizona, USA, Waste Connections |