mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-10-08 23:46:38 +00:00
* feat: adapt configuration for multi optimization algorithms Decouple configuration from optimization algorithm parameters. Add to_[algorithm]_param() methods to the configuration that derive optimization algorithm specific parameters from the configuration. Add x-scope tags to the configuration options that describe for which specific algorithms the configuration option is for. The whole device settings are restructured. There are now general settings for the device classes with the afore mentioned to_[algorithm]_param() methods. The general device settings got their own directory `devices/settings`. By this the parameter class also does not have to be a pydantic model which can be used for future optimization/ simulations speed up. Also the parameter class for a device is now part of the device module. This better decouples and also is the natural place for parameters of a device. Besides this feature there are also fixes and improvements: * feat: extend home appliance time window settings and simulation Home appliance can now be configured for multiple runs with per-cycle allowed time windows. The number of remaining cycles to plan is determined at runtime by reading the ``cycles_completed_measurement_key`` from the measurement store. * feat: specialiced CycleTimeWindowSequence for time window sequences Sequence of time windows associated to cycles. This model specializes ``ValueTimeWindowSequence`` so that the ``value`` field of each ``ValueTimeWindow`` encodes the **cycle index** (0-based integer) the window belongs to. Typical use: an appliance that must run ``n`` times per day, each run constrained to a distinct time window. Assign ``value=0`` to windows for the first cycle, ``value=1`` for the second, and so on. Multiple windows may share the same cycle index (their allowed regions are unioned). Windows with ``value=None`` are silently ignored by all cycle-aware methods. * fix: Make test_configmigrate also regard the _ANY_SENTENIEL in key values * chore: Make devices configurations a map instead of a list This makes config paths stable regardless of declaration order and lets each device settings class build its own config path from ``self.device_id`` without needing an external index. Tests are adapted likewise. Devices configurations are automatically migrated from lists to maps. * chore: rename levelized_cost_of_storage_kwh to levelized_cost_of_storage_amt kwh This better fits in the naming scheme and also makes clear the costs are money. Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com> * fix: runtime config update ignored by config file Runtime settings were handed back to pydantic-settings as init settings, which rank below the config file and the environment. Any key already present in EOS.config.json or in the environment silently discarded the update, so a bulk PUT /v1/config returned 200 without applying anything, while the granular PUT /v1/config/{path} endpoint kept working. Add a dedicated runtime settings source ranked directly below the command line arguments and record granular updates there as well, so both endpoints share one store that survives re-evaluation of the settings sources. Environment variables keep precedence over the config file for all keys that were not set at runtime. Also repairs revert_settings() and update(), which passed their data through the same init settings. Closes #1303 * fix: env vars ignored on first config build ConfigEOS.__init__ passed self as first positional argument to _setup, which forwards it to pydantic_settings.BaseSettings.__init__. Its first positional parameter is _case_sensitive, so the environment source matched the upper case variable names against the lower case field names and returned nothing. Environment settings only took effect after the next configuration setup. * docs: changelog for config priority fixes * fix(config): preserve device identities and storage costs during migration * fix(devices): preserve charge-rate typing and public import compatibility * ruff format fix * fix(config): satisfy typed device conversion and migration contracts * docs(config): refresh validated configuration prerequisite schemas --------- Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com> Co-authored-by: Bobby Noelte <b0661n0e17e@gmail.com> Co-authored-by: r0b2g1t <r0b2g1t@users.noreply.github.com>
171 lines
6.6 KiB
Python
171 lines
6.6 KiB
Python
"""Keep imported sale revenues separate from purchase prices in main's async GENETIC path.
|
|
|
|
Adapted from PRs #1224 (Christin) and #1304 (Normann). Main has no direct-marketing
|
|
parameter override yet: these regressions cover its existing preparation/simulation
|
|
contract and refuse unavailable imported revenue instead of creating a demo tariff.
|
|
"""
|
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
|
|
|
import numpy as np
|
|
import pandas as pd
|
|
import pytest
|
|
|
|
from akkudoktoreos.devices.genetic.inverter import Inverter, InverterParameters
|
|
from akkudoktoreos.optimization.genetic.genetic import GeneticSimulation
|
|
from akkudoktoreos.optimization.genetic.geneticparams import (
|
|
GeneticOptimizationParameters,
|
|
)
|
|
from akkudoktoreos.prediction.feedintariffabc import FeedInTariffDataRecord
|
|
from akkudoktoreos.prediction.feedintariffimport import FeedInTariffImport
|
|
from akkudoktoreos.utils.datetimeutil import to_datetime
|
|
|
|
|
|
@pytest.fixture
|
|
def prepare_tariffs(config_eos):
|
|
"""Run real async preparation with deterministic forecasts and no device fallback."""
|
|
|
|
async def prepare(provider, revenues, tariff_reader=None):
|
|
config_eos.merge_settings_from_dict(
|
|
{
|
|
"prediction": {"hours": 24},
|
|
"optimization": {"genetic": {"horizon_hours": 24, "interval_sec": 3600}},
|
|
"feedintariff": {"provider": provider},
|
|
"elecfee": {"provider": None},
|
|
"devices": {
|
|
"max_batteries": 0,
|
|
"max_electric_vehicles": 0,
|
|
"max_inverters": 0,
|
|
"max_home_appliances": 0,
|
|
},
|
|
}
|
|
)
|
|
prices = np.array([0.000269, -0.00002] * 12)
|
|
arrays = {
|
|
"weather_temp_air": np.full(24, 20.0),
|
|
"pvforecast_ac_power": np.full(24, 1000.0),
|
|
"loadforecast_power_w": np.zeros(24),
|
|
"elecprice_marketprice_wh": prices,
|
|
"feed_in_tariff_wh": revenues,
|
|
}
|
|
|
|
async def read_array(key, **kwargs):
|
|
if key == "feed_in_tariff_wh" and tariff_reader is not None:
|
|
return await tariff_reader(key=key, **kwargs)
|
|
value = arrays[key]
|
|
if isinstance(value, Exception):
|
|
raise value
|
|
return np.asarray(value)
|
|
|
|
prediction = Mock(update_data=AsyncMock(), key_to_array=AsyncMock(side_effect=read_array))
|
|
ems = Mock(start_datetime=to_datetime("2026-08-01T00:00:00+00:00"))
|
|
ems.genetic_solution.return_value = None
|
|
with (
|
|
patch("akkudoktoreos.optimization.genetic.geneticparams.get_ems", return_value=ems),
|
|
patch.object(GeneticOptimizationParameters, "prediction", prediction),
|
|
):
|
|
parameters = await GeneticOptimizationParameters.prepare()
|
|
return parameters, prices, prediction
|
|
|
|
return prepare
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"provider",
|
|
[
|
|
"FeedInTariffImport",
|
|
"FeedInTariffAkkudoktor",
|
|
"FeedInTariffEnergyCharts",
|
|
"FeedInTariffTibber",
|
|
"FeedInTariffFixed",
|
|
"FeedInTariffSMARD",
|
|
"FeedInTariffDvhubOnline",
|
|
],
|
|
)
|
|
@pytest.mark.parametrize("revenues", [[0.00007], [0.0], [-0.00005], [0.000184, -0.00005]])
|
|
async def test_provider_revenues_survive_preparation_and_simulation(
|
|
prepare_tariffs, provider, revenues, config_eos
|
|
):
|
|
expected = (revenues * 24)[:24]
|
|
parameters, prices, prediction = await prepare_tariffs(provider, expected)
|
|
assert parameters is not None
|
|
assert parameters.ems.feed_in_tariff_per_wh == expected
|
|
assert parameters.ems.einspeiseverguetung_euro_pro_wh == expected
|
|
assert parameters.ems.electricity_price_per_wh == prices.tolist()
|
|
assert config_eos.feedintariff.provider == provider
|
|
prediction.update_data.assert_awaited_once()
|
|
|
|
# A 1 kWh export at 0.00007 amount/Wh earns 0.07, not 70 or 0.00007.
|
|
# No battery or household load is needed to expose tariff substitution/unit bugs.
|
|
inverter = Inverter(InverterParameters(device_id="inverter1", max_power_wh=10000))
|
|
inverter.self_consumption_predictor = Mock()
|
|
inverter.self_consumption_predictor.calculate_self_consumption.return_value = 1.0
|
|
simulation = GeneticSimulation()
|
|
simulation.prepare(
|
|
parameters.ems, optimization_hours=24, prediction_hours=24, inverter=inverter
|
|
)
|
|
result = simulation.simulate(start_hour=0)
|
|
assert result["Netzeinspeisung_Wh_pro_Stunde"] == pytest.approx([1000.0] * 24)
|
|
assert result["Einnahmen_Euro_pro_Stunde"] == pytest.approx(np.array(expected) * 1000)
|
|
assert result["Gesamteinnahmen_Euro"] == pytest.approx(sum(expected) * 1000)
|
|
assert result["Gesamtbilanz_Euro"] == pytest.approx(-sum(expected) * 1000)
|
|
assert parameters.ems.feed_in_tariff_per_wh == expected
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"revenues",
|
|
[
|
|
KeyError("feed_in_tariff_wh"),
|
|
RuntimeError("import unavailable"),
|
|
[],
|
|
[0.00007] * 23,
|
|
[0.00007] * 25,
|
|
[np.nan] * 24,
|
|
[0.00007] * 23 + [np.nan],
|
|
[np.inf] * 24,
|
|
[-np.inf] * 24,
|
|
[None] * 24,
|
|
[[0.00007]] * 24,
|
|
],
|
|
)
|
|
async def test_invalid_import_cancels_without_replacing_provider(
|
|
prepare_tariffs, revenues, config_eos, caplog
|
|
):
|
|
parameters, _, prediction = await prepare_tariffs("FeedInTariffImport", revenues)
|
|
assert parameters is None
|
|
assert config_eos.feedintariff.provider == "FeedInTariffImport"
|
|
prediction.update_data.assert_awaited_once()
|
|
assert "canceling optimization" in caplog.text
|
|
assert "FeedInTariffImport" in caplog.text
|
|
assert "defaulting to demo" not in caplog.text
|
|
|
|
|
|
def test_prediction_record_prices_are_already_per_wh():
|
|
record = FeedInTariffDataRecord(feed_in_tariff_wh=0.00007)
|
|
assert record.feed_in_tariff_kwh == pytest.approx(0.07)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("values", [[0.000184, -0.00005] * 12, [0.0] * 24, [None] * 24])
|
|
async def test_timestamped_import_records_are_read_in_order(prepare_tariffs, values):
|
|
provider = FeedInTariffImport()
|
|
provider._db_reset_state()
|
|
start = to_datetime("2026-08-01T00:00:00+00:00").set(hour=0)
|
|
try:
|
|
await provider.key_from_series(
|
|
"feed_in_tariff_wh",
|
|
pd.Series(values, index=pd.date_range(start=start, periods=24, freq="h")),
|
|
)
|
|
parameters, _, _ = await prepare_tariffs(
|
|
"FeedInTariffImport", [], tariff_reader=provider.key_to_array
|
|
)
|
|
if values[0] is None:
|
|
assert parameters is None
|
|
else:
|
|
assert parameters is not None
|
|
assert parameters.ems.feed_in_tariff_per_wh == pytest.approx(values)
|
|
finally:
|
|
provider._db_reset_state()
|