forked from mampfes/hacs_waste_collection_schedule
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Source: Bath & North East Somerset (mampfes#1015)
* Add Bath & North East Somerset source * Fix doc Heading
- Loading branch information
Showing
4 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...m_components/waste_collection_schedule/waste_collection_schedule/source/bathnes_gov_uk.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from datetime import date, datetime | ||
from typing import List | ||
|
||
import requests | ||
from waste_collection_schedule import Collection | ||
|
||
# Include work around for SSL UNSAFE_LEGACY_RENEGOTIATION_DISABLED error | ||
from waste_collection_schedule.service.SSLError import get_legacy_session | ||
|
||
TITLE = "Bath & North East Somerset Council" | ||
DESCRIPTION = ( | ||
"Source for bathnes.gov.uk services for Bath & North East Somerset Council" | ||
) | ||
URL = "https://bathnes.gov.uk" | ||
TEST_CASES = { | ||
"uprn": {"uprn": "10001138699"}, | ||
"houseNumber": {"postcode": "BA1 2LR", "housenameornumber": 1}, | ||
"houseName": {"postcode": "BA2 9AZ", "housenameornumber": "All Saints Church"}, | ||
} | ||
|
||
TYPES = { | ||
"residual": {"icon": "mdi:trash-can", "alias": "Rubbish"}, | ||
"recycling": {"icon": "mdi:recycle", "alias": "Recycling"}, | ||
"organic": {"icon": "mdi:leaf", "alias": "Garden Waste"}, | ||
} | ||
|
||
|
||
class Source: | ||
def __init__(self, uprn=None, postcode=None, housenameornumber=None): | ||
self._postcode = postcode | ||
self._housenameornumber = str(housenameornumber) | ||
self._uprn = uprn | ||
|
||
def fetch(self) -> List[Collection]: | ||
session = get_legacy_session() | ||
|
||
if self._uprn is None: | ||
self._uprn = self.get_uprn(session) | ||
|
||
info = session.get( | ||
f"https://www.bathnes.gov.uk/webapi/api/BinsAPI/v2/getbartecroute/{self._uprn}/true" | ||
).json() | ||
|
||
entries = [] | ||
for type, props in TYPES.items(): | ||
for dateType in ["Previous", "Next"]: | ||
if info.get(f"{type}Route", "NS") == "NS": | ||
continue | ||
|
||
entries.append( | ||
Collection( | ||
date=datetime.fromisoformat( | ||
info[f"{type}{dateType}Date"] | ||
).date(), | ||
t=props["alias"], | ||
icon=props["icon"], | ||
) | ||
) | ||
|
||
return entries | ||
|
||
def get_uprn(self, session) -> str: | ||
addresses = session.get( | ||
f"https://www.bathnes.gov.uk/webapi/api/AddressesAPI/v2/search/{self._postcode}/150/true" | ||
).json() | ||
address = next(filter(self.filter_addresses, addresses), None) | ||
if address is None: | ||
raise Exception( | ||
f"House {self._housenameornumber} not found for postcode {self._postcode}" | ||
) | ||
return address["uprn"] | ||
|
||
def filter_addresses(self, address) -> bool: | ||
return f"|{self._housenameornumber.upper()}|" in address["payment_Address"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Bath & North East Somerset Council | ||
|
||
Support for schedules provided by [Bath & North East Somerset Council](https://www.bathnes.gov.uk/). | ||
|
||
If collection data is available for the address provided, it will return rubbish and recycling waste collection dates. | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: bathnes_gov_uk | ||
args: | ||
uprn: UPRN | ||
``` | ||
### Configuration Variables | ||
**postcode** | ||
_(string) (optional)_ | ||
**housenameornumber** | ||
_(string|int) (optional)_ | ||
**uprn** | ||
_(string) (optional)_ | ||
Either the postcode _and_ housenameornumber or the UPRN should be supplied in the arguments | ||
## Examples | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: bathnes_gov_uk | ||
args: | ||
uprn: "10001138699" | ||
``` | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: bathnes_gov_uk | ||
args: | ||
postcode: "BA1 2LR" | ||
housenameornumber: "1" | ||
``` | ||
## How to find your UPRN | ||
An easy way to discover your Unique Property Reference Number (UPRN) is by going to [Find My Address](https://www.findmyaddress.co.uk/) and providing your address details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters