Skip to content

Commit

Permalink
New source: BIR (#1397)
Browse files Browse the repository at this point in the history
* New source: BIR

* bir_no improvements

---------

Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
  • Loading branch information
odin-h and 5ila5 authored Nov 9, 2023
1 parent 49c9214 commit 70392d5
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ Waste collection schedules in the following formats and countries are supported.
<details>
<summary>Norway</summary>

- [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
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
]
57 changes: 57 additions & 0 deletions doc/source/bir_no.md
Original file line number Diff line number Diff line change
@@ -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 <https://bir.no/> 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
```
2 changes: 1 addition & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down

0 comments on commit 70392d5

Please sign in to comment.