From 1f85a77edb4ea732069748948c0075b40b64103e Mon Sep 17 00:00:00 2001
From: 5ila5 <5ila5@users.noreply.github.com>
Date: Wed, 31 Jul 2024 19:56:42 +0200
Subject: [PATCH] add Fosen Norway
---
README.md | 1 +
.../waste_collection_schedule/sources.json | 5 ++
.../translations/en.json | 6 +-
.../source/fosenrenovasjon_no.py | 69 +++++++++++++++++++
doc/source/fosenrenovasjon_no.md | 34 +++++++++
info.md | 2 +-
6 files changed, 114 insertions(+), 3 deletions(-)
create mode 100644 custom_components/waste_collection_schedule/waste_collection_schedule/source/fosenrenovasjon_no.py
create mode 100644 doc/source/fosenrenovasjon_no.md
diff --git a/README.md b/README.md
index 1b889e0fc..cf6927da3 100644
--- a/README.md
+++ b/README.md
@@ -1176,6 +1176,7 @@ If your service provider is not listed, feel free to open a [source request issu
Norway
- [BIR (Bergensområdets Interkommunale Renovasjonsselskap)](/doc/source/bir_no.md) / bir.no
+- [Fosen Renovasjon](/doc/source/fosenrenovasjon_no.md) / fosenrenovasjon.no
- [IRiS](/doc/source/iris_salten_no.md) / iris-salten.no
- [Min Renovasjon](/doc/source/minrenovasjon_no.md) / norkart.no
- [Movar IKS](/doc/source/movar_no.md) / movar.no
diff --git a/custom_components/waste_collection_schedule/sources.json b/custom_components/waste_collection_schedule/sources.json
index fe8caee7b..031bd947c 100644
--- a/custom_components/waste_collection_schedule/sources.json
+++ b/custom_components/waste_collection_schedule/sources.json
@@ -6302,6 +6302,11 @@
"module": "bir_no",
"default_params": {}
},
+ {
+ "title": "Fosen Renovasjon",
+ "module": "fosenrenovasjon_no",
+ "default_params": {}
+ },
{
"title": "IRiS",
"module": "iris_salten_no",
diff --git a/custom_components/waste_collection_schedule/translations/en.json b/custom_components/waste_collection_schedule/translations/en.json
index 6eb80f06e..c38f16763 100644
--- a/custom_components/waste_collection_schedule/translations/en.json
+++ b/custom_components/waste_collection_schedule/translations/en.json
@@ -276,7 +276,8 @@
"garden_cutomer": "Garden Cutomer",
"app": "App",
"service_provider": "Service Provider",
- "facility_id": "Facility Id"
+ "facility_id": "Facility Id",
+ "abfall": "Abfall"
},
"data_description": {
"calendar_title": "A more readable, or user-friendly, name for the waste calendar. If nothing is provided, the name returned by the source will be used."
@@ -524,7 +525,8 @@
"garden_cutomer": "Garden Cutomer",
"app": "App",
"service_provider": "Service Provider",
- "facility_id": "Facility Id"
+ "facility_id": "Facility Id",
+ "abfall": "Abfall"
}
}
},
diff --git a/custom_components/waste_collection_schedule/waste_collection_schedule/source/fosenrenovasjon_no.py b/custom_components/waste_collection_schedule/waste_collection_schedule/source/fosenrenovasjon_no.py
new file mode 100644
index 000000000..ef434841a
--- /dev/null
+++ b/custom_components/waste_collection_schedule/waste_collection_schedule/source/fosenrenovasjon_no.py
@@ -0,0 +1,69 @@
+from datetime import datetime
+from urllib.parse import quote
+
+import requests
+from waste_collection_schedule import Collection
+
+TITLE = "Fosen Renovasjon"
+DESCRIPTION = "Source for Fosen Renovasjon."
+URL = "https://fosenrenovasjon.no/"
+TEST_CASES = {"Lysøysundveien 117": {"address": "Lysøysundveien 117"}}
+
+
+ICON_MAP = {
+ "Restavfall til forbrenning": "mdi:trash-can",
+ "Matavfall": "mdi:leaf",
+ "Papir og plastemballasje": "mdi:recycle",
+}
+
+
+ADDRESS_URL = "https://fosen.renovasjonsportal.no/api/address/{address}"
+COLLECTIONS_URL = ADDRESS_URL + "/details"
+
+
+class Source:
+ def __init__(self, address: str):
+ self._address = address.lower().strip()
+ self._address_id: str | None = None
+
+ def _fetch_address_id(self):
+ url = ADDRESS_URL.format(address=quote(self._address))
+ r = requests.get(url)
+ r.raise_for_status()
+ data = r.json()
+ for address in data["searchResults"]:
+ if address["title"].lower().strip() == self._address:
+ self._address_id = address["id"]
+ return
+
+ raise ValueError("Address not found", self._address)
+
+ def fetch(self) -> list[Collection]:
+ new_id = False
+ if self._address_id is None:
+ new_id = False
+ self._fetch_address_id()
+ try:
+ return self._get_collections()
+ except Exception:
+ if new_id:
+ raise
+ self._fetch_address_id()
+ return self._get_collections()
+
+ def _get_collections(self) -> list[Collection]:
+ if self._address_id is None:
+ raise ValueError("Address not found", self._address)
+ url = COLLECTIONS_URL.format(address=self._address_id)
+ r = requests.get(url)
+ r.raise_for_status()
+ data = r.json()
+
+ entries: list[Collection] = []
+ for d in data["disposals"]:
+ date = datetime.strptime(d["date"], "%Y-%m-%dT%H:%M:%S").date()
+ entries.append(
+ Collection(date=date, t=d["fraction"], icon=ICON_MAP.get(d["fraction"]))
+ )
+
+ return entries
diff --git a/doc/source/fosenrenovasjon_no.md b/doc/source/fosenrenovasjon_no.md
new file mode 100644
index 000000000..116079947
--- /dev/null
+++ b/doc/source/fosenrenovasjon_no.md
@@ -0,0 +1,34 @@
+# Fosen Renovasjon
+
+Support for schedules provided by [Fosen Renovasjon](https://fosenrenovasjon.no/), serving Fosen, Norway.
+
+## Configuration via configuration.yaml
+
+```yaml
+waste_collection_schedule:
+ sources:
+ - name: fosenrenovasjon_no
+ args:
+ address: ADDRESS
+
+```
+
+### Configuration Variables
+
+**address**
+*(String) (required)*
+
+## Example
+
+```yaml
+waste_collection_schedule:
+ sources:
+ - name: fosenrenovasjon_no
+ args:
+ address: Lysøysundveien 117
+
+```
+
+## How to get the source argument
+
+Visit and search for your address, then use the address spelled exactly as the autocomplete suggests.
diff --git a/info.md b/info.md
index 8ee708559..de3a67dc9 100644
--- a/info.md
+++ b/info.md
@@ -31,7 +31,7 @@ Waste collection schedules from service provider web sites are updated daily, de
| Luxembourg | Esch-sur-Alzette |
| Netherlands | ACV Group, Alpen an den Rijn, Area Afval, Avalex, Avri, Bar Afvalbeheer, Circulus, Cyclus NV, Dar, Den Haag, GAD, Gemeente Almere, Gemeente Berkelland, Gemeente Cranendonck, Gemeente Hellendoorn, Gemeente Lingewaard, Gemeente Meppel, Gemeente Middelburg + Vlissingen, Gemeente Peel en Maas, Gemeente Schouwen-Duiveland, Gemeente Sudwest-Fryslan, Gemeente Venray, Gemeente Voorschoten, Gemeente Waalre, Gemeente Westland, HVC Groep, Meerlanden, Mijn Blink, PreZero, Purmerend, RAD BV, Reinis, Spaarnelanden, Twente Milieu, Waardlanden, Ximmio, ZRD, Ôffalkalinder van Noardeast-Fryslân & Dantumadiel |
| New Zealand | Auckland Council, Christchurch City Council, Dunedin District Council, Gore, Invercargill & Southland, Hamilton City Council, Horowhenua District Council, Hutt City Council, Porirua City, Tauranga City Council, Waipa District Council, Wellington City Council |
-| Norway | BIR (Bergensområdets Interkommunale Renovasjonsselskap), IRiS, Min Renovasjon, Movar IKS, Oslo Kommune, ReMidt Orkland muni, Sandnes Kommune, Stavanger Kommune, Trondheim |
+| Norway | BIR (Bergensområdets Interkommunale Renovasjonsselskap), Fosen Renovasjon, IRiS, Min Renovasjon, Movar IKS, Oslo Kommune, ReMidt Orkland muni, Sandnes Kommune, Stavanger Kommune, Trondheim |
| Poland | Bydgoszcz Pronatura, Ecoharmonogram, Gmina Miękinia, Koziegłowy/Objezierze/Oborniki, Poznań, Warsaw, Wrocław |
| Slovenia | Moji odpadki, Ljubljana |
| Sweden | Affärsverken, Boden, Borås Energi och Miljö, EDPEvent - Multi Source, Gästrike Återvinnare, Jönköping - June Avfall & Miljö, Landskrona - Svalövs Renhållning, Lerum Vatten och Avlopp, Linköping - Tekniska Verken, Lund Waste Collection, Mölndal, Norrtalje Vatten & Avfall, North / Middle Bohuslän - Rambo AB, Region Gotland, Ronneby Miljöteknik, Samverkan Återvinning Miljö (SÅM), Skellefteå, SRV Återvinning, SSAM (Deprecated), SSAM Södra Smalånds Avfall & Miljö, Sysav Sophämntning, Uppsala Vatten, Uppsala Vatten och Avfall AB (Deprecated), VA Syd Sophämntning, VIVAB Sophämtning |