Skip to content

Commit

Permalink
fix hvcgroup_nl test case
Browse files Browse the repository at this point in the history
and add error log output if 1st fetch fails
  • Loading branch information
mampfes committed Jul 4, 2021
1 parent 0c29f90 commit b6bc84a
Showing 1 changed file with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import json
import logging
from datetime import datetime

import requests
import json

from waste_collection_schedule import Collection # type: ignore[attr-defined]

TITLE = "HVCGroep"
DESCRIPTION = "Source for the Dutch HVCGroep waste management."
URL = "https://www.hvcgroep.nl/zelf-regelen/afvalkalender"
TEST_CASES = {
"Obdam": {
"postal_code": "1713VM",
"house_number": "1",
},
"Tollebeek": {
"postal_code": "8309AV",
"house_number": "1",
}
}
TEST_CASES = {"Tollebeek": {"postal_code": "8309AV", "house_number": "1"}}

_LOGGER = logging.getLogger(__name__)


class Source:
def __init__(self, postal_code, house_number):
self.postal_code = postal_code
self.house_number = house_number
self.icons = {"plastic-blik-drinkpak": "mdi:recycle", "gft": "mdi:leaf", "papier-en-karton": "mdi:archive", "restafval": "mdi:trash-can"}
self.icons = {
"plastic-blik-drinkpak": "mdi:recycle",
"gft": "mdi:leaf",
"papier-en-karton": "mdi:archive",
"restafval": "mdi:trash-can",
}

def fetch(self):
bag_id = 0
Expand All @@ -35,12 +33,13 @@ def fetch(self):
)
data = json.loads(r.text)

bag_id = data[0]["bagid"]

# Something must be wrong, maybe the address isn't valid? No need to do the extra requests so just return here.
if bag_id == 0:
if len(data) == 0:
_LOGGER.error("no data found for this address")
return []

bag_id = data[0]["bagid"]

# Retrieve the details about different waste management flows (for example, paper, plastic etc.)
r = requests.get(
f"https://inzamelkalender.hvcgroep.nl/rest/adressen/{bag_id}/afvalstromen"
Expand All @@ -56,12 +55,14 @@ def fetch(self):
entries = []

for item in data:
waste_details = [x for x in waste_flows if x["id"] == item["afvalstroom_id"]]
waste_details = [
x for x in waste_flows if x["id"] == item["afvalstroom_id"]
]
entries.append(
Collection(
date=datetime.strptime(item["ophaaldatum"], "%Y-%m-%d").date(),
t=waste_details[0]["title"],
icon=self.icons.get(waste_details[0]["icon"], 'mdi:trash-can')
icon=self.icons.get(waste_details[0]["icon"], "mdi:trash-can"),
)
)

Expand Down

0 comments on commit b6bc84a

Please sign in to comment.