Skip to content

Commit

Permalink
New Source : BEP-Environnement (Belgium) (#3355)
Browse files Browse the repository at this point in the history
* Add support for BEP-Environnement (Belgium)

* Fix: Use date object not datetime

* fix: use SourceArgumentNotFoundWithSuggestions instead of SourceArgumentException
doc: HOW_TO_GET_ARGUMENTS_DESCRIPTION completed
refactor: rename Cities keyword with localities

* reformatting + fixing typehint + fix exception

* ./update_docu_links.py

---------

Co-authored-by: 5ila5 <[email protected]>
  • Loading branch information
amof and 5ila5 authored Jan 8, 2025
1 parent cd60780 commit 052c922
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ If your service provider is not listed, feel free to open a [source request issu
<details>
<summary>Belgium</summary>

- [Bep-Environnement](/doc/source/bep_environnement_be.md) / bep-environnement.be
- [Hygea](/doc/source/hygea_be.md) / hygea.be
- [Ittre](/doc/source/ittre_be.md) / ittre.be
- [Limburg.net](/doc/ics/limburg_net.md) / limburg.net
Expand Down
6 changes: 6 additions & 0 deletions custom_components/waste_collection_schedule/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -3558,6 +3558,12 @@
}
],
"Belgium": [
{
"title": "Bep-Environnement",
"module": "bep_environnement_be",
"default_params": {},
"id": "bep_environnement_be"
},
{
"title": "Hygea",
"module": "hygea_be",
Expand Down
20 changes: 20 additions & 0 deletions custom_components/waste_collection_schedule/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,26 @@
},
"data_description": {}
},
"args_bep_environnement_be": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. \n\nGo to the \"https://www.bep-environnement.be\" website if you're unsure about your locality.\n\nMehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/bep_environnement_be.md",
"data": {
"calendar_title": "Kalender Titel",
"locality": "Locality"
},
"data_description": {
"calendar_title": "Ein lesbarerer oder benutzerfreundlicherer Name für den Müllkalender. Wenn nichts angegeben wird, wird der Name der Quelle verwendet."
}
},
"reconfigure_bep_environnement_be": {
"title": "Quelle Neu Konfigurieren",
"description": "Konfiguriere deinen Service Provider. \n\nGo to the \"https://www.bep-environnement.be\" website if you're unsure about your locality.\n\nMehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/bep_environnement_be.md",
"data": {
"calendar_title": "Kalender Titel",
"locality": "Locality"
},
"data_description": {}
},
"args_hygea_be": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/hygea_be.md",
Expand Down
23 changes: 23 additions & 0 deletions custom_components/waste_collection_schedule/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,29 @@
},
"data_description": {}
},
"args_bep_environnement_be": {
"title": "Configure Source",
"description": "Configure your service provider. \n\nGo to the \"https://www.bep-environnement.be\" website if you're unsure about your locality.\n\nMore details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/bep_environnement_be.md.",
"data": {
"calendar_title": "Calendar Title",
"locality": "Locality"
},
"data_description": {
"calendar_title": "A more readable, or user-friendly, name for the waste calendar. If nothing is provided, the name returned by the source will be used.",
"locality": "Name of the locality"
}
},
"reconfigure_bep_environnement_be": {
"title": "Reconfigure Source",
"description": "Configure your service provider. \n\nGo to the \"https://www.bep-environnement.be\" website if you're unsure about your locality.\n\nMore details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/bep_environnement_be.md.",
"data": {
"calendar_title": "Calendar Title",
"locality": "Locality"
},
"data_description": {
"locality": "Name of the locality"
}
},
"args_hygea_be": {
"title": "Configure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/hygea_be.md.",
Expand Down
20 changes: 20 additions & 0 deletions custom_components/waste_collection_schedule/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,26 @@
},
"data_description": {}
},
"args_bep_environnement_be": {
"title": "Configurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. \n\nGo to the \"https://www.bep-environnement.be\" website if you're unsure about your locality.\n\nMaggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/bep_environnement_be.md.",
"data": {
"calendar_title": "Nome Calendario",
"locality": "Locality"
},
"data_description": {
"calendar_title": "Puoi cambiare il nome del calendario della raccolta dei rifiuti, altrimenti di default verra' utilizzato il nome del tuo fornitore di servizi."
}
},
"reconfigure_bep_environnement_be": {
"title": "Riconfigurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. \n\nGo to the \"https://www.bep-environnement.be\" website if you're unsure about your locality.\n\nPer maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/bep_environnement_be.md.",
"data": {
"calendar_title": "Nome Calendario",
"locality": "Locality"
},
"data_description": {}
},
"args_hygea_be": {
"title": "Configurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. Maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/hygea_be.md.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import json
from datetime import date
from typing import List

import requests
from bs4 import BeautifulSoup
from waste_collection_schedule import Collection
from waste_collection_schedule.exceptions import SourceArgumentNotFoundWithSuggestions

TITLE = "Bep-Environnement"
DESCRIPTION = "Source for Bep Environnement garbage collection" # Describe your source
# Insert url to service homepage. URL will show up in README.md and info.md
URL = "https://www.bep-environnement.be"
TEST_CASES = { # Insert arguments for test cases to be used by test_sources.py script
"Dinant": {"locality": "Dinant"},
}

WASTE_MAP = {
# Déchers ménagers + organique
"dmorga": {"type": "DM & Organiques", "icon": "mdi:trash-can"},
# PMC
"pmc": {"type": "PMC", "icon": "mdi:recycle"},
# Papiers cartons
"papierscartons": {"type": "Papiers & Cartons", "icon": "mdi:leaf"},
}

# ### Arguments affecting the configuration GUI ####

HOW_TO_GET_ARGUMENTS_DESCRIPTION = { # Optional dictionary to describe how to get the arguments, will be shown in the GUI configuration form above the input fields, does not need to be translated in all languages
"en": 'Go to the "https://www.bep-environnement.be" website if you\'re unsure about your locality.',
}

PARAM_DESCRIPTIONS = { # Optional dict to describe the arguments, will be shown in the GUI configuration below the respective input field
"en": {"locality": "Name of the locality"}
}


def GetLocalities() -> dict[str, str]:
"""Return id for each locality available in calendar.
Returns:
List[dict]: key is the city, value is the id for the calendar
"""
response = requests.get(URL)
soup = BeautifulSoup(response.text, "html.parser")

# Find all <option> elements inside the select
options = soup.select("#locform-loc option")

# Create a dictionary of city names and their values
localities = {
option.text.lower(): option["value"]
for option in options
if option.text.strip()
}

# Print the result
return localities


def BepWasteParser(response: dict) -> List[Collection]:
"""Specific implementation to parse response from Bep-Environement.
Args:
response (dict): response from the website
Returns:
List[Collection]: list of Collection found
"""
collections = []
cal_soup = BeautifulSoup(response.get("cal", ""), "html.parser")
cells = cal_soup.find_all("td")

for cell in cells:
# Extract attributes
data_day = cell.get("data-day")
data_month = cell.get("data-month")
data_year = cell.get("data-year")

# Check for waste types in the class attribute
classes = cell.get("class", [])
waste_types = [cls for cls in classes if cls in WASTE_MAP]

if data_day and data_month and data_year and waste_types:
# Construct the date
collection_date = date(
year=int(data_year), month=int(data_month), day=int(data_day)
)

for abbr, map in WASTE_MAP.items():
if abbr in waste_types:
c = Collection(
date=collection_date, t=map["type"], icon=map["icon"]
)
collections.append(c)

return collections


class Source:
BEP_CALENDAR_URL = "https://www.bep-environnement.be/wp-admin/admin-ajax.php"
GARBAGE_COLLECTION_ACTION = "calendriercollectes"

def __init__(self, locality: str):
self._locality = locality.lower()

def fetch(self) -> list[Collection]:
# Check for city name given
localities = GetLocalities()

if self._locality not in localities:
raise SourceArgumentNotFoundWithSuggestions(
"locality", self._locality, localities.keys()
)

# Make the request to get the data
params = {
"action": self.GARBAGE_COLLECTION_ACTION,
"locID": localities[self._locality],
}
response = requests.get(self.BEP_CALENDAR_URL, params=params)
response.raise_for_status()
data = json.loads(response.text)

# Parse the response
entries = BepWasteParser(data)

return entries
33 changes: 33 additions & 0 deletions doc/source/bep_environnement_be.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# BEP Environnement

Support for schedules provided by [bep-environnement.be](https://www.bep-environnement.be/).

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: bep_environnement_be
args:
locality: LOCALITY
```
The arguments can be found in the URL after visiting the [the calendar](https://www.bep-environnement.be/). Type your *Postal Code* or *City Name* then select the **Locality**. This is the value you have to give in argument.
### Configuration Variables
**locality**
*(string)*
Name of your Locality (Localité).
## Example
```yaml
# URL: https://www.bep-environnement.be/

waste_collection_schedule:
sources:
- name: bep_environnement_be
args:
locality: "Dinant"
```
Loading

0 comments on commit 052c922

Please sign in to comment.