From 3a0c81bc4bba975437aeeeeac6f4a65e16f06a06 Mon Sep 17 00:00:00 2001 From: NSF12345 Date: Sat, 19 Nov 2022 15:24:25 +0000 Subject: [PATCH] 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. --- .../source/sheffield_gov_uk.py | 74 +++++++++++++++++++ doc/source/sheffield_gov_uk.md | 35 +++++++++ 2 files changed, 109 insertions(+) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/sheffield_gov_uk.py create mode 100644 doc/source/sheffield_gov_uk.md diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/sheffield_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/sheffield_gov_uk.py new file mode 100644 index 00000000..bba1eb8d --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/sheffield_gov_uk.py @@ -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 \ No newline at end of file diff --git a/doc/source/sheffield_gov_uk.md b/doc/source/sheffield_gov_uk.md new file mode 100644 index 00000000..82aa8800 --- /dev/null +++ b/doc/source/sheffield_gov_uk.md @@ -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**
+*(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* \ No newline at end of file