mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Fix nordpool dont have previous or next price (#132457)
This commit is contained in:
parent
ff46b3a2bb
commit
ce3db31b30
@ -27,7 +27,9 @@ from .entity import NordpoolBaseEntity
|
|||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
|
|
||||||
def get_prices(data: DeliveryPeriodData) -> dict[str, tuple[float, float, float]]:
|
def get_prices(
|
||||||
|
data: DeliveryPeriodData,
|
||||||
|
) -> dict[str, tuple[float | None, float, float | None]]:
|
||||||
"""Return previous, current and next prices.
|
"""Return previous, current and next prices.
|
||||||
|
|
||||||
Output: {"SE3": (10.0, 10.5, 12.1)}
|
Output: {"SE3": (10.0, 10.5, 12.1)}
|
||||||
@ -39,6 +41,7 @@ def get_prices(data: DeliveryPeriodData) -> dict[str, tuple[float, float, float]
|
|||||||
previous_time = current_time - timedelta(hours=1)
|
previous_time = current_time - timedelta(hours=1)
|
||||||
next_time = current_time + timedelta(hours=1)
|
next_time = current_time + timedelta(hours=1)
|
||||||
price_data = data.entries
|
price_data = data.entries
|
||||||
|
LOGGER.debug("Price data: %s", price_data)
|
||||||
for entry in price_data:
|
for entry in price_data:
|
||||||
if entry.start <= current_time <= entry.end:
|
if entry.start <= current_time <= entry.end:
|
||||||
current_price_entries = entry.entry
|
current_price_entries = entry.entry
|
||||||
@ -46,10 +49,20 @@ def get_prices(data: DeliveryPeriodData) -> dict[str, tuple[float, float, float]
|
|||||||
last_price_entries = entry.entry
|
last_price_entries = entry.entry
|
||||||
if entry.start <= next_time <= entry.end:
|
if entry.start <= next_time <= entry.end:
|
||||||
next_price_entries = entry.entry
|
next_price_entries = entry.entry
|
||||||
|
LOGGER.debug(
|
||||||
|
"Last price %s, current price %s, next price %s",
|
||||||
|
last_price_entries,
|
||||||
|
current_price_entries,
|
||||||
|
next_price_entries,
|
||||||
|
)
|
||||||
|
|
||||||
result = {}
|
result = {}
|
||||||
for area, price in current_price_entries.items():
|
for area, price in current_price_entries.items():
|
||||||
result[area] = (last_price_entries[area], price, next_price_entries[area])
|
result[area] = (
|
||||||
|
last_price_entries.get(area),
|
||||||
|
price,
|
||||||
|
next_price_entries.get(area),
|
||||||
|
)
|
||||||
LOGGER.debug("Prices: %s", result)
|
LOGGER.debug("Prices: %s", result)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -90,7 +103,7 @@ class NordpoolDefaultSensorEntityDescription(SensorEntityDescription):
|
|||||||
class NordpoolPricesSensorEntityDescription(SensorEntityDescription):
|
class NordpoolPricesSensorEntityDescription(SensorEntityDescription):
|
||||||
"""Describes Nord Pool prices sensor entity."""
|
"""Describes Nord Pool prices sensor entity."""
|
||||||
|
|
||||||
value_fn: Callable[[tuple[float, float, float]], float | None]
|
value_fn: Callable[[tuple[float | None, float, float | None]], float | None]
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
@ -136,13 +149,13 @@ PRICES_SENSOR_TYPES: tuple[NordpoolPricesSensorEntityDescription, ...] = (
|
|||||||
NordpoolPricesSensorEntityDescription(
|
NordpoolPricesSensorEntityDescription(
|
||||||
key="last_price",
|
key="last_price",
|
||||||
translation_key="last_price",
|
translation_key="last_price",
|
||||||
value_fn=lambda data: data[0] / 1000,
|
value_fn=lambda data: data[0] / 1000 if data[0] else None,
|
||||||
suggested_display_precision=2,
|
suggested_display_precision=2,
|
||||||
),
|
),
|
||||||
NordpoolPricesSensorEntityDescription(
|
NordpoolPricesSensorEntityDescription(
|
||||||
key="next_price",
|
key="next_price",
|
||||||
translation_key="next_price",
|
translation_key="next_price",
|
||||||
value_fn=lambda data: data[2] / 1000,
|
value_fn=lambda data: data[2] / 1000 if data[2] else None,
|
||||||
suggested_display_precision=2,
|
suggested_display_precision=2,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user