Add Zonneplan get gas prices action (#194944)

This commit is contained in:
Erwin Douna
2026-11-29 11:27:59 +00:00
committed by GitHub
parent 72310e9aa8
commit 90f847f4fa
6 changed files with 127 additions and 19 deletions
@@ -164,6 +164,9 @@
"services": {
"get_electricity_prices": {
"service": "mdi:cash-clock"
},
"get_gas_prices": {
"service": "mdi:fire"
}
}
}
+46 -10
View File
@@ -3,6 +3,7 @@
from enum import StrEnum
import probatio
from pyzonneplan import ConsumerPrices
from homeassistant.core import (
HomeAssistant,
@@ -21,6 +22,7 @@ class ZonneplanService(StrEnum):
"""Zonneplan actions."""
GET_ELECTRICITY_PRICES = "get_electricity_prices"
GET_GAS_PRICES = "get_gas_prices"
class ZonneplanServiceArgument(StrEnum):
@@ -41,6 +43,28 @@ GET_ELECTRICITY_PRICES_SCHEMA = probatio.Schema(
}
)
GET_GAS_PRICES_SCHEMA = probatio.Schema(
{
probatio.Required(
ZonneplanServiceArgument.CONFIG_ENTRY
): selector.ConfigEntrySelector({"integration": DOMAIN}),
}
)
def _prices_response(prices: ConsumerPrices) -> ServiceResponse:
"""Return the price points as action response data."""
return {
"prices": [
{
"start": point.start_date.isoformat(),
"end": point.end_date.isoformat(),
"price": float(point.price_tax_included.euro),
}
for point in prices.prices
]
}
async def _async_get_electricity_prices(call: ServiceCall) -> ServiceResponse:
"""Return the electricity prices the integration has fetched."""
@@ -59,16 +83,21 @@ async def _async_get_electricity_prices(call: ServiceCall) -> ServiceResponse:
translation_domain=DOMAIN,
translation_key="no_electricity_prices",
)
return {
"prices": [
{
"start": point.start_date.isoformat(),
"end": point.end_date.isoformat(),
"price": float(point.price_tax_included.euro),
}
for point in prices.prices
]
}
return _prices_response(prices)
async def _async_get_gas_prices(call: ServiceCall) -> ServiceResponse:
"""Return the gas prices the integration has fetched."""
entry = service.async_get_config_entry(
call.hass, DOMAIN, call.data[ZonneplanServiceArgument.CONFIG_ENTRY]
)
if (prices := entry.runtime_data.coordinator.data.gas_prices) is None:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="no_gas_prices",
)
return _prices_response(prices)
@callback
@@ -81,3 +110,10 @@ def async_setup_services(hass: HomeAssistant) -> None:
schema=GET_ELECTRICITY_PRICES_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
hass.services.async_register(
DOMAIN,
ZonneplanService.GET_GAS_PRICES,
_async_get_gas_prices,
schema=GET_GAS_PRICES_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
@@ -14,3 +14,11 @@ get_electricity_prices:
options:
- hour
- quarter
get_gas_prices:
fields:
config_entry:
required: true
selector:
config_entry:
integration: zonneplan
@@ -263,6 +263,9 @@
"no_electricity_prices": {
"message": "The Zonneplan account has no electricity prices"
},
"no_gas_prices": {
"message": "The Zonneplan account has no gas prices"
},
"rate_limited": {
"message": "The Zonneplan API rate limit was reached"
},
@@ -292,6 +295,16 @@
}
},
"name": "Get electricity prices"
},
"get_gas_prices": {
"description": "Returns the all-in gas prices from Zonneplan.",
"fields": {
"config_entry": {
"description": "[%key:component::zonneplan::services::get_electricity_prices::fields::config_entry::description%]",
"name": "[%key:component::zonneplan::services::get_electricity_prices::fields::config_entry::name%]"
}
},
"name": "Get gas prices"
}
}
}
@@ -336,3 +336,14 @@
]),
})
# ---
# name: test_get_gas_prices
dict({
'prices': list([
dict({
'end': '2026-08-30T04:00:00+00:00',
'price': 1.6029802,
'start': '2026-08-29T04:00:00+00:00',
}),
]),
})
# ---
+46 -9
View File
@@ -41,12 +41,52 @@ async def test_get_electricity_prices(
assert response == snapshot
async def test_get_electricity_prices_without_electricity(
async def test_get_gas_prices(
hass: HomeAssistant,
init_integration: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test getting the gas prices."""
response = await hass.services.async_call(
DOMAIN,
ZonneplanService.GET_GAS_PRICES,
{ZonneplanServiceArgument.CONFIG_ENTRY: init_integration.entry_id},
blocking=True,
return_response=True,
)
assert response == snapshot
@pytest.mark.parametrize(
("market_segment", "action", "data", "translation_key"),
[
pytest.param(
"electricity",
ZonneplanService.GET_ELECTRICITY_PRICES,
{ZonneplanServiceArgument.INTERVAL: "hour"},
"no_electricity_prices",
id="electricity",
),
pytest.param(
"gas",
ZonneplanService.GET_GAS_PRICES,
{},
"no_gas_prices",
id="gas",
),
],
)
async def test_get_prices_without_market_segment(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_zonneplan_client: AsyncMock,
market_segment: str,
action: ZonneplanService,
data: dict[str, str],
translation_key: str,
) -> None:
"""Test getting the electricity prices of an account without electricity."""
"""Test getting the prices of a market segment the account doesn't have."""
mock_zonneplan_client.async_get_account.return_value = dataclasses.replace(
MOCK_ACCOUNT,
address_groups=[
@@ -55,7 +95,7 @@ async def test_get_electricity_prices_without_electricity(
connections=[
connection
for connection in address_group.connections
if connection.market_segment != "electricity"
if connection.market_segment != market_segment
],
)
for address_group in MOCK_ACCOUNT.address_groups
@@ -68,15 +108,12 @@ async def test_get_electricity_prices_without_electricity(
with pytest.raises(ServiceValidationError) as exc_info:
await hass.services.async_call(
DOMAIN,
ZonneplanService.GET_ELECTRICITY_PRICES,
{
ZonneplanServiceArgument.CONFIG_ENTRY: mock_config_entry.entry_id,
ZonneplanServiceArgument.INTERVAL: "hour",
},
action,
{ZonneplanServiceArgument.CONFIG_ENTRY: mock_config_entry.entry_id, **data},
blocking=True,
return_response=True,
)
assert exc_info.value.translation_key == "no_electricity_prices"
assert exc_info.value.translation_key == translation_key
@pytest.mark.parametrize(