diff --git a/packages/modules/electricity_tariffs/energycharts/tariff.py b/packages/modules/electricity_tariffs/energycharts/tariff.py index 3104efc25b..f164d3016c 100644 --- a/packages/modules/electricity_tariffs/energycharts/tariff.py +++ b/packages/modules/electricity_tariffs/energycharts/tariff.py @@ -1,9 +1,7 @@ from typing import Dict from datetime import datetime, timedelta -from helpermodules.utils.error_handling import ImportErrorContext -with ImportErrorContext(): - import pytz +from helpermodules import timecheck from modules.common import req from modules.common.abstract_device import DeviceDescriptor @@ -13,17 +11,9 @@ def fetch_prices(config: EnergyChartsTariffConfiguration) -> Dict[int, float]: - current_dateTime = datetime.now() tomorrow = datetime.now() + timedelta(1) - if datetime.today().astimezone(pytz.timezone("Europe/Berlin")).dst().total_seconds()/3600: - # UTC Zeit +02:00 = '%2B02%3A00' - start_time = current_dateTime.strftime("%Y-%m-%d") + 'T' + current_dateTime.strftime("%H") + \ - '%3A00' + '%2B02%3A00' - else: - # UTC Zeit +01:00 = '%2B01%3A00' - start_time = current_dateTime.strftime("%Y-%m-%d") + 'T' + current_dateTime.strftime("%H") + \ - '%3A00' + '%2B01%3A00' - end_time = tomorrow.strftime("%Y-%m-%d") + 'T23%3A59%2B01%3A00' + start_time = timecheck.create_unix_timestamp_current_full_hour() + end_time = int(tomorrow.timestamp()) url = f'https://api.energy-charts.info/price?bzn={config.country}&start={start_time}&end={end_time}' raw_prices = req.get_http_session().get(url).json() price_arr = [] diff --git a/packages/modules/electricity_tariffs/rabot/tariff.py b/packages/modules/electricity_tariffs/rabot/tariff.py index 7af448ffff..f5f816f4bd 100644 --- a/packages/modules/electricity_tariffs/rabot/tariff.py +++ b/packages/modules/electricity_tariffs/rabot/tariff.py @@ -56,6 +56,7 @@ def get_raw_prices(): ).json()["records"] validate_token(config) + # ToDo: get rid of hard coded timezone! # start_date von voller Stunde sonst liefert die API die nächste Stunde start_date = datetime.datetime.fromtimestamp( timecheck.create_unix_timestamp_current_full_hour()).astimezone( diff --git a/packages/modules/electricity_tariffs/tibber/tariff.py b/packages/modules/electricity_tariffs/tibber/tariff.py index 67513e6af7..ff34cecff1 100644 --- a/packages/modules/electricity_tariffs/tibber/tariff.py +++ b/packages/modules/electricity_tariffs/tibber/tariff.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from datetime import datetime, timezone +from datetime import datetime from typing import Dict from helpermodules import timecheck @@ -30,13 +30,11 @@ def fetch_prices(config: TibberTariffConfiguration) -> Dict[int, float]: tomorrow_prices = _get_sorted_price_data(response_json, 'tomorrow') sorted_market_prices = today_prices + tomorrow_prices prices: Dict[int, float] = {} + current_hour = timecheck.create_unix_timestamp_current_full_hour() for price_data in sorted_market_prices: - # konvertiere Time-String (Format 2021-02-06T00:00:00+01:00) ()':' nicht von strptime unterstützt) - time_str = ''.join(price_data['startsAt'].rsplit(':', 1)) - start_time_localized = datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S.%f%z') - start_time_utc = int(start_time_localized.astimezone(timezone.utc).timestamp()) - if timecheck.create_unix_timestamp_current_full_hour() <= start_time_utc: - prices.update({start_time_utc: price_data['total'] / 1000}) + start_time_epoch = datetime.fromisoformat(price_data['startsAt']).timestamp() + if current_hour <= start_time_epoch: + prices.update({start_time_epoch: price_data['total'] / 1000}) else: error = response_json['errors'][0]['message'] raise Exception(error) diff --git a/packages/modules/electricity_tariffs/voltego/tariff.py b/packages/modules/electricity_tariffs/voltego/tariff.py index 2171e90111..20ec05f1b0 100644 --- a/packages/modules/electricity_tariffs/voltego/tariff.py +++ b/packages/modules/electricity_tariffs/voltego/tariff.py @@ -52,6 +52,7 @@ def get_raw_prices(): ).json()["elements"] validate_token(config) + # ToDo: get rid of hard coded timezone! Check supported time formats by Voltego API # start_date von voller Stunde sonst liefert die API die nächste Stunde start_date = datetime.datetime.fromtimestamp( timecheck.create_unix_timestamp_current_full_hour()).astimezone( @@ -73,6 +74,7 @@ def get_raw_prices(): prices: Dict[int, float] = {} for data in raw_prices: formatted_price = data["price"]/1000000 # €/MWh -> €/Wh + # timezone of the result should already be UTC as epoch does not support timezones timestamp = datetime.datetime.fromisoformat(data["begin"]).astimezone( pytz.timezone("Europe/Berlin")).timestamp() prices.update({int(timestamp): formatted_price})