diff --git a/packages/modules/electricity_tariffs/energycharts/__init__.py b/packages/modules/electricity_tariffs/energycharts/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/modules/electricity_tariffs/energycharts/config.py b/packages/modules/electricity_tariffs/energycharts/config.py new file mode 100644 index 0000000000..d92a8527e6 --- /dev/null +++ b/packages/modules/electricity_tariffs/energycharts/config.py @@ -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() diff --git a/packages/modules/electricity_tariffs/energycharts/tariff.py b/packages/modules/electricity_tariffs/energycharts/tariff.py new file mode 100644 index 0000000000..2139779821 --- /dev/null +++ b/packages/modules/electricity_tariffs/energycharts/tariff.py @@ -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)