From 90f847f4fa19328e684ebadd49dca7122ab490ff Mon Sep 17 00:00:00 2001 From: Erwin Douna Date: Sun, 29 Nov 2026 12:27:59 +0100 Subject: [PATCH] Add Zonneplan get gas prices action (#194944) --- homeassistant/components/zonneplan/icons.json | 3 + .../components/zonneplan/services.py | 56 +++++++++++++++---- .../components/zonneplan/services.yaml | 8 +++ .../components/zonneplan/strings.json | 13 +++++ .../zonneplan/snapshots/test_services.ambr | 11 ++++ tests/components/zonneplan/test_services.py | 55 +++++++++++++++--- 6 files changed, 127 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/zonneplan/icons.json b/homeassistant/components/zonneplan/icons.json index 1a2aef586246..70e2d31c8d2c 100644 --- a/homeassistant/components/zonneplan/icons.json +++ b/homeassistant/components/zonneplan/icons.json @@ -164,6 +164,9 @@ "services": { "get_electricity_prices": { "service": "mdi:cash-clock" + }, + "get_gas_prices": { + "service": "mdi:fire" } } } diff --git a/homeassistant/components/zonneplan/services.py b/homeassistant/components/zonneplan/services.py index 40b66cb59eea..e2e99365a475 100644 --- a/homeassistant/components/zonneplan/services.py +++ b/homeassistant/components/zonneplan/services.py @@ -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, + ) diff --git a/homeassistant/components/zonneplan/services.yaml b/homeassistant/components/zonneplan/services.yaml index 03bee83a7cae..4e3a448112fe 100644 --- a/homeassistant/components/zonneplan/services.yaml +++ b/homeassistant/components/zonneplan/services.yaml @@ -14,3 +14,11 @@ get_electricity_prices: options: - hour - quarter + +get_gas_prices: + fields: + config_entry: + required: true + selector: + config_entry: + integration: zonneplan diff --git a/homeassistant/components/zonneplan/strings.json b/homeassistant/components/zonneplan/strings.json index 1c9aed324336..10cdb5f65c44 100644 --- a/homeassistant/components/zonneplan/strings.json +++ b/homeassistant/components/zonneplan/strings.json @@ -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" } } } diff --git a/tests/components/zonneplan/snapshots/test_services.ambr b/tests/components/zonneplan/snapshots/test_services.ambr index 7339163c14df..be5fa69d0755 100644 --- a/tests/components/zonneplan/snapshots/test_services.ambr +++ b/tests/components/zonneplan/snapshots/test_services.ambr @@ -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', + }), + ]), + }) +# --- diff --git a/tests/components/zonneplan/test_services.py b/tests/components/zonneplan/test_services.py index f4d95446c6ee..aba0f75cd8d3 100644 --- a/tests/components/zonneplan/test_services.py +++ b/tests/components/zonneplan/test_services.py @@ -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(