Skip to content

Commit

Permalink
add rd4_nl serving multiple municipalities
Browse files Browse the repository at this point in the history
  • Loading branch information
5ila5 committed Aug 6, 2024
1 parent 7c5c32a commit a343cfb
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,7 @@ If your service provider is not listed, feel free to open a [source request issu
- [PreZero](/doc/source/hvcgroep_nl.md) / prezero.nl
- [Purmerend](/doc/source/hvcgroep_nl.md) / purmerend.nl
- [RAD BV](/doc/source/ximmio_nl.md) / radbv.nl
- [Rd4](/doc/source/rd4_nl.md) / rd4.nl
- [Reinis](/doc/source/ximmio_nl.md) / reinis.nl
- [Spaarnelanden](/doc/source/hvcgroep_nl.md) / spaarnelanden.nl
- [Twente Milieu](/doc/source/ximmio_nl.md) / twentemilieu.nl
Expand Down
5 changes: 5 additions & 0 deletions custom_components/waste_collection_schedule/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -6213,6 +6213,11 @@
"company": "rad"
}
},
{
"title": "Rd4",
"module": "rd4_nl",
"default_params": {}
},
{
"title": "Reinis",
"module": "ximmio_nl",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from datetime import datetime

import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]

TITLE = "Rd4"
DESCRIPTION = "Source for Rd4."
URL = "https://rd4.nl/"
TEST_CASES = {"6417 AT 32": {"postal_code": "6417 AT", "house_number": 32}}


ICON_MAP = {
"pmd": "mdi:recycle",
"gft": "mdi:leaf",
"residual_waste": "mdi:trash-can",
"paper": "mdi:newspaper",
"pruning_waste": "mdi:leaf",
"best_bag": "mdi:bag-personal",
"christmas_trees": "mdi:christmas-tree",
}


API_URL = "https://data.rd4.nl/api/v1/waste-calendar"


class Source:
def __init__(self, postal_code: str, house_number: str | int):
self._postal_code: str = postal_code
self._house_number: str | int = house_number

def fetch(self) -> list[Collection]:
now = datetime.now()
year = now.year
entries = []
exception = None
try:
entries = self._get_collections(year)
except Exception as e:
# Do not fail in december to try to fetch next year
if now.month != 12:
raise
exception = e

if now.month != 12:
return entries

# Fetch next year in December
year += 1
try:
return entries + self._get_collections(year)
except Exception:
if exception:
raise exception
return entries

def _get_collections(self, year) -> list[Collection]:
args = {
"postal_code": self._postal_code,
"house_number": self._house_number,
"year": year,
}

# get json file
r = requests.get(API_URL, params=args)
r.raise_for_status()
data = r.json()
if not (
"data" in data
and "items" in data["data"]
and len(data["data"]["items"]) > 0
):
raise ValueError("No data found, check your arguments")

entries = []

for item in data["data"]["items"]:
for collection in item:
date_ = datetime.strptime(collection["date"], "%Y-%m-%d").date()
bin_type = collection["type"]

icon = ICON_MAP.get(bin_type) # Collection icon
entries.append(Collection(date=date_, t=bin_type, icon=icon))

return entries
39 changes: 39 additions & 0 deletions doc/source/rd4_nl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Rd4

Support for schedules provided by [Rd4](https://rd4.nl/), serving multiple municipalities in the Netherlands.

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: rd4_nl
args:
postal_code: POSTAL CODE (postcode)
house_number: "HOUSE NUMBER (huisnummer)"

```
### Configuration Variables
**postal_code**
*(String) (required)*
**house_number**
*(String | Integer) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: rd4_nl
args:
postal_code: 6417 AT
house_number: "32"

```
## How to get the source argument
Use your postal code and house number. You can verify that your parameter are valid by entering them in the form at the [Rd4 website](https://mijn.rd4.nl/afvalkalender).
2 changes: 1 addition & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Waste collection schedules from service provider web sites are updated daily, de
| Italy | Contarina S.p.A |
| Lithuania | Kauno švara, Telšių keliai |
| 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, Ôffalkalinder van Noardeast-Fryslân & Dantumadiel |
| 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, Rd4, Reinis, Spaarnelanden, Twente Milieu, Waardlanden, Ximmio, ZRD, Ôffalkalinder van Noardeast-Fryslân & Dantumadiel |
| New Zealand | Auckland Council, Christchurch City Council, Dunedin District Council, Gore, Invercargill & Southland, Hamilton City Council, Horowhenua District Council, Hutt City Council, Porirua City, Rotorua Lakes Council, Tauranga City Council, Waipa District Council, Wellington City Council |
| Norway | BIR (Bergensområdets Interkommunale Renovasjonsselskap), Fosen Renovasjon, IRiS, Min Renovasjon, Movar IKS, Oslo Kommune, ReMidt Orkland muni, Sandnes Kommune, Stavanger Kommune, Trondheim |
| Poland | App Moje Odpady, Bydgoszcz Pronatura, Ecoharmonogram, Gmina Miękinia, Koziegłowy/Objezierze/Oborniki, Poznań, Warsaw, Wrocław |
Expand Down

0 comments on commit a343cfb

Please sign in to comment.