fix(tibber): fetch forward-looking today+tomorrow via priceInfo(QUARTER_HOURLY)
priceInfoRange is a historical cursor connection whose range ends at "now": it
never returns upcoming slots. With it, the DB only ever held prices up to the
last hourly refresh, which caused two problems:
1. The price chart could only show history up to now, never a forward curve.
2. Worse, per-slot billing was subtly wrong. _tibber_strategy looks up the
price via `starts_at <= t0` (nearest slot at or before the period). Because
a period's exact 15-min slot was not fetched until the next hourly refresh
(~1h later), intra-hour periods were billed with the PREVIOUS quarter's
price and then locked in by the immutability guard — never corrected.
Switch to priceInfo(resolution: QUARTER_HOURLY) { today tomorrow }, which is
forward-looking AND quarter-hourly: today is always the full local day (96
slots) and tomorrow fills in once Tibber publishes day-ahead prices (picked up
by the next hourly refresh). Every 15-min slot's exact price is now in the DB
before the slot closes, so each period finds its own slot (accurate billing)
and the live current-price entity stays fresh.
Verified against the live Tibber API: fetch_price_range returns 192 points
(96 today + 96 tomorrow), spanning local today 00:00 → tomorrow 23:45, with
future slots present (previously 0).
This commit is contained in:
+90
-35
@@ -69,22 +69,28 @@ _THREE_NODES = [
|
||||
},
|
||||
]
|
||||
|
||||
_PRICE_RANGE_RESPONSE = {
|
||||
"data": {
|
||||
"viewer": {
|
||||
"homes": [
|
||||
{
|
||||
"id": "home-id-1",
|
||||
"currentSubscription": {
|
||||
"priceInfoRange": {
|
||||
"nodes": _THREE_NODES,
|
||||
}
|
||||
},
|
||||
}
|
||||
]
|
||||
def _price_info_response(today: list[dict], tomorrow: list[dict] | None = None) -> dict:
|
||||
"""Build a priceInfo(resolution: QUARTER_HOURLY) { today tomorrow } response."""
|
||||
return {
|
||||
"data": {
|
||||
"viewer": {
|
||||
"homes": [
|
||||
{
|
||||
"id": "home-id-1",
|
||||
"currentSubscription": {
|
||||
"priceInfo": {
|
||||
"today": today,
|
||||
"tomorrow": tomorrow if tomorrow is not None else [],
|
||||
}
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_PRICE_RANGE_RESPONSE = _price_info_response(_THREE_NODES)
|
||||
|
||||
_CURRENT_PRICE_RESPONSE = {
|
||||
"data": {
|
||||
@@ -173,23 +179,8 @@ def test_fetch_price_range_parses_nodes(monkeypatch):
|
||||
|
||||
def test_fetch_price_range_does_not_assume_node_count(monkeypatch):
|
||||
"""Parser handles an arbitrary number of nodes (not hardcoded to 96)."""
|
||||
# Build a response with a single node only.
|
||||
one_node_response = {
|
||||
"data": {
|
||||
"viewer": {
|
||||
"homes": [
|
||||
{
|
||||
"id": "home-id-1",
|
||||
"currentSubscription": {
|
||||
"priceInfoRange": {
|
||||
"nodes": [_THREE_NODES[0]],
|
||||
}
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
# Build a response with a single today node and no tomorrow yet.
|
||||
one_node_response = _price_info_response([_THREE_NODES[0]])
|
||||
transport = _make_transport(200, one_node_response)
|
||||
|
||||
def _patched_post(url, *, json, headers, timeout): # noqa: A002
|
||||
@@ -202,6 +193,68 @@ def test_fetch_price_range_does_not_assume_node_count(monkeypatch):
|
||||
assert len(points) == 1
|
||||
|
||||
|
||||
def test_fetch_price_range_concatenates_today_and_tomorrow(monkeypatch):
|
||||
"""today and tomorrow node lists are both parsed (today first, then tomorrow)."""
|
||||
tomorrow_nodes = [
|
||||
{
|
||||
"startsAt": "2026-06-24T00:00:00.000+02:00",
|
||||
"total": 0.40,
|
||||
"energy": 0.32,
|
||||
"tax": 0.08,
|
||||
"currency": "EUR",
|
||||
"level": "EXPENSIVE",
|
||||
},
|
||||
]
|
||||
response = _price_info_response(_THREE_NODES, tomorrow_nodes)
|
||||
transport = _make_transport(200, response)
|
||||
|
||||
def _patched_post(url, *, json, headers, timeout): # noqa: A002
|
||||
client = httpx.Client(transport=transport)
|
||||
return client.post(url, json=json, headers=headers, timeout=timeout)
|
||||
|
||||
monkeypatch.setattr("app.integrations.tibber.client.httpx.post", _patched_post)
|
||||
|
||||
points = fetch_price_range(_FAKE_TOKEN)
|
||||
# 3 today + 1 tomorrow, in order.
|
||||
assert len(points) == 4
|
||||
# First node is today's first; last node is tomorrow's.
|
||||
assert points[0].starts_at == datetime(2026, 6, 22, 22, 0, 0, tzinfo=UTC)
|
||||
# 2026-06-24T00:00:00+02:00 → 2026-06-23T22:00:00Z
|
||||
assert points[-1].starts_at == datetime(2026, 6, 23, 22, 0, 0, tzinfo=UTC)
|
||||
assert points[-1].total == pytest.approx(0.40)
|
||||
|
||||
|
||||
def test_fetch_price_range_tomorrow_null_returns_today_only(monkeypatch):
|
||||
"""A null tomorrow (before day-ahead publication) yields today's nodes only."""
|
||||
response = {
|
||||
"data": {
|
||||
"viewer": {
|
||||
"homes": [
|
||||
{
|
||||
"id": "home-id-1",
|
||||
"currentSubscription": {
|
||||
"priceInfo": {
|
||||
"today": _THREE_NODES,
|
||||
"tomorrow": None,
|
||||
}
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
transport = _make_transport(200, response)
|
||||
|
||||
def _patched_post(url, *, json, headers, timeout): # noqa: A002
|
||||
client = httpx.Client(transport=transport)
|
||||
return client.post(url, json=json, headers=headers, timeout=timeout)
|
||||
|
||||
monkeypatch.setattr("app.integrations.tibber.client.httpx.post", _patched_post)
|
||||
|
||||
points = fetch_price_range(_FAKE_TOKEN)
|
||||
assert len(points) == 3
|
||||
|
||||
|
||||
def test_fetch_price_range_home_id_selection(monkeypatch):
|
||||
"""When home_id is specified, the matching home is selected."""
|
||||
two_homes_response = {
|
||||
@@ -211,16 +264,18 @@ def test_fetch_price_range_home_id_selection(monkeypatch):
|
||||
{
|
||||
"id": "home-id-first",
|
||||
"currentSubscription": {
|
||||
"priceInfoRange": {
|
||||
"nodes": [_THREE_NODES[0]],
|
||||
"priceInfo": {
|
||||
"today": [_THREE_NODES[0]],
|
||||
"tomorrow": [],
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "home-id-second",
|
||||
"currentSubscription": {
|
||||
"priceInfoRange": {
|
||||
"nodes": [_THREE_NODES[1], _THREE_NODES[2]],
|
||||
"priceInfo": {
|
||||
"today": [_THREE_NODES[1], _THREE_NODES[2]],
|
||||
"tomorrow": [],
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user