Skip to content

Commit c782489

Browse files
Fix Nord Pool sensor resolution for next/previous price (home-assistant#155119)
1 parent c9dbc14 commit c782489

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

homeassistant/components/nordpool/sensor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from collections.abc import Callable
66
from dataclasses import dataclass
7-
from datetime import datetime, timedelta
7+
from datetime import datetime
88

99
from homeassistant.components.sensor import (
1010
EntityCategory,
@@ -54,10 +54,11 @@ def get_prices(
5454
current_price_entries: dict[str, float] = {}
5555
next_price_entries: dict[str, float] = {}
5656
current_time = dt_util.utcnow()
57-
previous_time = current_time - timedelta(hours=1)
58-
next_time = current_time + timedelta(hours=1)
5957
LOGGER.debug("Price data: %s", data)
6058
for entry in data:
59+
resolution = entry.end - entry.start
60+
previous_time = current_time - resolution
61+
next_time = current_time + resolution
6162
if entry.start <= current_time <= entry.end:
6263
current_price_entries = entry.entry
6364
if entry.start <= previous_time <= entry.end:

tests/components/nordpool/snapshots/test_sensor.ambr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@
414414
'last_changed': <ANY>,
415415
'last_reported': <ANY>,
416416
'last_updated': <ANY>,
417-
'state': '1.21814',
417+
'state': '1.42403',
418418
})
419419
# ---
420420
# name: test_sensor[sensor.nord_pool_se3_off_peak_1_average-entry]
@@ -1255,7 +1255,7 @@
12551255
'last_changed': <ANY>,
12561256
'last_reported': <ANY>,
12571257
'last_updated': <ANY>,
1258-
'state': '3.82803',
1258+
'state': '2.30807',
12591259
})
12601260
# ---
12611261
# name: test_sensor[sensor.nord_pool_se4_currency-entry]
@@ -1673,7 +1673,7 @@
16731673
'last_changed': <ANY>,
16741674
'last_reported': <ANY>,
16751675
'last_updated': <ANY>,
1676-
'state': '1.40502',
1676+
'state': '1.64617',
16771677
})
16781678
# ---
16791679
# name: test_sensor[sensor.nord_pool_se4_off_peak_1_average-entry]
@@ -2514,6 +2514,6 @@
25142514
'last_changed': <ANY>,
25152515
'last_reported': <ANY>,
25162516
'last_updated': <ANY>,
2517-
'state': '4.44274',
2517+
'state': '2.67764',
25182518
})
25192519
# ---

tests/components/nordpool/test_sensor.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ async def test_sensor_no_next_price(hass: HomeAssistant, load_int: ConfigEntry)
5959
assert last_price is not None
6060
assert next_price is not None
6161
assert current_price.state == "0.78568" # SE3 2025-10-01T21:45:00Z
62-
assert last_price.state == "0.82171" # SE3 2025-10-01T21:30:00Z
63-
assert next_price.state == "0.81174" # SE3 2025-10-01T22:00:00Z
62+
assert last_price.state == "0.82005" # SE3 2025-10-01T21:30:00Z
63+
assert next_price.state == "0.93322" # SE3 2025-10-01T22:00:00Z
6464

6565

6666
@pytest.mark.freeze_time("2025-10-02T00:00:00+02:00")
@@ -78,8 +78,8 @@ async def test_sensor_no_previous_price(
7878
assert last_price is not None
7979
assert next_price is not None
8080
assert current_price.state == "0.93322" # SE3 2025-10-01T22:00:00Z
81-
assert last_price.state == "0.8605" # SE3 2025-10-01T21:45:00Z
82-
assert next_price.state == "0.83513" # SE3 2025-10-01T22:15:00Z
81+
assert last_price.state == "0.78568" # SE3 2025-10-01T21:45:00Z
82+
assert next_price.state == "0.85422" # SE3 2025-10-01T22:15:00Z
8383

8484

8585
@pytest.mark.freeze_time("2025-10-01T11:00:01+01:00")
@@ -102,8 +102,8 @@ async def test_sensor_empty_response(
102102
assert last_price is not None
103103
assert next_price is not None
104104
assert current_price.state == "0.67405"
105-
assert last_price.state == "0.8616"
106-
assert next_price.state == "0.63736"
105+
assert last_price.state == "0.60774"
106+
assert next_price.state == "0.63858"
107107

108108
aioclient_mock.clear_requests()
109109
aioclient_mock.request(
@@ -154,8 +154,8 @@ async def test_sensor_empty_response(
154154
assert last_price is not None
155155
assert next_price is not None
156156
assert current_price.state == "0.63736"
157-
assert last_price.state == "0.67405"
158-
assert next_price.state == "0.62233"
157+
assert last_price.state == "0.63482"
158+
assert next_price.state == "0.66068"
159159

160160
aioclient_mock.clear_requests()
161161
aioclient_mock.request(
@@ -207,5 +207,5 @@ async def test_sensor_empty_response(
207207
assert last_price is not None
208208
assert next_price is not None
209209
assert current_price.state == "0.78568"
210-
assert last_price.state == "0.82171"
210+
assert last_price.state == "0.82005"
211211
assert next_price.state == STATE_UNKNOWN

0 commit comments

Comments
 (0)