Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Source: Coventry City Council, UK #3332

Merged
merged 4 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,7 @@ If your service provider is not listed, feel free to open a [source request issu
- [Colchester City Council](/doc/source/colchester_gov_uk.md) / colchester.gov.uk
- [Conwy County Borough Council](/doc/source/conwy_gov_uk.md) / conwy.gov.uk
- [Cornwall Council](/doc/source/cornwall_gov_uk.md) / cornwall.gov.uk
- [Coventry City Council](/doc/source/coventry_gov_uk.md) / coventry.gov.uk
- [Crawley Borough Council (myCrawley)](/doc/source/crawley_gov_uk.md) / crawley.gov.uk
- [Croydon Council](/doc/source/croydon_gov_uk.md) / croydon.gov.uk
- [Dacorum Borough Council](/doc/source/dacorum_gov_uk.md) / dacorum.gov.uk
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 @@ -13070,6 +13070,12 @@
"default_params": {},
"id": "cornwall_gov_uk"
},
{
"title": "Coventry City Council",
"module": "coventry_gov_uk",
"default_params": {},
"id": "coventry_gov_uk"
},
{
"title": "Crawley Borough Council (myCrawley)",
"module": "crawley_gov_uk",
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 @@ -13251,6 +13251,26 @@
"uprn": "Eine einfache Möglichkeit, Ihre Unique Property Reference Number (UPRN) zu finden, besteht darin, auf https://www.findmyaddress.co.uk/ zu gehen und Ihre Adressdaten einzugeben."
}
},
"args_coventry_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/coventry_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"street": "Straße"
},
"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_coventry_gov_uk": {
"title": "Quelle Neu Konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/coventry_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"street": "Straße"
},
"data_description": {}
},
"args_crawley_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/crawley_gov_uk.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 @@ -13277,6 +13277,29 @@
"uprn": "An easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details."
}
},
"args_coventry_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/coventry_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"street": "Your street name, as it appears on the Coventry City Council website"
},
"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.",
"street": "Your street name, as it appears on the Coventry City Council website"
}
},
"reconfigure_coventry_gov_uk": {
"title": "Reconfigure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/coventry_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"street": "Your street name, as it appears on the Coventry City Council website"
},
"data_description": {
"street": "Your street name, as it appears on the Coventry City Council website"
}
},
"args_crawley_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/crawley_gov_uk.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 @@ -13212,6 +13212,26 @@
"uprn": "Un modo facile per scoprire il tuo Numero di Riferimento Proprietà Unica (UPRN) è andare su https://www.findmyaddress.co.uk/ e inserire i dettagli del tuo indirizzo."
}
},
"args_coventry_gov_uk": {
"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/coventry_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"street": "Strada"
},
"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_coventry_gov_uk": {
"title": "Riconfigurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. Per maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/coventry_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"street": "Strada"
},
"data_description": {}
},
"args_crawley_gov_uk": {
"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/crawley_gov_uk.md.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from datetime import date, datetime, timedelta

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

TITLE = "Coventry City Council"
DESCRIPTION = "Source for waste collection services for Coventry City Council"
URL = "https://www.coventry.gov.uk/"

HEADERS = {"user-agent": "Mozilla/5.0"}
API_URLS = {
"search": "https://www.coventry.gov.uk/directory/search",
"directory_record": "https://www.coventry.gov.uk",
}
TEST_CASES = {
"Test_001": {
"street": "Linwood Drive",
},
"Test_002": {
"street": "Cromwell Lane",
},
"Test_003": {
"street": "Lutterworth Road",
},
}
ICON_MAP = {
"green-lidded (rubbish) bin": "mdi:trash-can",
"blue-lidded (recycling) bin": "mdi:recycle",
"brown-lidded (garden waste) bin": "mdi:leaf",
}

PARAM_TRANSLATIONS = {
"en": {
"street": "Your street name, as it appears on the Coventry City Council website",
}
}
PARAM_DESCRIPTIONS = {
"en": {
"street": "Your street name, as it appears on the Coventry City Council website",
}
}


class Source:
def __init__(self, street: str):
self._street: str = street

def append_year(self, d: str) -> date:
# Website dates don't have the year.
# Append the current year, and then check to see if the date is in the past.
# If it is, increment the year by 1.
today: date = datetime.now().date()
year: int = today.year
dt: date = datetime.strptime(f"{d} {str(year)}", "%A %d %B %Y").date()
if (dt - today) < timedelta(days=-31):
dt = dt.replace(year=dt.year + 1)
return dt

def fetch(self) -> list[Collection]:
s = requests.Session()

# search for address to get directory record
params: dict = {
"directoryID": "82",
"showInMap": "",
"keywords": self._street,
"search": "Search",
}
r = s.get(API_URLS["search"], headers=HEADERS, params=params)
soup: BeautifulSoup = BeautifulSoup(r.content, "html.parser")
list_links: list = soup.find_all("a", {"class": "list__link"})
for link in list_links:
if self._street.upper() in link.text.upper():
directory_record: str = link["href"]

# use directory record to get collection day
r = s.get(API_URLS["directory_record"] + directory_record, headers=HEADERS)
soup = BeautifulSoup(r.content, "html.parser")
buttons: list = soup.find_all("a", {"class": "button"})
for button in buttons:
if "bins" in button["href"]:
schedule: str = button["href"]

# use collction day to get schedule
r = s.get(schedule, headers=HEADERS)
soup = BeautifulSoup(r.content, "html.parser")
widgets: list = soup.find_all("div", {"class": "widget-content"})
entries: list = []
for widget in widgets:
list_items: list = widget.find_all("li")
for item in list_items:
waste_date: date = self.append_year(item.text.split(": ")[0])
waste_types: list = item.text.split(": ")[1].split(" and ")
for waste_type in waste_types:
entries.append(
Collection(
date=waste_date,
t=waste_type,
icon=ICON_MAP.get(waste_type),
)
)

return entries
28 changes: 28 additions & 0 deletions doc/source/coventry_gov_uk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Coventry City Council

Support for schedules provided by [Coventry City Council](https://www.coventry.gov.uk/rubbishandrecycling), serving Coventry, UK.

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: coventry_gov_uk
args:
street: STREET
```

### Configuration Variables
**street**
*(string) (required)*<br>
Your street name as it appears on the Coventry City Council website.

## Example

```yaml
waste_collection_schedule:
sources:
- name: coventry_gov_uk
args:
street: "Linwood Drive"
```
Loading
Loading