1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-14 23:28:42 +00:00

Fix Green Planet Energy price unit conversion (#162511)

This commit is contained in:
Peter Grauvogel
2026-02-08 21:07:32 +01:00
committed by GitHub
parent e7fd744941
commit 10497c2bf4
2 changed files with 38 additions and 21 deletions

View File

@@ -43,7 +43,11 @@ SENSOR_DESCRIPTIONS: list[GreenPlanetEnergySensorEntityDescription] = [
translation_key="highest_price_today",
native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}",
suggested_display_precision=4,
value_fn=lambda api, data: api.get_highest_price_today(data),
value_fn=lambda api, data: (
price / 100
if (price := api.get_highest_price_today(data)) is not None
else None
),
),
GreenPlanetEnergySensorEntityDescription(
key="gpe_highest_price_time",
@@ -61,7 +65,11 @@ SENSOR_DESCRIPTIONS: list[GreenPlanetEnergySensorEntityDescription] = [
native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}",
suggested_display_precision=4,
translation_placeholders={"time_range": "(06:00-18:00)"},
value_fn=lambda api, data: api.get_lowest_price_day(data),
value_fn=lambda api, data: (
price / 100
if (price := api.get_lowest_price_day(data)) is not None
else None
),
),
GreenPlanetEnergySensorEntityDescription(
key="gpe_lowest_price_day_time",
@@ -80,7 +88,11 @@ SENSOR_DESCRIPTIONS: list[GreenPlanetEnergySensorEntityDescription] = [
native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}",
suggested_display_precision=4,
translation_placeholders={"time_range": "(18:00-06:00)"},
value_fn=lambda api, data: api.get_lowest_price_night(data),
value_fn=lambda api, data: (
price / 100
if (price := api.get_lowest_price_night(data)) is not None
else None
),
),
GreenPlanetEnergySensorEntityDescription(
key="gpe_lowest_price_night_time",
@@ -98,7 +110,11 @@ SENSOR_DESCRIPTIONS: list[GreenPlanetEnergySensorEntityDescription] = [
translation_key="current_price",
native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}",
suggested_display_precision=4,
value_fn=lambda api, data: api.get_current_price(data, dt_util.now().hour),
value_fn=lambda api, data: (
price / 100
if (price := api.get_current_price(data, dt_util.now().hour)) is not None
else None
),
),
]

View File

@@ -48,13 +48,14 @@ def mock_api() -> Generator[MagicMock]:
mock_api_instance = MagicMock()
# Mock the API response data
# Today's prices: 0.20 + (hour * 0.01)
# API returns prices in Cent/kWh (e.g., 25.0 Cent/kWh = 0.25 €/kWh)
# Today's prices: 20 + (hour * 1) Cent/kWh
today_prices = {
f"gpe_price_{hour:02d}": 0.20 + (hour * 0.01) for hour in range(24)
f"gpe_price_{hour:02d}": 20.0 + (hour * 1.0) for hour in range(24)
}
# Tomorrow's prices: 0.25 + (hour * 0.01) (slightly different for testing)
# Tomorrow's prices: 25 + (hour * 1) Cent/kWh (slightly different for testing)
tomorrow_prices = {
f"gpe_price_{hour:02d}_tomorrow": 0.25 + (hour * 0.01) for hour in range(24)
f"gpe_price_{hour:02d}_tomorrow": 25.0 + (hour * 1.0) for hour in range(24)
}
# Combine all prices
@@ -63,24 +64,24 @@ def mock_api() -> Generator[MagicMock]:
# Make get_electricity_prices async since coordinator uses it
mock_api_instance.get_electricity_prices = AsyncMock(return_value=all_prices)
# Mock the calculation methods to return actual values (not coroutines)
# Highest price today: 0.20 + (23 * 0.01) = 0.43 at hour 23
mock_api_instance.get_highest_price_today.return_value = 0.43
mock_api_instance.get_highest_price_today_with_hour.return_value = (0.43, 23)
# Mock the calculation methods to return actual values in Cent/kWh (not coroutines)
# Highest price today: 20 + (23 * 1) = 43 Cent/kWh at hour 23
mock_api_instance.get_highest_price_today.return_value = 43.0
mock_api_instance.get_highest_price_today_with_hour.return_value = (43.0, 23)
# Lowest price day (6-22): 0.20 + (6 * 0.01) = 0.26 at hour 6
mock_api_instance.get_lowest_price_day.return_value = 0.26
mock_api_instance.get_lowest_price_day_with_hour.return_value = (0.26, 6)
# Lowest price day (6-22): 20 + (6 * 1) = 26 Cent/kWh at hour 6
mock_api_instance.get_lowest_price_day.return_value = 26.0
mock_api_instance.get_lowest_price_day_with_hour.return_value = (26.0, 6)
# Lowest price night (22-6): 0.20 + (0 * 0.01) = 0.20 at hour 0
mock_api_instance.get_lowest_price_night.return_value = 0.20
mock_api_instance.get_lowest_price_night_with_hour.return_value = (0.20, 0)
# Lowest price night (22-6): 20 + (0 * 1) = 20 Cent/kWh at hour 0
mock_api_instance.get_lowest_price_night.return_value = 20.0
mock_api_instance.get_lowest_price_night_with_hour.return_value = (20.0, 0)
# Current price depends on the hour passed to the method
# Mock get_current_price to return the price for the requested hour
# Mock get_current_price to return the price for the requested hour in Cent/kWh
def get_current_price_mock(data, hour):
"""Return price for a specific hour."""
return 0.20 + (hour * 0.01)
"""Return price for a specific hour in Cent/kWh."""
return 20.0 + (hour * 1.0)
mock_api_instance.get_current_price.side_effect = get_current_price_mock