From c53a53f26d8d66c6b681a5f1a05b3e4acca9b2d3 Mon Sep 17 00:00:00 2001 From: Calvin Bui <3604363+calvinbui@users.noreply.github.com> Date: Sun, 27 Nov 2022 15:11:42 +1100 Subject: [PATCH] Add RecycleSmart --- README.md | 3 +- .../source/recyclesmart_com.py | 59 +++++++++++++++++++ doc/source/recyclesmart_com.md | 33 +++++++++++ info.md | 5 +- 4 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/recyclesmart_com.py create mode 100644 doc/source/recyclesmart_com.md diff --git a/README.md b/README.md index beabfc6a..33afd781 100644 --- a/README.md +++ b/README.md @@ -62,10 +62,11 @@ Currently the following service providers are supported: - [City of Canada Bay Council](./doc/source/canadabay_nsw_gov_au.md) - [Inner West Council (NSW)](./doc/source/innerwest_nsw_gov_au.md) - [Ku-ring-gai Council](./doc/source/kuringgai_nsw_gov_au.md) -- [Maroondah City Council](./doc/source/maroondah_vic_gov_au.md) - [Macedon Ranges Shire Council, Melbourne](./doc/source/mrsc_vic_gov_au.md) +- [Maroondah City Council](./doc/source/maroondah_vic_gov_au.md) - [Melton City Council, Melbourne](./doc/source/melton_vic_gov_au.md) - [North Adelaide Waste Management Authority, South Australia](./doc/source/nawma_sa_gov_au.md) +- [RecycleSmart](./doc/source/recyclesmart_com.md) - [Stonnington City Council, Melbourne](./doc/source/stonnington_vic_gov_au.md) - [The Hills Council, Sydney](./doc/source/thehills_nsw_gov_au.md) - [Wyndham City Council, Melbourne](./doc/source/wyndham_vic_gov_au.md) diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/recyclesmart_com.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/recyclesmart_com.py new file mode 100644 index 00000000..e64f53b2 --- /dev/null +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/recyclesmart_com.py @@ -0,0 +1,59 @@ +import json +from datetime import datetime + +import requests +from waste_collection_schedule import Collection # type: ignore[attr-defined] + +TITLE = "RecycleSmart" +DESCRIPTION = "Source for RecycleSmart collection." +URL = "https://www.recyclesmart.com/" +TEST_CASES = { + "pickup": { + "email": "!secret recyclesmart_email", + "password": "!secret recyclesmart_password" + }, +} + + +class Source: + def __init__(self, email, password): + self._email = email + self._password = password + + def fetch(self): + # login is via Google Identity Toolkit, providing username and password. + r = requests.post( + "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword", + json={ + "returnSecureToken": True, + "email": self._email, + "password": self._password, + }, + params={"key": "AIzaSyBnup3QFYYGwnvjZi7r5a39c8b94SNospU"}, + ) + data = json.loads(r.text) + + # take the token returned from Google Identity Toolkit to get the API auth token (JWT) from Firebase + r = requests.post( + "https://www.app.recyclesmart.com/api/sessions/firebase", + json={"id_token": data["idToken"]}, + headers={"content-type": "application/json"}, + ) + data = json.loads(r.text) + + # use the auth token (jwt) from firebase to get pickups + r = requests.get( + "https://www.app.recyclesmart.com/api/pickups", + # retrieves future pickups (if scheduled), then past pickups, until per_page is reached + # the average user is scheduled for monthly pickups + params={"page":"1", "per_page":"5"}, + headers={"Authorization": data["data"]["attributes"]["auth_token"]}, + ) + + data = json.loads(r.text) + + entries = [] + for item in data["data"]: + collection_date = datetime.strptime(item["attributes"]["pickup_on"], "%Y-%m-%d") + entries.append(Collection(collection_date, "Pickup")) + return entries diff --git a/doc/source/recyclesmart_com.md b/doc/source/recyclesmart_com.md new file mode 100644 index 00000000..c80c2e92 --- /dev/null +++ b/doc/source/recyclesmart_com.md @@ -0,0 +1,33 @@ +# RecycleSmart + +Support for schedules povided by [RecycleSmart](https://www.recyclesmart.com/) + +## Configuration via configuration.yaml + +```yaml +waste_collection_schedule: + sources: + - name: recyclesmart_com + args: + email: EMAIL + password: PASSWORD +``` + +### Configuration Variables + +**username**
+*(string) (required)* + +**password**
+*(string) (required)* + +## Example + +```yaml +waste_collection_schedule: + sources: + - name: recyclesmart_com + args: + email: abc@example.com + password: 8WdpR%TZPPdM$n5*$FiRz +``` diff --git a/info.md b/info.md index 47a6127f..a7c2f497 100644 --- a/info.md +++ b/info.md @@ -48,13 +48,14 @@ Currently the following service providers are supported: - [City of Canada Bay Council](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/canadabay_nsw_gov_au.md) - [Inner West Council (NSW)](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/innerwest_nsw_gov_au.md) - [Ku-ring-gai Council](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/kuringgai_nsw_gov_au.md) -- [Maroondah City Council](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/maroondah_vic_gov_au.md) - [Macedon Ranges Shire Council, Melbourne](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/mrsc_vic_gov_au.md) +- [Maroondah City Council](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/maroondah_vic_gov_au.md) - [Melton City Council, Melbourne](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/melton_vic_gov_au.md) - [North Adelaide Waste Management Authority, South Australia](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/nawma_sa_gov_au.md) +- [RecycleSmart](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/recyclesmart.md) - [Stonnington City Council, Melbourne](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/stonnington_vic_gov_au.md) - [The Hills Council, Sydney](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/thehills_nsw_gov_au.md) -- [Wyndham City Council, Melbourne](./doc/source/wyndham_vic_gov_au.md) +- [Wyndham City Council, Melbourne](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/sourcewyndham_vic_gov_au.md) ### Austria