mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-08-30 04:06:37 +00:00
Add new provider class for electricity fees providers. Add the generic providers: - ElecFeeFixed - ElecFeeImport The providers provide predictions for: - elecfee_consumption_amt_wh: Total fixed fee for consumed energy per Wh [amount/Wh]. This is the accumulation of all fixed per-Wh fees payable on "consumed energy - such as network charge, concession fee, and electricity charge - into a single amount. - elecfee_consumption_percent_amt: Total fixed surcharge on consumed energy, given as a percentage of the monetary amount already charged for that energy [%]. This is the accumulation of all percentage-based surcharges payable on top of the consumed-energy fee - such as VAT - into a single percentage. This is a percentage of the fee amount, not a per-Wh rate. - elecfee_feedin_amt_wh: Total fixed deduction from feed-in energy per Wh [amount/Wh]. This is the accumulation of all fixed per-Wh charges deducted from feed-in energy - such as metering fees or grid-operator handling "charges - into a single amount. Applied after the percentage-based deduction, i.e. it reduces the price by a flat amount per Wh rather than by a share of the raw price. - elecfee_feedin_percent_amt: Total percentage deducted from the raw feed-in price (spot price) [%]. This is the accumulation of all percentage-based deductions payable on the feed-in tariff - such as a marketing or balancing fee retained by the aggregator - into a single percentage. It is applied as `raw_price * (100 - percent) / 100`, i.e. it scales down the raw price rather than adding a surcharge to it. A new _apply_fee() method is added to the base class for ElecPrice and FeedInTariff to be used to add the fees in a consistent way. Fees are taken from the active ElecFee provider and applied to the raw prices given to the _apply_fee() method. The optional application of fees is added to: - ElecPriceAkkudoktor - ElecPriceFixed - ElecPriceEnergyCharts - ElecPriceSMARD - FeedInTariffEnergyCharts - FeedInTariffFixed - FeedInTariffSMARD The import providers ElecPriceImport and FeedInTariffImport do not apply fees by intentention. The following providers currently do not handle fees defined by ElecFee: - ElecPriceTibber - FeedInTariffAkkudoktor - FeedInTariffDvhubOnline - FeedInTariffTibber The tests for this feature are either added or existing tests are extended. The documentation was extended for the electricity fee provider settings. Besides this feature further improvements are added: * feat: add SMARD quarter-hour electricty price and feed-in tariff provider * feat: to_series method for TimeWindows and ValueTimeWindows Additional to to_array the time window sequence can now also produce a pandas series. Test have been extended to cover the series generation. * feat: use time windows in fixed feedin tariff provider Feedin tariff can now be configured by time windows - not a single value. * feat: EOSdash select for PVLib inverters and modules Provide PVLib inverter and module names in config selection. * feat: EOSdash lazy select for big option sets Add a new form for lazy selection of big option sets. Filtering and generation of the option set is done server-side. * fix: use raw data for ETS/ median prediction Use to raw time series data for ETS/ median prediction to avoid interference by e.g. dynamic grid charges. * fix: EOSdash config drops by type only on details resolve Drop configuration by type and path. Prevents dropping of configuration items with same type and level but different path. * fix: EOSdash configuration section closes on update Open section if searching or if last update touched this category — including updates on deeply nested sub-fields. * chore: make elecfeefixed, elecpricefixed and feedintarifffixed warn about no windows and default to 0 Missining configuration creates default 0 value and a warning instead of an exception. * fix: test setup for providers Reset db state on each test run. * chore: improve config option naming for elecpricefixed. * chore: adapt elecpricefixed test to changed time_windows naming * chore: factorized common price provider helpers to priceabc.py Factorized common price provider helpers to priceabc.py. Add tests for these helpers. Reduce/ change testing of elecpriceabc.py and feedintariffabc.py to cover only specifics. Rest of testing is already covered by test_priceabc.py. * chore: update version Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
192 lines
7.8 KiB
Python
192 lines
7.8 KiB
Python
"""Tests for feed in tariff prediction abstract/base classes.
|
|
|
|
Shared `_apply_fees`/`_store_gross_series` plumbing (empty/short-series
|
|
validation, non-uniform-spacing fallback, missing-fee-row zero-fill, key
|
|
wiring, singleton mechanics) lives in `PricePredictionProviderBase` and is
|
|
exercised generically in `test_priceabc.py` - it is not re-tested here. This
|
|
module covers what's genuinely specific to `FeedInTariffProvider`: the data
|
|
record model, config-driven provider identity, and the feed-in-fee formula in
|
|
`_compute_gross`.
|
|
"""
|
|
|
|
from typing import Optional
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pandas as pd
|
|
import pytest
|
|
|
|
from akkudoktoreos.prediction.feedintariffabc import (
|
|
FeedInTariffDataRecord,
|
|
FeedInTariffProvider,
|
|
)
|
|
from akkudoktoreos.utils.datetimeutil import to_datetime
|
|
|
|
|
|
class _FeedInTariffProviderForTest(FeedInTariffProvider):
|
|
"""Minimal concrete subclass to exercise the abstract FeedInTariffProvider base class."""
|
|
|
|
@classmethod
|
|
def provider_id(cls) -> str:
|
|
return "FeedInTariffProviderForTest"
|
|
|
|
async def _update_data(self, force_update: Optional[bool] = False) -> None:
|
|
"""No-op update.
|
|
|
|
Not exercised by the apply_fees() tests below - they build
|
|
raw_price_amt_wh directly and never call update_data() on this
|
|
provider - but FeedInTariffProvider declares _update_data as abstract,
|
|
so a concrete subclass must implement it to be instantiable at all.
|
|
"""
|
|
return None
|
|
|
|
|
|
class TestFeedInTariffDataRecord:
|
|
"""Tests for FeedInTariffDataRecord model."""
|
|
|
|
def test_tariff_kwh_computed_from_wh(self):
|
|
"""Test that the kWh tariff is the Wh tariff scaled by 1000."""
|
|
record = FeedInTariffDataRecord(feed_in_tariff_wh=0.0003)
|
|
assert record.feed_in_tariff_wh == 0.0003
|
|
assert record.feed_in_tariff_kwh is not None
|
|
assert abs(record.feed_in_tariff_kwh - 0.3) < 1e-9
|
|
|
|
def test_tariff_kwh_none_when_wh_none(self):
|
|
"""Test that the kWh tariff is None when the underlying Wh tariff is unset."""
|
|
record = FeedInTariffDataRecord()
|
|
assert record.feed_in_tariff_wh is None
|
|
assert record.feed_in_tariff_kwh is None
|
|
|
|
def test_tariff_kwh_zero_when_wh_zero(self):
|
|
"""Test that a genuine zero Wh tariff computes to a zero kWh tariff, not None."""
|
|
record = FeedInTariffDataRecord(feed_in_tariff_wh=0.0)
|
|
assert record.feed_in_tariff_kwh == 0.0
|
|
|
|
|
|
@pytest.fixture
|
|
def provider(monkeypatch, config_eos):
|
|
"""Fixture to create a concrete FeedInTariffProvider instance for testing apply_fees()."""
|
|
monkeypatch.setenv("EOS_FEEDINTARIFF__FEEDINTARIFF_PROVIDER", "FeedInTariffProviderForTest")
|
|
|
|
_FeedInTariffProviderForTest.reset_instance()
|
|
return _FeedInTariffProviderForTest()
|
|
|
|
|
|
def _patch_keys_to_dataframe(monkeypatch, provider, df_elecfee: pd.DataFrame) -> AsyncMock:
|
|
"""Monkeypatch Prediction.keys_to_dataframe to return fixed fee data.
|
|
|
|
apply_fees() requires a real fee provider to already be registered and
|
|
have generated data in the prediction registry for keys_to_dataframe to
|
|
return anything - which we sidestep here by mocking the call directly,
|
|
so apply_fees() can be tested in isolation.
|
|
|
|
provider.prediction is a pydantic model with validate_assignment enabled,
|
|
so assigning directly onto the *instance* (`provider.prediction.keys_to_dataframe
|
|
= mock`) is rejected by pydantic - keys_to_dataframe is a real method, not
|
|
a declared field. Patching the *class* method instead is plain attribute
|
|
replacement and bypasses pydantic's __setattr__ validation.
|
|
"""
|
|
mock = AsyncMock(return_value=df_elecfee)
|
|
monkeypatch.setattr(type(provider.prediction), "keys_to_dataframe", mock)
|
|
return mock
|
|
|
|
|
|
class TestFeedInTariffProvider:
|
|
"""Tests for the FeedInTariffProvider base class itself (via a minimal subclass).
|
|
|
|
Only config-driven behavior is tested here - `enabled()` wiring against
|
|
`config.feedintariff.provider` specifically. Provider-identity/singleton
|
|
mechanics themselves come from PredictionMixin and are covered generically
|
|
in test_priceabc.py.
|
|
"""
|
|
|
|
def test_provider_id(self, provider):
|
|
"""Test provider ID returns correct value."""
|
|
assert provider.provider_id() == "FeedInTariffProviderForTest"
|
|
|
|
def test_invalid_provider(self, provider, monkeypatch):
|
|
"""Test requesting an unsupported provider."""
|
|
monkeypatch.setenv("EOS_FEEDINTARIFF__FEEDINTARIFF_PROVIDER", "<invalid>")
|
|
provider.config.reset_settings()
|
|
assert not provider.enabled()
|
|
|
|
|
|
class TestFeedInTariffProviderApplyFees:
|
|
"""Tests for FeedInTariffProvider._compute_gross(), via _apply_fees(), with keys_to_dataframe() mocked.
|
|
|
|
Only the feed-in-fee formula is under test here. Input validation,
|
|
non-uniform-spacing handling, and missing-fee-row zero-fill are shared
|
|
`_apply_fees` plumbing, already covered generically in test_priceabc.py
|
|
against PricePredictionProviderBase directly.
|
|
"""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_apply_fees_combines_amt_and_percent(self, provider, monkeypatch):
|
|
"""Test combined tariff = raw * (100 - percent fee) / 100 - per-Wh fee."""
|
|
start_dt = to_datetime("2024-01-01 00:00:00", in_timezone="Europe/Berlin")
|
|
idx = pd.DatetimeIndex([start_dt.add(minutes=15 * i) for i in range(4)])
|
|
|
|
raw_price_amt_wh = pd.Series([0.0001, 0.0002, 0.0003, 0.0004], index=idx, name="raw_price")
|
|
df_elecfee = pd.DataFrame(
|
|
{
|
|
"elecfee_feedin_amt_wh": [0.000288, 0.000288, 0.00034, 0.00034],
|
|
"elecfee_feedin_percent_amt": [19.0, 19.0, 19.0, 19.0],
|
|
},
|
|
index=idx,
|
|
)
|
|
|
|
mock = _patch_keys_to_dataframe(monkeypatch, provider, df_elecfee)
|
|
|
|
result = await provider._apply_fees(raw_price_amt_wh)
|
|
|
|
assert mock.await_count == 1
|
|
assert mock.await_args
|
|
called_kwargs = mock.await_args.kwargs
|
|
|
|
# Verifies _fee_keys resolves to the real feed-in-fee key names, not
|
|
# just that *some* keys get passed through (already covered
|
|
# generically in test_priceabc.py).
|
|
assert set(called_kwargs["keys"]) == {
|
|
"elecfee_feedin_amt_wh",
|
|
"elecfee_feedin_percent_amt",
|
|
}
|
|
assert called_kwargs["start_datetime"] == start_dt
|
|
assert called_kwargs["boundary"] == "context"
|
|
assert called_kwargs["align_to_interval"] is True
|
|
|
|
assert result.name == "raw_price"
|
|
assert len(result) == 4
|
|
assert not result.isna().any()
|
|
|
|
expected = [
|
|
0.0001 * (100.0 - 19.0) / 100.0 - 0.000288,
|
|
0.0002 * (100.0 - 19.0) / 100.0 - 0.000288,
|
|
0.0003 * (100.0 - 19.0) / 100.0 - 0.00034,
|
|
0.0004 * (100.0 - 19.0) / 100.0 - 0.00034,
|
|
]
|
|
for i, exp in enumerate(expected):
|
|
assert abs(result.iloc[i] - exp) < 1e-9, (
|
|
f"interval {i}: expected {exp}, got {result.iloc[i]}"
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_apply_fees_zero_percent_fee_subtracts_amt_fee_only(self, provider, monkeypatch):
|
|
"""Test that with a 0% deduction, the result is raw tariff minus the per-Wh fee only."""
|
|
start_dt = to_datetime("2024-01-01 00:00:00", in_timezone="Europe/Berlin")
|
|
idx = pd.DatetimeIndex([start_dt.add(minutes=15 * i) for i in range(4)])
|
|
|
|
raw_price_amt_wh = pd.Series([0.0002] * 4, index=idx)
|
|
df_elecfee = pd.DataFrame(
|
|
{
|
|
"elecfee_feedin_amt_wh": [0.0003] * 4,
|
|
"elecfee_feedin_percent_amt": [0.0] * 4,
|
|
},
|
|
index=idx,
|
|
)
|
|
_patch_keys_to_dataframe(monkeypatch, provider, df_elecfee)
|
|
|
|
result = await provider._apply_fees(raw_price_amt_wh)
|
|
|
|
expected = 0.0002 - 0.0003
|
|
for i in range(4):
|
|
assert abs(result.iloc[i] - expected) < 1e-9
|