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

* now the price will return even if it is exactly 0

* now the price will return even if it is exactly 0

* now the price will return even if it is exactly 0

* clean code

* clean code

* update testing code coverage

* change zero testing to SE4

* remove row duplicate

* fix date comments

* improve testing

* simplify if-return-0

* remove unnecessary tests

* order testing rows

* restore test_sensor_no_next_price

* remove_average_price_test

* fix test name
This commit is contained in:
Mikko Koo 2025-04-01 20:45:23 +03:00 committed by Franck Nijhof
parent 91438088a0
commit 51073c948c
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
5 changed files with 20 additions and 7 deletions

View File

@ -34,7 +34,7 @@ def validate_prices(
index: int, index: int,
) -> float | None: ) -> float | None:
"""Validate and return.""" """Validate and return."""
if result := func(entity)[area][index]: if (result := func(entity)[area][index]) is not None:
return result / 1000 return result / 1000
return None return None

View File

@ -162,7 +162,7 @@
"deliveryEnd": "2024-11-05T19:00:00Z", "deliveryEnd": "2024-11-05T19:00:00Z",
"entryPerArea": { "entryPerArea": {
"SE3": 1011.77, "SE3": 1011.77,
"SE4": 1804.46 "SE4": 0.0
} }
}, },
{ {

View File

@ -519,7 +519,7 @@
'deliveryStart': '2024-11-05T18:00:00Z', 'deliveryStart': '2024-11-05T18:00:00Z',
'entryPerArea': dict({ 'entryPerArea': dict({
'SE3': 1011.77, 'SE3': 1011.77,
'SE4': 1804.46, 'SE4': 0.0,
}), }),
}), }),
dict({ dict({

View File

@ -1332,7 +1332,7 @@
'last_changed': <ANY>, 'last_changed': <ANY>,
'last_reported': <ANY>, 'last_reported': <ANY>,
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': '1.80446', 'state': '0.0',
}) })
# --- # ---
# name: test_sensor[sensor.nord_pool_se4_daily_average-entry] # name: test_sensor[sensor.nord_pool_se4_daily_average-entry]
@ -1580,9 +1580,9 @@
# name: test_sensor[sensor.nord_pool_se4_lowest_price-state] # name: test_sensor[sensor.nord_pool_se4_lowest_price-state]
StateSnapshot({ StateSnapshot({
'attributes': ReadOnlyDict({ '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', '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', 'unit_of_measurement': 'SEK/kWh',
}), }),
'context': <ANY>, 'context': <ANY>,
@ -1590,7 +1590,7 @@
'last_changed': <ANY>, 'last_changed': <ANY>,
'last_reported': <ANY>, 'last_reported': <ANY>,
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': '0.06519', 'state': '0.0',
}) })
# --- # ---
# name: test_sensor[sensor.nord_pool_se4_next_price-entry] # name: test_sensor[sensor.nord_pool_se4_next_price-entry]

View File

@ -33,6 +33,19 @@ async def test_sensor(
await snapshot_platform(hass, entity_registry, snapshot, load_int.entry_id) 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_price_is_0(
hass: HomeAssistant, load_int: ConfigEntry
) -> None:
"""Test the Nord Pool sensor working if price is 0."""
current_price = hass.states.get("sensor.nord_pool_se4_current_price")
assert current_price is not None
assert current_price.state == "0.0" # SE4 2024-11-05T18:00:00Z
@pytest.mark.freeze_time("2024-11-05T23:00:00+00:00") @pytest.mark.freeze_time("2024-11-05T23:00:00+00:00")
@pytest.mark.usefixtures("entity_registry_enabled_by_default") @pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_sensor_no_next_price(hass: HomeAssistant, load_int: ConfigEntry) -> None: async def test_sensor_no_next_price(hass: HomeAssistant, load_int: ConfigEntry) -> None: