From 9430b7c85767752cb1670bd75981af910e46678b Mon Sep 17 00:00:00 2001 From: Peter Oettig Date: Sat, 20 Sep 2025 13:14:15 +0200 Subject: [PATCH] fix: api.co2signal.com went offline, switch to new API version --- psa_car_controller/psacc/application/ecomix.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/psa_car_controller/psacc/application/ecomix.py b/psa_car_controller/psacc/application/ecomix.py index 8eaf49e..f6f63d5 100644 --- a/psa_car_controller/psacc/application/ecomix.py +++ b/psa_car_controller/psacc/application/ecomix.py @@ -11,7 +11,7 @@ from pytz import UTC from requests import RequestException CO2_SIGNAL_REQ_INTERVAL = 600 -CO2_SIGNAL_URL = "https://api.co2signal.com" +CO2_SIGNAL_URL = "https://api.electricitymaps.com" TIMEOUT_IN_S = 10 logger = logging.getLogger(__name__) @@ -70,15 +70,18 @@ class Ecomix: elif len(Ecomix._cache[country_code]) > 0 and \ (now - Ecomix._cache[country_code][-1][0]).total_seconds() < CO2_SIGNAL_REQ_INTERVAL: return False - res = requests.get(CO2_SIGNAL_URL + "/v1/latest", + res = requests.get(CO2_SIGNAL_URL + "/v3/carbon-intensity/latest", headers={"auth-token": Ecomix.co2_signal_key}, - params={"countryCode": country_code}, + params={"zone": country_code}, timeout=TIMEOUT_IN_S) + if res.status_code != 200: + return False + data = res.json() - value = data["data"]["carbonIntensity"] + value = data["carbonIntensity"] assert isinstance(value, numbers.Number) Ecomix._cache[country_code].append([now, value]) - return data["status"] == "ok" + return True except (AssertionError, NameError, KeyError): logger.debug("ecomix:", exc_info=True) return False