Files
EOS/tests/test_feedintarifftibber.py
T
Andreas a2f4ef6f54 feat(optimization): split the control horizon from the forecast tail
The optimizer treated the end of `optimization.horizon_hours` as the end of the
world: energy left in the battery there was worth a single configured price per
kWh, so it either dumped the battery into the last hours or hoarded it,
depending on that one number.

The horizon is now two spans. `horizon_hours` still receives every control
command. The new `optimization.tail_horizon_hours` (default 48 h) is a pure
lookahead that never produces a command. In AUTO terminal-value mode a
deterministic dynamic program solves that tail backwards on a 101-point SoC
grid using the production battery and inverter models - SoC bounds, power caps,
conversion losses, configured charge and export rates, direct-marketing
permission and LCOS on delivered DC energy - and the existing AUTO proxy
supplies the continuation value at the tail end. Genetic fitness reads the
resulting curve. `tail_horizon_hours: 0` restores the plain proxy at the control
end, FIXED is unchanged.

The forecast budget is reported, never enforced by refusal: a tail that does not
fit is shortened to what the forecast covers and reported as
`effective_tail_hours`, and a control horizon that does not fit is warned about
at configuration time and rejected by the optimizer at run time, which knows
which series ran out. `prediction.hours` defaults to 72 so the new defaults fit
out of the box; existing shorter configurations keep starting.

Control arrays and warm-start genomes now begin at the run timestamp rather than
midnight, flagged by `controls_start_at_now` so the adapters still read older
solutions. `forecast_interval_seconds` declares the resolution of shortened
native quarter-hour inputs.

Required forecasts are no longer silently replaced by demo providers. A missing
PV, price, load, feed-in or weather forecast used to rewrite the configured
provider and retry, so a run could quietly optimize against invented data.
Missing values now stay missing, and provider values are held only within their
own source interval instead of being extended indefinitely.

Also fixes a config update that could leave EOS half-updated: the merged
candidate is validated before the singleton is reinitialized.

Four provider tests that hard-coded the old 48 h prediction default are rewritten
to derive their expectations from the configured horizon.
2026-09-09 07:56:38 +02:00

132 lines
4.6 KiB
Python

"""Tests for the native quarter-hour Tibber feed-in tariff provider."""
import json
from unittest.mock import Mock, patch
import pytest
from akkudoktoreos.core.coreabc import get_ems
from akkudoktoreos.optimization.genetic.geneticparams import (
MARKET_PRICE_FEED_IN_TARIFF_PROVIDERS,
)
from akkudoktoreos.prediction.elecpricetibber import TibberGraphQLResponse
from akkudoktoreos.prediction.feedintarifftibber import FeedInTariffTibber
from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration
def _point(starts_at: str, energy: float, total: float = 0.40) -> dict[str, object]:
return {"startsAt": starts_at, "energy": energy, "total": total}
def _payload(points: list[dict[str, object]]) -> dict[str, object]:
return {
"data": {
"viewer": {
"homes": [
{
"id": "home-1",
"currentSubscription": {
"priceInfo": {"today": points[:4], "tomorrow": points[4:]},
"priceInfoRange": {"nodes": points},
},
}
]
}
}
}
@pytest.fixture
def quarter_hour_points():
return [
_point(f"2026-07-15T0{index // 4}:{(index % 4) * 15:02d}:00+02:00", 0.10 + index / 100)
for index in range(8)
]
@pytest.fixture
def provider(config_eos):
FeedInTariffTibber.reset_instance()
config_eos.merge_settings_from_dict(
{
"elecprice": {"tibber": {"access_token": "token-123", "home_id": "home-1"}},
"feedintariff": {
"direct_marketing_enabled": True,
"provider": "FeedInTariffTibber",
},
"prediction": {"hours": 2},
"optimization": {"horizon_hours": 2, "tail_horizon_hours": 0},
}
)
value = FeedInTariffTibber()
value.highest_orig_datetime = None
value.records.clear()
get_ems().set_start_datetime(
to_datetime("2026-07-15T00:00:00+02:00", in_timezone="Europe/Berlin")
)
return value
def test_provider_is_registered_and_used_for_direct_marketing(provider, config_eos):
assert provider.enabled()
assert "FeedInTariffTibber" in config_eos.feedintariff.providers
assert "FeedInTariffTibber" in MARKET_PRICE_FEED_IN_TARIFF_PROVIDERS
def test_parse_uses_energy_component_at_native_quarter_hour_resolution(
provider, quarter_hour_points
):
response = TibberGraphQLResponse.model_validate(_payload(quarter_hour_points))
series = provider._parse_data(response)
assert series.tolist() == pytest.approx([(0.10 + index / 100) / 1000 for index in range(8)])
assert series.index.to_series().diff().dropna().dt.total_seconds().unique().tolist() == [900.0]
@patch("requests.post")
def test_request_is_strictly_quarter_hourly_and_requests_energy(
mock_post, provider, quarter_hour_points
):
response = Mock()
response.content = json.dumps(_payload(quarter_hour_points)).encode()
response.raise_for_status = Mock()
mock_post.return_value = response
provider._request_forecast(force_update=True)
query = mock_post.call_args.kwargs["json"]["query"]
assert "priceInfo(resolution: QUARTER_HOURLY)" in " ".join(query.split())
assert "priceInfoRange(resolution: QUARTER_HOURLY" in " ".join(query.split())
assert "energy" in query
assert mock_post.call_args.kwargs["headers"]["Authorization"] == "Bearer token-123"
def test_update_keeps_four_distinct_prices_per_hour(provider, quarter_hour_points, monkeypatch):
response = TibberGraphQLResponse.model_validate(_payload(quarter_hour_points))
monkeypatch.setattr(provider, "_request_forecast", lambda **_: response)
provider._update_data(force_update=True)
start = to_datetime("2026-07-15T00:00:00+02:00", in_timezone="Europe/Berlin")
prices = provider.key_to_array(
key="feed_in_tariff_wh",
start_datetime=start,
end_datetime=start + to_duration("2 hours"),
interval=to_duration("15 minutes"),
fill_method="ffill",
)
assert prices.tolist() == pytest.approx([(0.10 + index / 100) / 1000 for index in range(8)])
def test_update_rejects_hourly_tibber_data(provider, monkeypatch):
hourly = [
_point("2026-07-15T00:00:00+02:00", 0.10),
_point("2026-07-15T01:00:00+02:00", 0.11),
]
response = TibberGraphQLResponse.model_validate(_payload(hourly))
monkeypatch.setattr(provider, "_request_forecast", lambda **_: response)
with pytest.raises(ValueError, match="requires native 15-minute prices"):
provider._update_data(force_update=True)