Skip to content

Commit

Permalink
Add Rother District Council (#3391)
Browse files Browse the repository at this point in the history
* Add Rother District Council

* reformatting

---------

Co-authored-by: 5ila5 <[email protected]>
  • Loading branch information
sarumait and 5ila5 authored Jan 6, 2025
1 parent ece2deb commit 2cb7ccd
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,7 @@ If your service provider is not listed, feel free to open a [source request issu
- [Rhondda Cynon Taf County Borough Council](/doc/source/rctcbc_gov_uk.md) / rctcbc.gov.uk
- [Richmondshire District Council](/doc/source/richmondshire_gov_uk.md) / richmondshire.gov.uk
- [Rochdale Borough Council](/doc/source/rochdale_gov_uk.md) / rochdale.gov.uk
- [Rother District Council](/doc/source/rother_gov_uk.md) / rother.gov.uk
- [Rotherham](/doc/source/apps_imactivate_com.md) / rotherham.gov.uk
- [Rotherham Metropolitan Borough Council](/doc/source/rotherham_gov_uk.md) / rotherham.gov.uk
- [Royal Borough Of Greenwich](/doc/source/royalgreenwich_gov_uk.md) / royalgreenwich.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 @@ -14725,6 +14725,12 @@
"default_params": {},
"id": "rochdale_gov_uk"
},
{
"title": "Rother District Council",
"module": "rother_gov_uk",
"default_params": {},
"id": "rother_gov_uk"
},
{
"title": "Rotherham",
"module": "apps_imactivate_com",
Expand Down
23 changes: 23 additions & 0 deletions custom_components/waste_collection_schedule/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -15973,6 +15973,29 @@
"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_rother_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rother_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"uprn": "UPRN"
},
"data_description": {
"calendar_title": "Ein lesbarerer oder benutzerfreundlicherer Name für den Müllkalender. Wenn nichts angegeben wird, wird der Name der Quelle verwendet.",
"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."
}
},
"reconfigure_rother_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/rother_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"uprn": "UPRN"
},
"data_description": {
"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_rotherham_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rotherham_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 @@ -16018,6 +16018,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_rother_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rother_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"uprn": "UPRN"
},
"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.",
"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."
}
},
"reconfigure_rother_gov_uk": {
"title": "Reconfigure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rother_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"uprn": "UPRN"
},
"data_description": {
"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_rotherham_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rotherham_gov_uk.md.",
Expand Down
23 changes: 23 additions & 0 deletions custom_components/waste_collection_schedule/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -15934,6 +15934,29 @@
"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_rother_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/rother_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"uprn": "UPRN"
},
"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.",
"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."
}
},
"reconfigure_rother_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/rother_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"uprn": "UPRN"
},
"data_description": {
"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_rotherham_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/rotherham_gov_uk.md.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import re
from datetime import datetime

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

# With verify=True the POST fails due to a SSLCertVerificationError.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

TITLE = "Rother District Council"
DESCRIPTION = "Source for Rother District Council."
URL = "https://www.rother.gov.uk"
TEST_CASES = {
"Test_01": {"uprn": 10002653856},
"Test_02": {"uprn": 100060102891},
}


ICON_MAP: dict[str, str] = {
"refuse": "mdi:trash-can",
"garden": "mdi:leaf",
"recycling": "mdi:recycle",
}


API_URL = "https://www.rother.gov.uk/wp-admin/admin-ajax.php"


HEADERS = {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"User-Agent": "Mozilla/5.0",
}


class Source:
def __init__(self, uprn: str | int):
self._uprn: str | int = uprn

def fetch(self):
data = {"action": "get_address_data", "uprn": self._uprn}

r = requests.post(API_URL, headers=HEADERS, data=data, timeout=10, verify=False)
r.raise_for_status()

# Response is JSON with fields: success(boolean) and data (string containing HTML fragment)
bin_collection_data = r.json()
self.__validate_response(bin_collection_data)

soup = BeautifulSoup(bin_collection_data["data"], "html.parser")

entries = []
for bin_type in ICON_MAP:
date_span = soup.find("span", class_=f"find-my-nearest-bindays-{bin_type}")
if date_span:
date_str = date_span.text.strip()

if date_str.startswith("Sign up"):
# Garden waste requires a subscription and may not be available for all
continue

parsed_collection_date = datetime.strptime(
self.__remove_ordinal_indicators(date_str), "%A %d %B"
)

# The returned bin day does not include the year
collection_year = self.__resolve_bin_collection_year(
parsed_collection_date.month
)

parsed_collection_date = parsed_collection_date.replace(
year=collection_year
)

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

if not entries:
raise Exception("Unable to find any bin collection schedules")

return entries

def __validate_response(self, bin_collection_data):
if "success" not in bin_collection_data or "data" not in bin_collection_data:
raise Exception("Rother Disctrict Council returned an invalid response")

if not bin_collection_data["success"]:
raise Exception(
"Rother Disctrict Council returned a non-successful response"
)

def __remove_ordinal_indicators(self, original_str: str) -> str:
return re.sub(r"(\d)(st|nd|rd|th)", r"\1", original_str)

def __resolve_bin_collection_year(self, collection_month: int) -> int:
today = datetime.now()
collection_year = today.year

# Collection date is in previous year
if collection_month == 12 and today.month == 1:
collection_year -= 1

# Collection date is in the next year
elif collection_month == 1 and today.month == 12:
collection_year += 1

return collection_year
34 changes: 34 additions & 0 deletions doc/source/rother_gov_uk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Rother District Council

Support for schedules provided by [Rother District Council](https://www.rother.gov.uk), serving Rother District Council, UK.

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: rother_gov_uk
args:
uprn: "UNIQUE_PROPERTY_REFERENCE_NUMBER"

```
### Configuration Variables
**uprn**
*(String | Integer) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: rother_gov_uk
args:
uprn: "10002653856"

```
## How to get the UPRN
Use your address to search for your bin days on the council's website [rother.gov.uk/findmynearest](https://www.rother.gov.uk/findmynearest/). The UPRN will be displayed above the bin collection days.
Loading

0 comments on commit 2cb7ccd

Please sign in to comment.