Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nordpool Not to return Unknown if price is exactly 0 #140647

Draft
wants to merge 17 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/nordpool/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def validate_prices(
index: int,
) -> float | None:
"""Validate and return."""
if result := func(entity)[area][index]:
if (result := func(entity)[area][index]) is not None:
return result / 1000
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"deliveryEnd": "2024-11-05T19:00:00Z",
"entryPerArea": {
"SE3": 1011.77,
"SE4": 1804.46
"SE4": 0.0
}
},
{
Expand Down Expand Up @@ -266,7 +266,7 @@
},
{
"areaCode": "SE4",
"price": 1166.12
"price": 0.0
}
]
}
4 changes: 2 additions & 2 deletions tests/components/nordpool/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
}),
dict({
'areaCode': 'SE4',
'price': 1166.12,
'price': 0.0,
}),
]),
'areaStates': list([
Expand Down Expand Up @@ -519,7 +519,7 @@
'deliveryStart': '2024-11-05T18:00:00Z',
'entryPerArea': dict({
'SE3': 1011.77,
'SE4': 1804.46,
'SE4': 0.0,
}),
}),
dict({
Expand Down
10 changes: 5 additions & 5 deletions tests/components/nordpool/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '1.80446',
'state': '0.0',
})
# ---
# name: test_sensor[sensor.nord_pool_se4_daily_average-entry]
Expand Down Expand Up @@ -1386,7 +1386,7 @@
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '1.16612',
'state': '0.0',
})
# ---
# name: test_sensor[sensor.nord_pool_se4_exchange_rate-entry]
Expand Down Expand Up @@ -1580,17 +1580,17 @@
# name: test_sensor[sensor.nord_pool_se4_lowest_price-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'end': '2024-11-05T03:00:00+00:00',
'end': '2024-11-05T19:00:00+00:00',
'friendly_name': 'Nord Pool SE4 Lowest price',
'start': '2024-11-05T02:00:00+00:00',
'start': '2024-11-05T18:00:00+00:00',
'unit_of_measurement': 'SEK/kWh',
}),
'context': <ANY>,
'entity_id': 'sensor.nord_pool_se4_lowest_price',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '0.06519',
'state': '0.0',
})
# ---
# name: test_sensor[sensor.nord_pool_se4_next_price-entry]
Expand Down
19 changes: 19 additions & 0 deletions tests/components/nordpool/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ async def test_sensor(
await snapshot_platform(hass, entity_registry, snapshot, load_int.entry_id)


@pytest.mark.freeze_time("2024-11-05T18:00:00+00:00")
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_sensor_current_and_average_price_is_0(
hass: HomeAssistant, load_int: ConfigEntry
) -> None:
"""Test the Nord Pool sensor working if prices are 0."""

current_price = hass.states.get("sensor.nord_pool_se4_current_price")
average_price_se3 = hass.states.get("sensor.nord_pool_se3_daily_average")
average_price_se4 = hass.states.get("sensor.nord_pool_se4_daily_average")

assert current_price is not None
assert average_price_se3 is not None
assert average_price_se4 is not None
assert current_price.state == "0.0" # SE4 2024-11-05T18:00:00Z
assert average_price_se3.state == "0.90074"
assert average_price_se4.state == "0.0"


@pytest.mark.freeze_time("2024-11-05T23:00:00+00:00")
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_sensor_no_next_price(hass: HomeAssistant, load_int: ConfigEntry) -> None:
Expand Down