From 6d19a91687fc1a82639c1b0eb193b6f8d9eb2432 Mon Sep 17 00:00:00 2001 From: mampfes Date: Sat, 20 Jan 2024 20:23:21 +0100 Subject: [PATCH] spread fetch time and omit single errors Spread fetch time reduces peak load on servers --- .../epex_spot/EPEXSpot/EPEXSpotWeb/__init__.py | 4 +++- custom_components/epex_spot/__init__.py | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/custom_components/epex_spot/EPEXSpot/EPEXSpotWeb/__init__.py b/custom_components/epex_spot/EPEXSpot/EPEXSpotWeb/__init__.py index 40e0dbb..c0e649f 100644 --- a/custom_components/epex_spot/EPEXSpot/EPEXSpotWeb/__init__.py +++ b/custom_components/epex_spot/EPEXSpot/EPEXSpotWeb/__init__.py @@ -130,9 +130,11 @@ async def fetch(self): delivery_date = datetime.now(ZoneInfo("Europe/Berlin")) # get data for remaining day and upcoming day # Data for the upcoming day is typically available at 12:45 - self._marketdata = await self._fetch_day(delivery_date) + await self._fetch_day( + marketdata = await self._fetch_day(delivery_date) + await self._fetch_day( delivery_date + timedelta(days=1) ) + # overwrite cached marketdata only on success + self._marketdata = marketdata async def _fetch_day(self, delivery_date): data = await self._fetch_data(delivery_date) diff --git a/custom_components/epex_spot/__init__.py b/custom_components/epex_spot/__init__.py index c5b2ebc..e4bd688 100644 --- a/custom_components/epex_spot/__init__.py +++ b/custom_components/epex_spot/__init__.py @@ -1,6 +1,8 @@ """Component for EPEX Spot support.""" import logging from typing import Callable, Any +import asyncio +import random import homeassistant.helpers.config_validation as cv import voluptuous as vol @@ -105,7 +107,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) entry.async_on_unload( - async_track_time_change(hass, source.fetch, hour=None, minute=58, second=0) + async_track_time_change( + hass, coordinator.fetch_source, hour=None, minute=50, second=0 + ) ) # service call handling @@ -211,6 +215,17 @@ async def _async_update_data(self) -> dict[str, Any]: async def on_refresh(self, *args: Any): await self.async_refresh() + async def fetch_source(self, *args: Any): + # spread fetch over 9 minutes to reduce peak load on servers + await asyncio.sleep(random.uniform(0, 9 * 60)) + try: + await self.source.fetch() + self._error_count = 0 + except Exception: # pylint: disable=broad-except + self._error_count += 1 + if self._error_count >= 3: + raise + class EpexSpotEntity(CoordinatorEntity, Entity): """A entity implementation for EPEX Spot service."""