Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boersenmodul #1568

Merged
merged 10 commits into from
Jun 12, 2024
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
Empty file.
15 changes: 15 additions & 0 deletions packages/modules/electricity_tariffs/energycharts/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

class EnergyChartsTariffConfiguration:
def __init__(self, country: str = 'de', surcharge: float = 0):
self.country = country
self.surcharge = surcharge


class EnergyChartsTariff:
def __init__(self,
name: str = "Energy-Charts",
type: str = "energycharts",
configuration: EnergyChartsTariffConfiguration = None) -> None:
self.name = name
self.type = type
self.configuration = configuration or EnergyChartsTariffConfiguration()
33 changes: 33 additions & 0 deletions packages/modules/electricity_tariffs/energycharts/tariff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import Dict
from datetime import datetime, timedelta
from modules.common import req

from modules.common.abstract_device import DeviceDescriptor
from modules.common.component_state import TariffState
from modules.common.configurable_tariff import ConfigurableElectricityTariff
from modules.electricity_tariffs.energycharts.config import EnergyChartsTariffConfiguration
from modules.electricity_tariffs.energycharts.config import EnergyChartsTariff


def fetch_prices(config: EnergyChartsTariffConfiguration) -> Dict[int, float]:
current_dateTime = datetime.now()
tomorrow = datetime.now() + timedelta(1)
start_time = current_dateTime.strftime("%Y-%m-%d") + 'T00%3A00%2B01%3A00'
end_time = tomorrow.strftime("%Y-%m-%d") + 'T23%3A59%2B01%3A00'
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 = []
for price in raw_prices['price']:
price_arr.append((float(price + (config.surcharge*10))/1000000)) # €/MWh -> €/Wh + Aufschlag
prices: Dict[int, float] = {}
prices = dict(zip(raw_prices['unix_seconds'], price_arr))
return prices


def create_electricity_tariff(config: EnergyChartsTariff):
def updater():
return TariffState(prices=fetch_prices(config.configuration))
return ConfigurableElectricityTariff(config=config, component_updater=updater)


device_descriptor = DeviceDescriptor(configuration_factory=EnergyChartsTariff)