Skip to content

Commit

Permalink
Adding Sefton Council (#2947)
Browse files Browse the repository at this point in the history
* Adding Sefton Council

Gets bin collection days for Sefton Council. Includes suggested changes from 5ila5

* refromatting + adding missing import + fixing test_cases

---------

Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
  • Loading branch information
northerngeek and 5ila5 authored Oct 30, 2024
1 parent 1c520c7 commit 23ef38f
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,7 @@ If your service provider is not listed, feel free to open a [source request issu
- [Rushmoor Borough Council](/doc/source/rushmoor_gov_uk.md) / rushmoor.gov.uk
- [Salford City Council](/doc/source/salford_gov_uk.md) / salford.gov.uk
- [Sedgemoor District Council](/doc/source/iweb_itouchvision_com.md) / somerset.gov.uk
- [Sefton Council](/doc/source/sefton_gov_uk.md) / sefton.gov.uk
- [Sheffield City Council](/doc/source/sheffield_gov_uk.md) / sheffield.gov.uk
- [Shropshire Council](/doc/source/shropshire_gov_uk.md) / shropshire.gov.uk
- [Solihull Council](/doc/source/solihull_gov_uk.md) / denbighshire.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 @@ -13451,6 +13451,12 @@
},
"id": "iweb_itouchvision_com"
},
{
"title": "Sefton Council",
"module": "sefton_gov_uk",
"default_params": {},
"id": "sefton_gov_uk"
},
{
"title": "Sheffield City Council",
"module": "sheffield_gov_uk",
Expand Down
24 changes: 24 additions & 0 deletions custom_components/waste_collection_schedule/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -15092,6 +15092,30 @@
"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_sefton_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. \n\nUsing a browser, go to [sefton.gov.uk](https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/). For _Postcode_ and _Street name_ use the values you'd enter on Sefton's first page.Search, and then for _House Name or Number_ you need the value that comes before the street name you entered on the first screen.e.g. if your streetname is 'Liverpool Road' and the select box has an option of '1A Liverpool Road' enter '1A' as your _House Name or Number_.\n\nMehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/sefton_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"house_number_or_name": "Hausnummer oder Name",
"postcode": "PLZ",
"streetname": "Streetname"
},
"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_sefton_gov_uk": {
"title": "Quelle Neu Konfigurieren",
"description": "Konfiguriere deinen Service Provider. \n\nUsing a browser, go to [sefton.gov.uk](https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/). For _Postcode_ and _Street name_ use the values you'd enter on Sefton's first page.Search, and then for _House Name or Number_ you need the value that comes before the street name you entered on the first screen.e.g. if your streetname is 'Liverpool Road' and the select box has an option of '1A Liverpool Road' enter '1A' as your _House Name or Number_.\n\nMehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/sefton_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"house_number_or_name": "Hausnummer oder Name",
"postcode": "PLZ",
"streetname": "Streetname"
},
"data_description": {}
},
"args_sheffield_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/sheffield_gov_uk.md",
Expand Down
31 changes: 31 additions & 0 deletions custom_components/waste_collection_schedule/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15105,6 +15105,37 @@
"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_sefton_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. \n\nUsing a browser, go to [sefton.gov.uk](https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/). For _Postcode_ and _Street name_ use the values you'd enter on Sefton's first page.Search, and then for _House Name or Number_ you need the value that comes before the street name you entered on the first screen.e.g. if your streetname is 'Liverpool Road' and the select box has an option of '1A Liverpool Road' enter '1A' as your _House Name or Number_.\n\nMore details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/sefton_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"house_number_or_name": "House Number Or Name",
"postcode": "Postcode",
"streetname": "Streetname"
},
"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.",
"house_number_or_name": "House name or number",
"postcode": "Postcode",
"streetname": "Street name"
}
},
"reconfigure_sefton_gov_uk": {
"title": "Reconfigure Source",
"description": "Configure your service provider. \n\nUsing a browser, go to [sefton.gov.uk](https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/). For _Postcode_ and _Street name_ use the values you'd enter on Sefton's first page.Search, and then for _House Name or Number_ you need the value that comes before the street name you entered on the first screen.e.g. if your streetname is 'Liverpool Road' and the select box has an option of '1A Liverpool Road' enter '1A' as your _House Name or Number_.\n\nMore details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/sefton_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"house_number_or_name": "House Number Or Name",
"postcode": "Postcode",
"streetname": "Streetname"
},
"data_description": {
"house_number_or_name": "House name or number",
"postcode": "Postcode",
"streetname": "Street name"
}
},
"args_sheffield_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/sheffield_gov_uk.md.",
Expand Down
24 changes: 24 additions & 0 deletions custom_components/waste_collection_schedule/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -15081,6 +15081,30 @@
"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_sefton_gov_uk": {
"title": "Configurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. \n\nUsing a browser, go to [sefton.gov.uk](https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/). For _Postcode_ and _Street name_ use the values you'd enter on Sefton's first page.Search, and then for _House Name or Number_ you need the value that comes before the street name you entered on the first screen.e.g. if your streetname is 'Liverpool Road' and the select box has an option of '1A Liverpool Road' enter '1A' as your _House Name or Number_.\n\nMaggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/sefton_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"house_number_or_name": "House Number Or Name",
"postcode": "Codice Postale CAP",
"streetname": "Streetname"
},
"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_sefton_gov_uk": {
"title": "Riconfigurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. \n\nUsing a browser, go to [sefton.gov.uk](https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/). For _Postcode_ and _Street name_ use the values you'd enter on Sefton's first page.Search, and then for _House Name or Number_ you need the value that comes before the street name you entered on the first screen.e.g. if your streetname is 'Liverpool Road' and the select box has an option of '1A Liverpool Road' enter '1A' as your _House Name or Number_.\n\nPer maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/sefton_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"house_number_or_name": "House Number Or Name",
"postcode": "Codice Postale CAP",
"streetname": "Streetname"
},
"data_description": {}
},
"args_sheffield_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/sheffield_gov_uk.md.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import datetime

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

TITLE = "Sefton Council" # Title will show up in README.md and info.md
DESCRIPTION = "Source for Sefton Council, UK" # Describe your source
URL = "https://www.sefton.gov.uk/" # Insert url to service homepage. URL will show up in README.md and info.md
TEST_CASES = { # Insert arguments for test cases to be used by test_sources.py script
"Issue2369": {
"house_number_or_name": "1",
"streetname": "Ken Mews",
"postcode": "L20 6GF",
},
"Housename": {
"house_number_or_name": "Gladstone House",
"streetname": "Rosemary Lane",
"postcode": "L37 3JB",
},
"Issue2496": {
"house_number_or_name": 22,
"streetname": "Elton Avenue",
"postcode": "L23 8UW",
},
}

API_URL = "https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/"
ICON_MAP = { # Optional: Dict of waste types and suitable mdi icons
"RESIDUAL": "mdi:trash-can",
"RECYCLING": "mdi:recycle",
"GREEN": "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": "Using a browser, go to [sefton.gov.uk](https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/). "
"For _Postcode_ and _Street name_ use the values you'd enter on Sefton's first page."
"Search, and then for _House Name or Number_ you need the value that comes before the street name you entered on the first screen."
"e.g. if your streetname is 'Liverpool Road' and the select box has an option of '1A Liverpool Road' enter '1A' as your _House Name or Number_."
}

PARAM_DESCRIPTIONS = { # Optional dict to describe the arguments, will be shown in the GUI configuration below the respective input field
"en": {
"house_number_or_name": "House name or number",
"streetname": "Street name",
"postcode": "Postcode",
}
}


# ### End of arguments affecting the configuration GUI ####


class Source:
def __init__(
self, house_number_or_name: str | int, streetname: str, postcode: str
): # argX correspond to the args dict in the source configuration
self._house_number_or_name = str(house_number_or_name).upper()
self._streetname = streetname
self._postcode = postcode

def fetch(self) -> list[Collection]:
with requests.Session() as sess:
request = sess.get(
"https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/"
)

soup = BeautifulSoup(request.content, "html.parser")
hidden = soup.find_all("input", {"type": "hidden"}, limit=2)
payload = {x["name"]: x["value"] for x in hidden}
payload["Postcode"] = self._postcode
payload["Streetname"] = self._streetname
request = sess.post(
"https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/",
data=payload,
)
# We should now have the page displaying the select list for addresses, parse again to find the form elements we need.
soup = BeautifulSoup(request.content, "html.parser")
hidden = soup.find_all("input", {"type": "hidden"})
payload = {x["name"]: x["value"] for x in hidden}
payload["action"] = "Select"
option_tags = soup.select("select option")
for option in option_tags:
if option.text.upper().strip().startswith(self._house_number_or_name):
payload["selectedValue"] = option["value"]
break
if "selectedValue" not in payload:
raise SourceArgumentNotFoundWithSuggestions(
"houseNumberOrName",
self._house_number_or_name,
[option.text.strip() for option in option_tags],
)
request = sess.post(
"https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/",
data=payload,
)
soup = BeautifulSoup(request.content, "html.parser")
tables = soup.find_all("table")
entries = []
if len(tables) > 0:
for table in tables:
binType = table.td.text.split()[0]
binCollectionDate = datetime.datetime.strptime(
table.td.findNext("td").findNext("td").text, "%d/%m/%Y"
).date()
entries.append(
Collection(
date=binCollectionDate,
t=binType,
icon=ICON_MAP.get(binType.upper()),
)
)
else:
raise Exception(
"No entries could be parsed, check the Sefton website is working and your arguments exactly match what you enter online."
)

return entries
43 changes: 43 additions & 0 deletions doc/source/sefton_gov_uk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Sefton Council

Support for schedules provided by [Sefton Council](https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/), serving the
district of Sefton, UK.

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: sefton_gov_uk
args:
postcode: Postcode
streetname: Streetname
house_number_or_name: House number or name
```
### Configuration Variables
**postcode**
*(string) (required)*
**streetname**
*(string) (required)*
**house_number_or_name**
*(string) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: sefton_gov_uk
args:
postcode: "L20 6GF"
streetname: "Ken Mews"
house_number_or_name: "1"
```
## How to find the values for arguments above
Go to the Sefton "When is my Bin Collection Day?" page - the postcode and streetname arguments are the same values you'd enter on the first page of the form. The House Number or Name value is the string/number that appears before the streetname in the dropdown on the second page - e.g. if the dropdown shows "1A Liverpool Road" you should enter "1A" as your House Number or Name value.
Loading

0 comments on commit 23ef38f

Please sign in to comment.