-
-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New source: BIR * bir_no improvements --------- Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
custom_components/waste_collection_schedule/waste_collection_schedule/source/bir_no.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters