Added source for Sheffield City Council

Added source for Sheffield City Council with support for all bin types.
I never got Dev Containers working to properly test, however I have manually added this as a source to my instance of Home Assistant/HACS and tested fine.
This commit is contained in:
NSF12345
2022-11-19 15:24:25 +00:00
parent 85363f969d
commit 3a0c81bc4b
2 changed files with 109 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
import urllib.request
from bs4 import BeautifulSoup
from dateutil import parser
import logging
from waste_collection_schedule import Collection
TITLE = "Sheffield.gov.uk"
DESCRIPTION = (
"Source for waste collection services from Sheffield City Council (SCC)"
)
# Base URL for waste collection services
URL = "https://wasteservices.sheffield.gov.uk/"
# Headers to mimic the browser
HEADERS = {
"user-agent": "Mozilla/5.0",
}
TEST_CASES = {
# These are random addresses around Sheffield
# If your property is listed here and you don't want it, please raise an issue and I'll amend
"test001" : {"uprn": "100050938234"},
"test002" : {"uprn": "100050961380"},
"test003" : {"uprn": "100050920796"},
}
# Icons for the different bin types
ICONS = {
"BLACK": "mdi:delete-empty", # General Waste
"BROWN": "mdi:glass-fragile", # Glass, Tins, Cans & Plastics
"BLUE": "mdi:newspaper", # Paper & Cardboard
"GREEN": "mdi:leaf", # Garden Waste
}
_LOGGER = logging.getLogger(__name__)
class Source:
def __init__(self, uprn=None):
self._uprn = str(uprn)
def fetch(self):
if self._uprn:
# Get the page containing bin details
# /calendar gives further future informaion over just the "Services" page
req = urllib.request.Request(f"{URL}/property/{self._uprn}/calendar",headers=HEADERS)
with urllib.request.urlopen(req) as response:
html_doc = response.read()
# Parse the page to get the data required (collection date and type)
soup = BeautifulSoup(html_doc, 'html.parser')
entries = []
# Find all entries relating to bin collection & loop through them
for collection in soup.find_all('div',{"class":"calendar-table-cell"}):
try:
# The collection details are in the title field of each ul/li
# Converting date from title field to a usable format
collection_date = parser.parse(collection.ul.li['title'].split(" - ")[0]).date()
# Getting the collection type
collection_type = collection.ul.li['title'].split(" - ")[1]
# Append to entries for main program
entries.append(
Collection(
date = collection_date,
t = collection_type,
icon = ICONS.get(collection_type.replace(" Bin","").upper()),
)
)
except ValueError:
pass
return entries

View File

@@ -0,0 +1,35 @@
# Sheffield City Council
Support for schedules provided by [Sheffield City Council](https://wasteservices.sheffield.gov.uk/).
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: sheffield_gov_uk
args:
uprn: UNIQUE_PROPERTY_REFERENCE_NUMBER
```
### Configuration Variables
**uprn**<br>
*(string) (optional) (preferred method)*
This is required if you do not supply any other options. Using a UPRN removes the need to do an address look up using web requests.
## Example using UPRN
```yaml
waste_collection_schedule:
sources:
- name: sheffield_gov_uk
args:
uprn: "100050938234"
```
## How to find your `UPRN`
An easy way to find your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details.
Otherwise you can inspect the URL on [Sheffield City Council's Waste Services](https://wasteservices.sheffield.gov.uk/) site having searched for and selected your address details. Your UPRN is the collection of digits at the end of the URL (before /calendar), for example: *https://wasteservices.sheffield.gov.uk/property/`100050938234`* or *https://wasteservices.sheffield.gov.uk/property/`100050938234`/calendar*