Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions packages/modules/electricity_tariffs/energycharts/tariff.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = []
Expand Down
1 change: 1 addition & 0 deletions packages/modules/electricity_tariffs/rabot/tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 5 additions & 7 deletions packages/modules/electricity_tariffs/tibber/tariff.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions packages/modules/electricity_tariffs/voltego/tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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})
Expand Down