From 70392d5636fd007e5b233b85fa61768ac8caaa7a Mon Sep 17 00:00:00 2001 From: odin-h <150337574+odin-h@users.noreply.github.com> Date: Thu, 9 Nov 2023 13:05:24 +0100 Subject: [PATCH] New source: BIR (#1397) * New source: BIR * bir_no improvements --------- Co-authored-by: 5ila5 <5ila5@users.noreply.github.com> --- README.md | 1 + .../source/bir_no.py | 82 +++++++++++++++++++ doc/source/bir_no.md | 57 +++++++++++++ info.md | 2 +- 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/bir_no.py create mode 100644 doc/source/bir_no.md diff --git a/README.md b/README.md index d35223e2f..7b17b5353 100644 --- a/README.md +++ b/README.md @@ -879,6 +879,7 @@ Waste collection schedules in the following formats and countries are supported.
Norway +- [BIR (Bergensområdets Interkommunale Renovasjonsselskap)](/doc/source/bir_no.md) / bir.no - [IRiS](/doc/source/iris_salten_no.md) / iris-salten.no - [Min Renovasjon](/doc/source/minrenovasjon_no.md) / norkart.no - [Movar IKS](/doc/source/movar_no.md) / movar.no diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/bir_no.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/bir_no.py new file mode 100644 index 000000000..3de5651a2 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/bir_no.py @@ -0,0 +1,82 @@ +import datetime + +import requests +from lxml import etree +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +TITLE = "BIR (Bergensområdets Interkommunale Renovasjonsselskap)" +DESCRIPTION = "Askøy, Bergen, Bjørnafjorden, Eidfjord, Kvam, Osterøy, Samnanger, Ulvik, Vaksdal, Øygarden og Voss Kommune (Norway)." +URL = "https://bir.no" + +TEST_CASES = { + "Villa Paradiso": { + "street_name": "Nordåsgrenda", + "house_number": 7, + "house_letter": "", + }, + "Mardalsrenen 12 B": { + "street_name": "Mardalsrenen", + "house_number": "11", + }, + "Alf Bondes Veg 13 B": { + "street_name": "Alf Bondes Veg", + "house_number": "13", + "house_letter": "B", + }, +} + +API_URL = "https://bir.no/api/search/AddressSearch" +ICON_MAP = {"restavfall": "mdi:trash-can", "papir": "mdi:newspaper-variant-multiple"} + + +def map_icon(text): + for key, value in ICON_MAP.items(): + if key in text: + return value + return "mdi:trash-can" + + +class Source: + def __init__(self, street_name, house_number, house_letter=""): + self._street_name = street_name + self._house_number = house_number + self._house_letter = house_letter + + def fetch(self): + headers = {"user-agent": "Home-Assitant-waste-col-sched/0.1"} + + args = { + "q": f"{self._street_name} {self._house_number}{self._house_letter} ", # The space at the end is serving as a termination character for the query + "s": False, + } + + r = requests.get(API_URL, params=args, headers=headers) + r = requests.get( + f"{URL}/adressesoek/toemmekalender", + params={"rId": {r.json()[0]["Id"]}}, + headers=headers, + ) + doc = etree.HTML(r.content) + month_containers = doc.cssselect( + ".main-content .address-page-box .month-container" + ) + + return [ + Collection( + date=datetime.datetime.strptime( + f"{date.text.replace('des','dec').replace('mai','may').replace('okt','oct')} {container.cssselect('.month-title')[0].text.strip().split(' ')[1]}", + "%d. %b %Y", + ).date(), + t=doc.cssselect( + f'.trash-categories > .trash-row > img[src="{category_row.cssselect(".icon > img")[0].get("src")}"] + .trash-text' + )[0].text, + icon=map_icon( + doc.cssselect( + f'.trash-categories > .trash-row > img[src="{category_row.cssselect(".icon > img")[0].get("src")}"] + .trash-text' + )[0].text.lower() + ), + ) + for container in month_containers + for category_row in container.cssselect(".category-row") + for date in category_row.cssselect(".date-item > .date-item-date") + ] diff --git a/doc/source/bir_no.md b/doc/source/bir_no.md new file mode 100644 index 000000000..b65521fe5 --- /dev/null +++ b/doc/source/bir_no.md @@ -0,0 +1,57 @@ +# BIR - Bergensområdets Interkommunale Renovasjonsselskap + +```yaml +waste_collection_schedule: + sources: + - name: bir_no + args: + street_name: "" + house_number: "" + house_letter: "" +``` + +### Configuration Variables + +**street_name** +*(string) (required)* + +**house_number** +*(string|Integer) (required)* + +**house_letter** +*(string) (optional)* + +The arguments should be written exactly like on the website + +# Example configuration.yaml: + +```yaml +# Waste collection +waste_collection_schedule: + sources: + - name: bir_no + args: + street_name: "Alf Bondes Veg" + house_number: "13" + house_letter: "B" + customize: + - type: blue bin + alias: Papir + - type: green bin + alias: Restavfall + +# Optional Sensors +sensor: + - platform: waste_collection_schedule + name: next_collection + - platform: waste_collection_schedule + name: waste_collection_garbage + details_format: upcoming + types: + - Restavfall + - platform: waste_collection_schedule + name: waste_collection_paper + details_format: upcoming + types: + - Papir og plastemballasje +``` \ No newline at end of file diff --git a/info.md b/info.md index ba3dd9b73..cd94e5140 100644 --- a/info.md +++ b/info.md @@ -28,7 +28,7 @@ Waste collection schedules from service provider web sites are updated daily, de | Luxembourg | Esch-sur-Alzette | | Netherlands | ACV Group, Alpen an den Rijn, Area Afval, Avalex, Avri, Bar Afvalbeheer, Circulus, Cyclus NV, Dar, Den Haag, GAD, Gemeente Almere, Gemeente Berkelland, Gemeente Cranendonck, Gemeente Hellendoorn, Gemeente Lingewaard, Gemeente Meppel, Gemeente Middelburg + Vlissingen, Gemeente Peel en Maas, Gemeente Schouwen-Duiveland, Gemeente Sudwest-Fryslan, Gemeente Venray, Gemeente Voorschoten, Gemeente Waalre, Gemeente Westland, HVC Groep, Meerlanden, Mijn Blink, PreZero, Purmerend, RAD BV, Reinis, Spaarnelanden, Twente Milieu, Waardlanden, Ximmio, ZRD | | New Zealand | Auckland Council, Christchurch City Council, Dunedin District Council, Gore, Invercargill & Southland, Hamilton City Council, Horowhenua District Council, Waipa District Council, Wellington City Council | -| Norway | IRiS, Min Renovasjon, Movar IKS, Oslo Kommune, ReMidt Orkland muni, Sandnes Kommune, Stavanger Kommune, Trondheim | +| Norway | BIR (Bergensområdets Interkommunale Renovasjonsselskap), IRiS, Min Renovasjon, Movar IKS, Oslo Kommune, ReMidt Orkland muni, Sandnes Kommune, Stavanger Kommune, Trondheim | | Poland | Ecoharmonogram, Poznań/Koziegłowy/Objezierze/Oborniki, Warsaw, Wrocław | | Slovenia | Moji odpadki, Ljubljana | | Sweden | Affärsverken, Jönköping - June Avfall & Miljö, Landskrona - Svalövs Renhållning, Lerum Vatten och Avlopp, Linköping - Tekniska Verken, Region Gotland, Ronneby Miljöteknik, Samverkan Återvinning Miljö (SÅM), SRV Återvinning, SSAM, Sysav Sophämntning, Uppsala Vatten och Avfall AB, VA Syd Sophämntning |