mirror of
https://github.com/sascha-hemi/hacs_waste_collection_schedule.git
synced 2026-03-21 04:06:03 +01:00
Add cambridge_gov_uk as source
This commit is contained in:
@@ -117,6 +117,7 @@ Currently the following service providers are supported:
|
|||||||
### United Kingdom
|
### United Kingdom
|
||||||
|
|
||||||
- [York.gov.uk](./doc/source/york_gov_uk.md)
|
- [York.gov.uk](./doc/source/york_gov_uk.md)
|
||||||
|
- [Cambridge.gov.uk](./doc/source/cambridge_gov_uk.md)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
import logging
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from waste_collection_schedule import Collection
|
||||||
|
|
||||||
|
TITLE = "Cambridge.gov.uk"
|
||||||
|
DESCRIPTION = (
|
||||||
|
"Source for cambridge.gov.uk services for Cambridge and part of Cambridgeshire"
|
||||||
|
)
|
||||||
|
URL = "cambridge.gov.uk"
|
||||||
|
TEST_CASES = {
|
||||||
|
"houseNumber": {"post_code": "CB13JD", "number": 37},
|
||||||
|
"houseName": {"post_code": "cb215hd", "number": "ROSEMARY HOUSE"},
|
||||||
|
}
|
||||||
|
|
||||||
|
API_URLS = {
|
||||||
|
"address_search": "https://servicelayer3c.azure-api.net/wastecalendar/address/search/",
|
||||||
|
"collection": "https://servicelayer3c.azure-api.net/wastecalendar/collection/search/{}/",
|
||||||
|
}
|
||||||
|
|
||||||
|
ICONS = {
|
||||||
|
"DOMESTIC": "mdi:trash-can",
|
||||||
|
"RECYCLE": "mdi:recycle",
|
||||||
|
"ORGANIC": "mdi:leaf",
|
||||||
|
}
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Source:
|
||||||
|
def __init__(self, post_code: str, number: str):
|
||||||
|
self._post_code = post_code
|
||||||
|
self._number = str(number).capitalize()
|
||||||
|
|
||||||
|
def fetch(self):
|
||||||
|
# fetch location id
|
||||||
|
r = requests.get(
|
||||||
|
API_URLS["address_search"], params={"postCode": self._post_code}
|
||||||
|
)
|
||||||
|
r.raise_for_status()
|
||||||
|
addresses = r.json()
|
||||||
|
|
||||||
|
address_ids = [
|
||||||
|
x["id"] for x in addresses if x["houseNumber"].capitalize() == self._number
|
||||||
|
]
|
||||||
|
|
||||||
|
if len(address_ids) == 0:
|
||||||
|
raise Exception(f"Could not find address {self._post_code} {self._number}")
|
||||||
|
|
||||||
|
q = str(API_URLS["collection"]).format(address_ids[0])
|
||||||
|
r = requests.get(q)
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
collections = r.json()["collections"]
|
||||||
|
entries = []
|
||||||
|
|
||||||
|
for collection in collections:
|
||||||
|
for round_type in collection["roundTypes"]:
|
||||||
|
entries.append(
|
||||||
|
Collection(
|
||||||
|
date=datetime.strptime(
|
||||||
|
collection["date"], "%Y-%m-%dT%H:%M:%SZ"
|
||||||
|
).date(),
|
||||||
|
t=round_type.title(),
|
||||||
|
icon=ICONS[round_type],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return entries
|
||||||
35
doc/source/cambridge_gov_uk.md
Normal file
35
doc/source/cambridge_gov_uk.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# Cambridge City Council
|
||||||
|
|
||||||
|
Support for schedules provided by [Cambridge City Council](https://www.cambridge.gov.uk/check-when-your-bin-will-be-emptied), serving Cambridge (UK) and part of Cambridgeshire.
|
||||||
|
|
||||||
|
## Configuration via configuration.yaml
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
waste_collection_schedule:
|
||||||
|
sources:
|
||||||
|
- name: cambridge_gov_uk
|
||||||
|
args:
|
||||||
|
post_code: POST_CODE
|
||||||
|
number: NUMBER
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Configuration Variables
|
||||||
|
|
||||||
|
**POST_CODE**<br>
|
||||||
|
*(string) (required)*
|
||||||
|
|
||||||
|
**NUMBER**<br>
|
||||||
|
*(string) (required)*
|
||||||
|
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
waste_collection_schedule:
|
||||||
|
sources:
|
||||||
|
- name: cambridge_gov_uk
|
||||||
|
args:
|
||||||
|
post_code: "CB13JD"
|
||||||
|
number: "37"
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user