From b80f71bf9b8ec8f0b1602da4a92fb6945ef3b121 Mon Sep 17 00:00:00 2001 From: dt215git <86194065+dt215git@users.noreply.github.com> Date: Mon, 6 Mar 2023 20:35:17 +0000 Subject: [PATCH] address arg implemented --- .../source/midsussex_gov_uk.py | 18 +++++-- doc/source/midsussex_gov_uk.md | 47 ++++++++++++++++++- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/midsussex_gov_uk.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/midsussex_gov_uk.py index dd8f9cb5..3835ee8e 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/midsussex_gov_uk.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/midsussex_gov_uk.py @@ -1,5 +1,7 @@ +import re import requests + from bs4 import BeautifulSoup from datetime import datetime from waste_collection_schedule import Collection # type: ignore[attr-defined] @@ -12,6 +14,7 @@ TEST_CASES = { "Test_001": {"house_number": "6", "street": "Withypitts", "postcode": "RH10 4PJ"}, "Test_002": {"house_name": "Oaklands", "street": "Oaklands Road", "postcode": "RH16 1SS"}, "Test_003": {"house_number": 9, "street": "Bolnore Road", "postcode": "RH16 4AB"}, + "Test_004": {"address": "HAZELMERE REST HOME, 21 BOLNORE ROAD RH16 4AB"} } ICON_MAP = { @@ -21,25 +24,30 @@ ICON_MAP = { } API_URL = "https://www.midsussex.gov.uk/waste-recycling/bin-collection/" +REGEX = "([A-Z]{1,2}\d[A-Z\d]?\s*\d[A-Z]{2})" # regex for UK postcode format class Source: - def __init__(self, house_name="", house_number="", street="", postcode=""): + def __init__(self, house_name="", house_number="", street="", postcode="", address=""): self._house_name = str(house_name).upper() self._house_number = str(house_number) self._street = str(street).upper() self._postcode = str(postcode).upper() + self._address = str(address).upper() def fetch(self): s = requests.Session() - if self._house_name == "": - address = self._house_number + " " + self._street + " " + self._postcode + if self._address != "": + # extract postcode + self._postcode = re.findall(REGEX, self._address) + elif self._house_name == "": + self._address = self._house_number + " " + self._street + " " + self._postcode else: - address = self._house_name + "," + self._house_number + " " + self._street + " " + self._postcode + self._address = self._house_name + "," + self._house_number + " " + self._street + " " + self._postcode payload = { "PostCodeStep.strAddressSearch": self._postcode, - "AddressStep.strAddressSelect": address, + "AddressStep.strAddressSelect": self._address, "Next": "true", "StepIndex": "1", } diff --git a/doc/source/midsussex_gov_uk.md b/doc/source/midsussex_gov_uk.md index 084f46b5..6a9a90cf 100644 --- a/doc/source/midsussex_gov_uk.md +++ b/doc/source/midsussex_gov_uk.md @@ -9,6 +9,7 @@ waste_collection_schedule: sources: - name: midsussex_gov_uk args: + address: ADDRESS house_name: NAME house_number: NUMBER street: STREET @@ -17,6 +18,16 @@ waste_collection_schedule: ### Configuration Variables +#### Preferred Method +**address** +*(string) (required)* + +The address as it appears on the midsussex.gov.uk website. + +This is the preferred approach, if this is not used then the following legacy methods can be used. + +#### Legacy Methods + **house_name** *(string) (optional)* @@ -34,6 +45,16 @@ If house_name is not provided then house_number becomes *(required)* ## Example ```yaml +# Preferred method +waste_collection_schedule: + sources: + - name: midsussex_gov_uk + args: + address: HAZELMERE REST HOME, 21 BOLNORE ROAD RH16 4AB +``` + +```yaml +# legacy method: house name, but no house number waste_collection_schedule: sources: - name: midsussex_gov_uk @@ -44,6 +65,7 @@ waste_collection_schedule: ``` ```yaml +# legacy method: house number, but no house name waste_collection_schedule: sources: - name: midsussex_gov_uk @@ -53,6 +75,29 @@ waste_collection_schedule: postcode: RH10 4PJ ``` +```yaml +# legacy method: house name used instead of house number +waste_collection_schedule: + sources: + - name: midsussex_gov_uk + args: + house_number: Lamedos + street: Withypitts + postcode: RH11 4XY +``` +```yaml + +# legacy method: house name and house number +waste_collection_schedule: + sources: + - name: midsussex_gov_uk + args: + house_name: Tireggub + house_number: Lamedos + street: Widdershins + postcode: RH14 8BX +``` + ## How to get the source arguments -Search for your collection schedule on the address on the [Mid-Sussex District Council](https://www.midsussex.gov.uk/waste-recycling/bin-collection/) site to see how they format your address. General rule seems to be `HOUSE_NAME, HOUSE_NUMBER STREET POSTCODE` but it can vary for multi-occupancy buildings etc, so you may need to adjust which parts of the address are used for each arg. \ No newline at end of file +Search for your collection schedule on the address on the [Mid-Sussex District Council](https://www.midsussex.gov.uk/waste-recycling/bin-collection/) site to see how they format your address. Preferred approach is to copy the address as displayed. If that doesn't work, the individual components can be supplied. General rule seems to be `HOUSE_NAME, HOUSE_NUMBER STREET POSTCODE` but it can vary for multi-occupancy buildings, house names where there are no numbers, house names where there are also house numbers, etc, so you may need to adjust which parts of the address are used for each arg. \ No newline at end of file