Add edpevent and merge SSAM and Uppsalavatten (#2281)

* Add edpevent and merge SSAM and Uppsalavatten

This follows a discussion on pull request #2261

* Revert "Add edpevent and merge SSAM and Uppsalavatten"

This reverts commit 4e3b88e842954856751a04b7fb1b042c2cf24e3f.

* Add support for doc generator for edpevent multiple sources

* Add edpevent source

This is a multi source for all that use edpevent.
As of this commit that includes:
- Boden
- Uppsala Vatten
- SSAM
- Skellefteå

* Deprecate SSAM and Uppsala Vatten

* reformatting

---------

Co-authored-by: 5ila5 <5ila5@users.noreply.github.com>
This commit is contained in:
Gustav Mårdestam
2024-07-17 17:42:13 +02:00
committed by GitHub
parent 4b7ef1e35e
commit 8a5a7cb9bf
11 changed files with 407 additions and 177 deletions

View File

@@ -1203,6 +1203,8 @@ Waste collection schedules in the following formats and countries are supported.
<summary>Sweden</summary>
- [Affärsverken](/doc/source/affarsverken_se.md) / affarsverken.se
- [Boden](/doc/source/edpevent_se.md) / boden.se
- [EDPEvent - Multi Source](/doc/source/edpevent_se.md) / edpevent.se
- [Gästrike Återvinnare](/doc/source/gastrikeatervinnare_se.md) / gastrikeatervinnare.se
- [Jönköping - June Avfall & Miljö](/doc/source/juneavfall_se.md) / juneavfall.se
- [Landskrona - Svalövs Renhållning](/doc/source/lsr_nu.md) / lsr.nu
@@ -1214,9 +1216,12 @@ Waste collection schedules in the following formats and countries are supported.
- [Region Gotland](/doc/source/gotland_se.md) / gotland.se
- [Ronneby Miljöteknik](/doc/source/miljoteknik_se.md) / fyrfackronneby.se
- [Samverkan Återvinning Miljö (SÅM)](/doc/source/samiljo_se.md) / samiljo.se
- [Skellefteå](/doc/source/edpevent_se.md) / skelleftea.se
- [SRV Återvinning](/doc/source/srvatervinning_se.md) / srvatervinning.se
- [SSAM](/doc/source/ssam_se.md) / ssam.se
- [SSAM Södra Smalånds Avfall & Miljö](/doc/source/edpevent_se.md) / ssam.se
- [Sysav Sophämntning](/doc/source/sysav_se.md) / sysav.se
- [Uppsala Vatten](/doc/source/edpevent_se.md) / uppsalavatten.se
- [Uppsala Vatten och Avfall AB](/doc/source/uppsalavatten_se.md) / uppsalavatten.se
- [VA Syd Sophämntning](/doc/source/vasyd_se.md) / vasyd.se
- [VIVAB Sophämtning](/doc/source/vivab_se.md) / vivab.se

View File

@@ -6378,6 +6378,18 @@
"module": "affarsverken_se",
"default_params": {}
},
{
"title": "Boden",
"module": "edpevent_se",
"default_params": {
"service_provider": "boden"
}
},
{
"title": "EDPEvent - Multi Source",
"module": "edpevent_se",
"default_params": {}
},
{
"title": "G\u00e4strike \u00c5tervinnare",
"module": "gastrikeatervinnare_se",
@@ -6433,6 +6445,13 @@
"module": "samiljo_se",
"default_params": {}
},
{
"title": "Skellefte\u00e5",
"module": "edpevent_se",
"default_params": {
"service_provider": "skelleftea"
}
},
{
"title": "SRV \u00c5tervinning",
"module": "srvatervinning_se",
@@ -6443,11 +6462,25 @@
"module": "ssam_se",
"default_params": {}
},
{
"title": "SSAM S\u00f6dra Smal\u00e5nds Avfall & Milj\u00f6",
"module": "edpevent_se",
"default_params": {
"service_provider": "ssam"
}
},
{
"title": "Sysav Soph\u00e4mntning",
"module": "sysav_se",
"default_params": {}
},
{
"title": "Uppsala Vatten",
"module": "edpevent_se",
"default_params": {
"service_provider": "uppsalavatten"
}
},
{
"title": "Uppsala Vatten och Avfall AB",
"module": "uppsalavatten_se",

View File

@@ -262,7 +262,8 @@
"postCode": "Post Code",
"zipCode": "Zip Code",
"garden_cutomer": "Garden Cutomer",
"app": "App"
"app": "App",
"service_provider": "Service Provider"
},
"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."
@@ -508,7 +509,8 @@
"postCode": "Post Code",
"zipCode": "Zip Code",
"garden_cutomer": "Garden Cutomer",
"app": "App"
"app": "App",
"service_provider": "Service Provider"
}
}
},

View File

@@ -0,0 +1,229 @@
import json
import logging
from datetime import datetime
import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
TITLE = "EDPEvent - Multi Source"
DESCRIPTION = "Source for all EDPEvent waste collection sources. This included multiple municipalities in Sweden."
URL = "https://www.edpevent.se"
TEST_CASES = {
"Boden - Bodens Kommun": {
"street_address": "KYRKGATAN 24",
"service_provider": "boden",
},
"Boden - Gymnasiet": {
"street_address": "IDROTTSGATAN 4",
"url": "https://edpmobile.boden.se/FutureWeb/SimpleWastePickup",
},
"Uppsalavatten - Test1": {
"street_address": "SADELVÄGEN 1",
"url": "https://futureweb.uppsalavatten.se/Uppsala/FutureWeb/SimpleWastePickup",
},
"Uppsalavatten - Test2": {
"street_address": "BJÖRKLINGE-GRÄNBY 33",
"service_provider": "uppsalavatten",
},
"Uppsalavatten - Test3": {
"street_address": "BJÖRKLINGE-GRÄNBY 20",
"service_provider": "uppsalavatten",
},
"SSAM - Home": {
"street_address": "Asteroidvägen 1, Växjö",
"service_provider": "ssam",
},
"SSAM - Slambrunn": {
"street_address": "Svanebro Ormesberga, Ör",
"service_provider": "ssam",
},
"Skelleftea - Test1": {
"street_address": "Frögatan 76 -150",
"service_provider": "skelleftea",
},
}
COUNTRY = "se"
_LOGGER = logging.getLogger(__name__)
# This maps the icon based on the waste type
ICON_MAP = {
"Brännbart": "mdi:trash-can",
"Matavfall tätt": "mdi:food",
"Deponi": "mdi:recycle",
"Restavfall": "mdi:trash-can",
"Matavfall": "mdi:food-apple",
"Slam": "",
"Trädgårdsavfall": "mdi:leaf",
}
# This can be used to rename the waste types to something more user friendly
WASTE_TYPE_REPLACEMENTS = {
"FNI1": "Kärl 1",
"FNI2": "Kärl 2",
}
MONTH_MAP = {
"Jan": 1,
"Feb": 2,
"Mar": 3,
"Apr": 4,
"Maj": 5,
"Jun": 6,
"Jul": 7,
"Aug": 8,
"Sep": 9,
"Okt": 10,
"Nov": 11,
"Dec": 12,
}
SERVICE_PROVIDERS = {
"skelleftea": {
"title": "Skellefteå",
"url": "https://skelleftea.se",
"api_url": "https://wwwtk2.skelleftea.se/FutureWeb/SimpleWastePickup",
},
"boden": {
"title": "Boden",
"url": "https://boden.se",
"api_url": "https://edpmobile.boden.se/FutureWeb/SimpleWastePickup",
},
"ssam": {
"title": "SSAM Södra Smalånds Avfall & Miljö",
"url": "https://ssam.se",
"api_url": "https://edpfuture.ssam.se/FutureWeb/SimpleWastePickup",
},
"uppsalavatten": {
"title": "Uppsala Vatten",
"url": "https://uppsalavatten.se",
"api_url": "https://futureweb.uppsalavatten.se/Uppsala/FutureWeb/SimpleWastePickup",
},
}
EXTRA_INFO = [
{
"title": data["title"],
"url": data["url"],
"default_params": {"service_provider": provider},
}
for provider, data in SERVICE_PROVIDERS.items()
]
class Source:
def __init__(
self,
street_address: str,
service_provider: str | None = None,
url: str | None = None,
):
self._street_address = street_address
self._url = url
# Check if the user provided a url
if url is None:
# Raise an exception if the user did not provide a service provider (or url)
if service_provider is None:
raise ValueError("You must provide either a service provider or a url")
# Get the api url using the service provider
self._url = SERVICE_PROVIDERS.get(service_provider.lower(), {}).get(
"api_url"
)
if self._url is None:
raise ValueError(
f"Unknown service provider: {service_provider}, use one of {[x for x in SERVICE_PROVIDERS.keys()]}"
)
# Remove trailing slash from the url if present
if self._url.endswith("/"):
self._url = self._url[:-1]
def fetch(self):
params = {"searchText": self._street_address}
# Use the street address to find the full street address with the building ID
searchUrl = self._url + "/SearchAdress"
# Search for the address
response = requests.post(searchUrl, params=params, timeout=30)
address_data = json.loads(response.text)
address = None
# Make sure the response is valid and contains data
if address_data and len(address_data) > 0:
# Check if the request was successful
if address_data["Succeeded"]:
# The request can be successful but still not return any buildings at the specified address
if len(address_data["Buildings"]) > 0:
address = address_data["Buildings"][0]
else:
raise Exception(
f"No returned building address for: {self._street_address}"
)
else:
raise Exception(
f"The server failed to fetch the building data for: {self._street_address}"
)
# Raise exception if all the above checks failed
if not address:
raise Exception(
f"Failed to find building address for: {self._street_address}"
)
# Use the address we got to get the waste collection schedule
params = {"address": address}
getUrl = self._url + "/GetWastePickupSchedule"
# Get the waste collection schedule
response = requests.get(getUrl, params=params, timeout=30)
data = json.loads(response.text)
entries = []
for item in data["RhServices"]:
waste_type = ""
next_pickup = item["NextWastePickup"]
try:
if "v" in next_pickup:
date_parts = next_pickup.split()
month = MONTH_MAP[date_parts[1]]
date_joined = "-".join([date_parts[0], str(month), date_parts[2]])
next_pickup_date = datetime.strptime(
date_joined, "v%W-%m-%Y"
).date()
elif not next_pickup:
continue
else:
next_pickup_date = datetime.strptime(next_pickup, "%Y-%m-%d").date()
except ValueError:
# In some cases the date is just a month, so parse this as the
# first of the month to at least get something close
try:
next_pickup_date = datetime.strptime(next_pickup, "%b %Y").date()
except ValueError as month_parse_error:
_LOGGER.warning(
"Failed to parse date %s, %s,",
next_pickup,
str(month_parse_error),
)
continue
waste_type_prefix = item["WasteType"]
if item["WasteType"] in WASTE_TYPE_REPLACEMENTS:
waste_type_prefix = WASTE_TYPE_REPLACEMENTS[item["WasteType"]]
waste_type = (
waste_type_prefix
+ ", "
+ item["BinType"]["ContainerType"]
+ " "
+ str(item["BinType"]["Size"])
+ item["BinType"]["Unit"]
)
# Get the icon for the waste type, default to help icon if not found
icon = ICON_MAP.get(item["WasteType"], "mdi:help")
found = found = any(
x.date == next_pickup_date and x.type == waste_type for x in entries
)
if not found:
entries.append(
Collection(date=next_pickup_date, t=waste_type, icon=icon)
)
return entries

View File

@@ -1,12 +1,9 @@
from datetime import datetime
import json
import logging
import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
from .edpevent_se import Source as EdpEventSource # type: ignore[attr-defined]
TITLE = "SSAM"
DESCRIPTION = "Source for SSAM waste collection."
TITLE = "SSAM (Deprecated)"
DESCRIPTION = "Deprecated, please use edpevent_se instead."
URL = "https://ssam.se"
TEST_CASES = {
"Home": {"street_address": "Asteroidvägen 1, Växjö"},
@@ -16,94 +13,10 @@ TEST_CASES = {
_LOGGER = logging.getLogger(__name__)
class Source:
class Source(EdpEventSource):
def __init__(self, street_address):
self._street_address = street_address
def fetch(self):
params = {"searchText": self._street_address}
response = requests.post(
"https://edpfuture.ssam.se/FutureWeb/SimpleWastePickup/SearchAdress",
params=params,
timeout=30,
super().__init__(street_address, service_provider="ssam")
# Log a warning message indicating that this source is deprecated
_LOGGER.warning(
"The SSAM source is deprecated, please use edpevent_se instead."
)
address_data = json.loads(response.text)
address = None
if address_data and len(address_data) > 0:
address = address_data["Buildings"][0]
if not address:
return []
params = {"address": address}
response = requests.get(
"https://edpfuture.ssam.se/FutureWeb/SimpleWastePickup/GetWastePickupSchedule",
params=params,
timeout=30,
)
data = json.loads(response.text)
entries = []
for item in data["RhServices"]:
waste_type = ""
next_pickup = item["NextWastePickup"]
try:
next_pickup_date = datetime.fromisoformat(next_pickup).date()
except ValueError as _:
# In some cases the date is just a month, so parse this as the
# first of the month to atleast get something close
try:
next_pickup_date = datetime.strptime(next_pickup, "%b %Y").date()
except ValueError as month_parse_error:
_LOGGER.warning(
"Failed to parse date %s, %s,",
next_pickup,
str(month_parse_error),
)
continue
if item["WasteType"] == "FNI1":
waste_type = (
"Kärl 1, "
+ item["BinType"]["ContainerType"]
+ " "
+ str(item["BinType"]["Size"])
+ item["BinType"]["Unit"]
)
icon = "mdi:trash-can"
elif item["WasteType"] == "FNI2":
waste_type = (
"Kärl 2, "
+ item["BinType"]["ContainerType"]
+ " "
+ str(item["BinType"]["Size"])
+ item["BinType"]["Unit"]
)
icon = "mdi:trash-can"
elif item["BinType"]["Code"] == "KM140":
waste_type = "Matpåsar"
icon = "mdi:recycle"
else:
waste_type = (
item["WasteType"]
+ " "
+ item["BinType"]["ContainerType"]
+ " "
+ str(item["BinType"]["Size"])
+ item["BinType"]["Unit"]
)
icon = "mdi:trash-can"
if item["WasteType"] == "Trädgårdsavfall":
icon = "mdi:leaf"
found = 0
for x in entries:
if x.date == next_pickup_date and x.type == waste_type:
found = 1
if found == 0:
entries.append(
Collection(date=next_pickup_date, t=waste_type, icon=icon)
)
return entries

View File

@@ -1,14 +1,15 @@
import json
import urllib.parse
from datetime import datetime
import logging
import requests
from waste_collection_schedule import Collection
# from waste_collection_schedule import Collection
from .edpevent_se import Source as EdpEventSource
TITLE = "Uppsala Vatten och Avfall AB"
DESCRIPTION = "Source script for uppsalavatten.se"
TITLE = "Uppsala Vatten och Avfall AB (Deprecated)"
DESCRIPTION = "Deprecated, please use edpevent_se instead."
URL = "https://www.uppsalavatten.se"
# Init logger
_LOGGER = logging.getLogger(__name__)
TEST_CASES = {
"Test1": {
"city": "BJÖRKLINGE",
@@ -24,75 +25,11 @@ TEST_CASES = {
},
}
API_URLS = {
"address_search": "https://futureweb.uppsalavatten.se/Uppsala/FutureWebBasic/SimpleWastePickup/SearchAdress",
"collection": "https://futureweb.uppsalavatten.se/Uppsala/FutureWebBasic/SimpleWastePickup/GetWastePickupSchedule",
}
ICON_MAP = {
"Restavfall": "mdi:trash-can",
"Matavfall": "mdi:food-apple",
"Slam": "",
}
MONTH_MAP = {
"Jan": 1,
"Feb": 2,
"Mar": 3,
"Apr": 4,
"Maj": 5,
"Jun": 6,
"Jul": 7,
"Aug": 8,
"Sep": 9,
"Okt": 10,
"Nov": 11,
"Dec": 12,
}
class Source:
class Source(EdpEventSource):
def __init__(self, street, city):
self.street = street
self.city = city
# self.facid = facid
def fetch(self):
# request to get facility id
addresslist = requests.post(
API_URLS["address_search"],
{"searchText": f"{self.street.upper()}, {self.city.upper()}"},
super().__init__(street, service_provider="uppsalavatten")
# Log a warning message indicating that this source is deprecated
_LOGGER.warning(
"The Uppsala Vatten och Avfall AB source is deprecated, please use edpevent_se instead"
)
addresslist.raise_for_status()
adresslist_json = json.loads(addresslist.text)
payload = {"address": adresslist_json["Buildings"][0]}
payload_str = urllib.parse.urlencode(payload, encoding="utf8")
# request for the wasteschedule
wasteschedule = requests.get(API_URLS["collection"], params=payload_str)
wasteschedule.raise_for_status()
data = json.loads(wasteschedule.text)
entries = []
for i in data["RhServices"]:
icon = ICON_MAP.get(i["WasteType"])
if "v" in i["NextWastePickup"]:
date_parts = i["NextWastePickup"].split()
month = MONTH_MAP[date_parts[1]]
date_joined = "-".join([date_parts[0], str(month), date_parts[2]])
date = datetime.strptime(date_joined, "v%W-%m-%Y").date()
elif not i["NextWastePickup"]:
continue
else:
date = datetime.strptime(i["NextWastePickup"], "%Y-%m-%d").date()
entries.append(
Collection(
t=i["WasteType"],
icon=icon,
date=date,
)
)
return entries

94
doc/source/edpevent_se.md Normal file
View File

@@ -0,0 +1,94 @@
# EDPEvent
This is a waste collection schedule integration for the EDPEvent API. EDPEent is used in multiple municipalities in Sweden. The previous SSAM and Uppsala Vatten integrations have been merged into this integration.
This integration now also supports the municipalities of Boden and Skellefteå.
## Current supported service providers
<!--Begin of service section-->
- `boden`: Boden
- `skelleftea`: Skellefteå
- `ssam`: SSAM Södra Smalånds Avfall & Miljö
- `uppsalavatten`: Uppsala Vatten
<!--End of service section-->
## Configuration via configuration.yaml
```yaml
waste_collection_schedule:
sources:
- name: edpevent_se
args:
street_address: STREET_ADDRESS
service_provider: SERVICE_PROVIDER
url: URL
```
### Configuration Variables
**street_address**
*(string) (required)*
**service_provider**
*(string) (optional)*
***url***
*(string) (optional)*
### Note on optional parameters
While service_provider and url are optional. At least one of them must be provided. The service provider can be used with the preconfigured cities. The url can be used to configure the integration with a custom city.
## Examples
Support for Boden's municipality waste collection schedule.
```yaml
waste_collection_schedule:
sources:
- name: edpevent_se
args:
street_address: KYRKGATAN 24
service_provider: boden
```
Support for schedules provided by Uppsala Vatten och Avfall AB, serving the municipality of Uppsala.
```yaml
waste_collection_schedule:
sources:
- name: edpevent_se
args:
street_address: SADELVÄGEN 1
service_provider: uppsalavatten
```
Support for schedules provided by [SSAM](https://ssam.se/mitt-ssam/hamtdagar.html), serving the municipality of Lessebo, Tingsryd, Älmhult, Markaryd and Växjö Sweden.
```yaml
waste_collection_schedule:
sources:
- name: edpevent_se
args:
street_address: Asteroidvägen 1, Växjö
service_provider: ssam
```
Support for schedules provided by Skellefteå municipality.
```yaml
waste_collection_schedule:
sources:
- name: edpevent_se
args:
street_address: Frögatan 76 -150
service_provider: skelleftea
```
## How to get the correct address
To find your correct address, search for it on your service providers website:
- [Boden](https://www.boden.se/boende-trafik/avfall-och-aterbruk/avfall-395A)
- [Skellefteå](https://skelleftea.se/invanare/startsida/bygga-bo-och-miljo/avfall-och-atervinning/sophamtning---nar-toms-soporna)
- [SSAM](https://ssam.se/mitt-ssam/hamtdagar.html)
- [Uppsala Vatten](https://www.uppsalavatten.se/sjalvservice/hamtningar-och-berakningar/dag-for-sophamtning-och-slamtomning)

View File

@@ -1,4 +1,6 @@
# SSAM
# SSAM (Deprecated)
This integration is deprecated and will probably not work forever. Please use the [edpevent_se source](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/edpevent_se.md) instead.
Support for schedules provided by [SSAM](https://ssam.se/mitt-ssam/hamtdagar.html), serving the municipality of Lessebo, Tingsryd, Älmhult, Markaryd and Växjö Sweden.

View File

@@ -1,4 +1,6 @@
# Uppsala Vatten och Avfall AB
# Uppsala Vatten och Avfall AB (Deprecated)
This integration is deprecated and will probably not work forever. Please use the [edpevent_se source](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/edpevent_se.md) instead.
Support for schedules provided by [Uppsala Vatten och Avfall AB](https://www.uppsalavatten.se), serving the municipality of Uppsala.

File diff suppressed because one or more lines are too long

View File

@@ -123,6 +123,18 @@ def split_camel_and_snake_case(s):
s = re.sub("([a-z0-9])([A-Z])", r"\1 \2", s) # Split CamelCase
return s.replace("_", " ").split() # Split snake_case
def update_edpevent_se(modules: dict[str, ModuleType]):
module = modules.get("edpevent_se")
if not module:
print("edpevent_se not found")
return
services = getattr(module, "SERVICE_PROVIDERS", {})
str = ""
for provider, data in sorted(services.items()):
str += f'- `{provider}`: {data["title"]}\n'
_patch_file("doc/source/edpevent_se.md", "service", str)
def main() -> None:
sources: list[SourceInfo] = []
@@ -228,7 +240,8 @@ def browse_sources() -> list[SourceInfo]:
update_citiesapps_com(modules)
update_app_abfallplus_de(modules)
update_abfallnavi_de(modules)
update_edpevent_se(modules)
return sources