diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/republicservices_com.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/republicservices_com.py index f597f22d1..8c3756c82 100644 --- a/custom_components/waste_collection_schedule/waste_collection_schedule/source/republicservices_com.py +++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/republicservices_com.py @@ -1,7 +1,7 @@ -import requests import json - from datetime import datetime, timedelta + +import requests from waste_collection_schedule import Collection # type: ignore[attr-defined] TITLE = "Republic Services" @@ -9,11 +9,25 @@ URL = "https://www.republicservices.com" COUNTRY = "us" TEST_CASES = { - "Scott Country Clerk": {"street_address": "101 E Main St, Georgetown, KY 40324"}, - "Branch County Clerk": {"street_address": "31 Division St. Coldwater, MI 49036"}, - "Contract Collection": {"street_address": "8957 Park Meadows Dr, Elk Grove, CA 95624"}, - "Residential Collection": {"street_address": "117 Roxie Ln, Georgetown, KY 40324"}, - + "Scott Country Clerk": { + "street_address": "101 E Main St, Georgetown, KY 40324", + "method": 1, + }, + "Branch County Clerk": { + "street_address": "31 Division St. Coldwater, MI 49036", + "method": "1", + }, + "Contract Collection": { + "street_address": "8957 Park Meadows Dr, Elk Grove, CA 95624", + "method": 2, + }, + "Residential Collection": { + "street_address": "117 Roxie Ln, Georgetown, KY 40324", + "method": "2", + }, + "No Method arg": { + "street_address": "8957 Park Meadows Dr, Elk Grove, CA 95624", + }, } DELAYS = { " one ": 1, @@ -27,8 +41,9 @@ class Source: - def __init__(self, street_address): + def __init__(self, street_address, method=1): self._street_address = street_address + self._method = int(method) def fetch(self): s = requests.Session() @@ -59,35 +74,42 @@ def fetch(self): dt = datetime.strptime(day, "%Y-%m-%d").date() service = item["containerCategory"] schedule.update( - {i: { - "date": dt, - "waste_type": item["wasteTypeDescription"], - "waste_description": item["productDescription"], - "service": service, + { + i: { + "date": dt, + "waste_type": item["wasteTypeDescription"], + "waste_description": item["productDescription"], + "service": service, } } ) i += 1 # Compile holidays that impact collections - r2 = s.get(f"https://www.republicservices.com/api/v3/holidaySchedules/schedules", params={"latitude": latitude, "longitude": longitude}) + r2 = s.get( + "https://www.republicservices.com/api/v3/holidaySchedules/schedules", + params={"latitude": latitude, "longitude": longitude}, + ) r2_json = json.loads(r2.text)["data"] day_offset = 0 i = 0 holidays = {} for item in r2_json: - if item["serviceImpacted"] == True and item["LOB"] == service: + if item["serviceImpacted"] is True and item["LOB"] == service: for delay in DELAYS: if delay in item["description"]: day_offset = DELAYS[delay] - dt = datetime.strptime(item["date"], "%Y-%m-%dT00:00:00.0000000Z").date() + dt = datetime.strptime( + item["date"], "%Y-%m-%dT00:00:00.0000000Z" + ).date() holidays.update( - {i: { - "date": dt, - "name": item["name"], - "description": item["description"], - "delay": day_offset, - "incorporated": False + { + i: { + "date": dt, + "name": item["name"], + "description": item["description"], + "delay": day_offset, + "incorporated": False, } } ) @@ -104,7 +126,7 @@ def fetch(self): p = schedule[pickup]["date"] date_difference = (p - h).days if date_difference <= 5 and date_difference >= 0: - revised_date = p + timedelta(days = d) + revised_date = p + timedelta(days=d) schedule[pickup]["date"] = revised_date holidays[holiday]["incorporated"] = True # print(p, h, d, date_difference, revised_date) @@ -114,19 +136,40 @@ def fetch(self): # Build final schedule (implements original logic for assigning icon) entries = [] - for item in schedule: - if "RECYCLE" in schedule[item]["waste_description"]: - icon = "mdi:recycle" - elif "YARD" in schedule[item]["waste_description"]: - icon = "mdi:leaf" - else: - icon = "mdi:trash-can" - entries.append( - Collection( - date=schedule[item]["date"], - t=schedule[item]["waste_type"], - icon=icon, - ), - ) - + if self._method == 1: # Original logic + for item in schedule: + if "RECYCLE" in schedule[item]["waste_description"]: + icon = "mdi:recycle" + elif "YARD" in schedule[item]["waste_description"]: + icon = "mdi:leaf" + else: + icon = "mdi:trash-can" + entries.append( + Collection( + date=schedule[item]["date"], + t=schedule[item]["waste_type"], + icon=icon, + ), + ) + elif ( + self._method == 2 + ): # Updated to report yard waste as a separate category to recycling + for item in schedule: + if "YARD" in schedule[item]["waste_description"]: + icon = "mdi:leaf" + schedule[item]["waste_type"] = "Yard Waste" + elif "RECYCLE" in schedule[item]["waste_description"]: + icon = "mdi:recycle" + elif "YARD" in schedule[item]["waste_description"]: + icon = "mdi:leaf" + else: + icon = "mdi:trash-can" + entries.append( + Collection( + date=schedule[item]["date"], + t=schedule[item]["waste_type"], + icon=icon, + ), + ) + return entries diff --git a/doc/source/republicservices_com.md b/doc/source/republicservices_com.md index 7653f06bc..cf4efcb32 100644 --- a/doc/source/republicservices_com.md +++ b/doc/source/republicservices_com.md @@ -10,6 +10,7 @@ waste_collection_schedule: - name: republicservices_com args: street_address: STREET_ADDRESS + method: METHOD ``` ### Configuration Variables @@ -17,16 +18,55 @@ waste_collection_schedule: **street_address** *(string) (required)* -## Example +**method** +*(int) (optional)* + +_method: 1_ + +Waste type categories are returned as per the Republic Services entries. This is the default behaviour if no arg is provided. + +method: 2 + +Recycling waste type categories described as Yard Waste are returned as a "Yard Waste" waste type rather than source "Recycling" waste type. + +## Example (method: 1) ```yaml waste_collection_schedule: sources: - name: republicservices_com args: - street_address: "101 E Main St, Georgetown, KY 40324" + street_address: "8957 Park Meadows Dr, Elk Grove, CA 95624" + method: 1 +``` +```bash +2023-10-24 : Recycle [mdi:leaf] +2023-10-31 : Recycle [mdi:recycle] +2023-11-14 : Recycle [mdi:recycle] +2023-11-28 : Recycle [mdi:recycle] +2023-12-12 : Recycle [mdi:recycle] +2023-12-26 : Recycle [mdi:recycle] +2023-10-24 : Solid Waste [mdi:trash-can] ``` +## Example (method: 2) +```yaml +waste_collection_schedule: + sources: + - name: republicservices_com + args: + street_address: "8957 Park Meadows Dr, Elk Grove, CA 95624" + method: 2 +``` +```bash +2023-10-24 : Yard Waste [mdi:leaf] +2023-10-31 : Recycle [mdi:recycle] +2023-11-14 : Recycle [mdi:recycle] +2023-11-28 : Recycle [mdi:recycle] +2023-12-12 : Recycle [mdi:recycle] +2023-12-26 : Recycle [mdi:recycle] +2023-10-24 : Solid Waste [mdi:trash-can] +``` ## How to check the street address The street address can be tested [here](https://republicservices.com).