Skip to content

Commit

Permalink
Spread the load on the Tibber server (home-assistant#40797)
Browse files Browse the repository at this point in the history
* Tibber, Spread the load on the server

* start hour
  • Loading branch information
Danielhiversen authored Oct 3, 2020
1 parent 473afc4 commit 16d5c14
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion homeassistant/components/tibber/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
from datetime import timedelta
import logging
from random import randrange

import aiohttp

Expand Down Expand Up @@ -59,6 +60,7 @@ def __init__(self, tibber_home):
self._name = tibber_home.info["viewer"]["home"]["address"].get(
"address1", ""
)
self._spread_load_constant = randrange(3600)

@property
def device_state_attributes(self):
Expand Down Expand Up @@ -110,7 +112,8 @@ async def async_update(self):

if (
not self._tibber_home.last_data_timestamp
or (self._tibber_home.last_data_timestamp - now).total_seconds() / 3600 < 12
or (self._tibber_home.last_data_timestamp - now).total_seconds()
< 12 * 3600 + self._spread_load_constant
or not self._is_available
):
_LOGGER.debug("Asking for new data")
Expand Down Expand Up @@ -156,6 +159,7 @@ def unique_id(self):

@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def _fetch_data(self):
_LOGGER.debug("Fetching data")
try:
await self._tibber_home.update_info_and_price_info()
except (asyncio.TimeoutError, aiohttp.ClientError):
Expand Down

0 comments on commit 16d5c14

Please sign in to comment.