Skip to content

Commit

Permalink
spread fetch time and omit single errors
Browse files Browse the repository at this point in the history
Spread fetch time reduces peak load on servers
  • Loading branch information
mampfes committed Jan 20, 2024
1 parent b3b5e7f commit 6d19a91
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion custom_components/epex_spot/EPEXSpot/EPEXSpotWeb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 16 additions & 1 deletion custom_components/epex_spot/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 6d19a91

Please sign in to comment.