Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6d19a91

Browse files
committedJan 20, 2024
spread fetch time and omit single errors
Spread fetch time reduces peak load on servers
1 parent b3b5e7f commit 6d19a91

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed
 

‎custom_components/epex_spot/EPEXSpot/EPEXSpotWeb/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,11 @@ async def fetch(self):
130130
delivery_date = datetime.now(ZoneInfo("Europe/Berlin"))
131131
# get data for remaining day and upcoming day
132132
# Data for the upcoming day is typically available at 12:45
133-
self._marketdata = await self._fetch_day(delivery_date) + await self._fetch_day(
133+
marketdata = await self._fetch_day(delivery_date) + await self._fetch_day(
134134
delivery_date + timedelta(days=1)
135135
)
136+
# overwrite cached marketdata only on success
137+
self._marketdata = marketdata
136138

137139
async def _fetch_day(self, delivery_date):
138140
data = await self._fetch_data(delivery_date)

‎custom_components/epex_spot/__init__.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Component for EPEX Spot support."""
22
import logging
33
from typing import Callable, Any
4+
import asyncio
5+
import random
46

57
import homeassistant.helpers.config_validation as cv
68
import voluptuous as vol
@@ -105,7 +107,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
105107
)
106108

107109
entry.async_on_unload(
108-
async_track_time_change(hass, source.fetch, hour=None, minute=58, second=0)
110+
async_track_time_change(
111+
hass, coordinator.fetch_source, hour=None, minute=50, second=0
112+
)
109113
)
110114

111115
# service call handling
@@ -211,6 +215,17 @@ async def _async_update_data(self) -> dict[str, Any]:
211215
async def on_refresh(self, *args: Any):
212216
await self.async_refresh()
213217

218+
async def fetch_source(self, *args: Any):
219+
# spread fetch over 9 minutes to reduce peak load on servers
220+
await asyncio.sleep(random.uniform(0, 9 * 60))
221+
try:
222+
await self.source.fetch()
223+
self._error_count = 0
224+
except Exception: # pylint: disable=broad-except
225+
self._error_count += 1
226+
if self._error_count >= 3:
227+
raise
228+
214229

215230
class EpexSpotEntity(CoordinatorEntity, Entity):
216231
"""A entity implementation for EPEX Spot service."""

0 commit comments

Comments
 (0)
Failed to load comments.