From a9337d2a8f41b74d8b7ec9d643090658deb1813d Mon Sep 17 00:00:00 2001 From: Erwin Douna Date: Thu, 10 Sep 2026 19:59:48 +0200 Subject: [PATCH] Add coroutines to Zonneplan's coordinator (#181106) --- .../components/zonneplan/coordinator.py | 51 +++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/zonneplan/coordinator.py b/homeassistant/components/zonneplan/coordinator.py index c0a3e6801caa..fbc439ab6fdb 100644 --- a/homeassistant/components/zonneplan/coordinator.py +++ b/homeassistant/components/zonneplan/coordinator.py @@ -1,9 +1,11 @@ """Coordinator for Zonneplan.""" +import asyncio +from collections.abc import Coroutine from dataclasses import dataclass from datetime import timedelta import logging -from typing import TYPE_CHECKING, override +from typing import TYPE_CHECKING, Any, override from pyzonneplan import ( Account, @@ -62,27 +64,34 @@ class ZonneplanCoordinator(DataUpdateCoordinator[ZonneplanData]): """Fetch data from the Zonneplan API.""" try: account = await self.zonneplan.async_get_account() - electricity_prices: ConsumerPrices | None = None - gas_prices: ConsumerPrices | None = None + price_coroutines: dict[str, Coroutine[Any, Any, ConsumerPrices]] = {} - # Depending per contract, fetch the associated consumer prices - for connection in account.connections: - if ( - "electricity" in connection.market_segment - if connection.market_segment is not None - else False - ): - electricity_prices = await self.zonneplan.async_get_consumer_prices( + if any( + connection.market_segment is not None + and "electricity" in connection.market_segment + for connection in account.connections + ): + price_coroutines["electricity"] = ( + self.zonneplan.async_get_consumer_prices( PriceChart.ELECTRICITY_HOURLY ) - if ( - "gas" in connection.market_segment - if connection.market_segment is not None - else False - ): - gas_prices = await self.zonneplan.async_get_consumer_prices( - PriceChart.GAS_DAILY - ) + ) + if any( + connection.market_segment is not None + and "gas" in connection.market_segment + for connection in account.connections + ): + price_coroutines["gas"] = self.zonneplan.async_get_consumer_prices( + PriceChart.GAS_DAILY + ) + + prices = dict( + zip( + price_coroutines, + await asyncio.gather(*price_coroutines.values()), + strict=True, + ) + ) except ZonneplanAuthenticationError as err: raise ConfigEntryAuthFailed( translation_domain=DOMAIN, @@ -111,6 +120,6 @@ class ZonneplanCoordinator(DataUpdateCoordinator[ZonneplanData]): return ZonneplanData( account=account, - electricity_prices=electricity_prices, - gas_prices=gas_prices, + electricity_prices=prices.get("electricity"), + gas_prices=prices.get("gas"), )