mirror of
https://github.com/home-assistant/core.git
synced 2026-09-25 17:04:04 -04:00
Add coroutines to Zonneplan's coordinator (#181106)
This commit is contained in:
@@ -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"),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user