11import logging
22from collections import defaultdict
3- from datetime import datetime , timedelta
4- from functools import partial
3+ from datetime import timedelta
54from random import randint
65
6+ import backoff
77import voluptuous as vol
88from homeassistant .config_entries import ConfigEntry
99from homeassistant .core import Config , HomeAssistant
1313from homeassistant .util import dt as dt_utils
1414from pytz import timezone
1515
16- from .aio_price import AioPrices
16+ from .aio_price import AioPrices , InvalidValueException
1717from .events import async_track_time_change_in_tz
1818
1919DOMAIN = "nordpool"
@@ -74,12 +74,12 @@ async def _update(self, type_="today", dt=None):
7474 if data :
7575 self ._data [currency ][type_ ] = data ["areas" ]
7676
77- async def update_today (self , _ : datetime ):
77+ async def update_today (self ):
7878 """Update today's prices"""
7979 _LOGGER .debug ("Updating today's prices." )
8080 await self ._update ("today" )
8181
82- async def update_tomorrow (self , _ : datetime ):
82+ async def update_tomorrow (self ):
8383 """Update tomorrows prices."""
8484 _LOGGER .debug ("Updating tomorrows prices." )
8585 await self ._update (type_ = "tomorrow" , dt = dt_utils .now () + timedelta (hours = 24 ))
@@ -96,8 +96,11 @@ async def _someday(self, area: str, currency: str, day: str):
9696 # set in the sensor.
9797 if currency not in self .currency :
9898 self .currency .append (currency )
99- await self .update_today (None )
100- await self .update_tomorrow (None )
99+ await self .update_today ()
100+ try :
101+ await self .update_tomorrow ()
102+ except InvalidValueException :
103+ _LOGGER .debug ("No data available for tomorrow, retrying later" )
101104
102105 # Send a new data request after new data is updated for this first run
103106 # This way if the user has multiple sensors they will all update
@@ -128,7 +131,7 @@ async def new_day_cb(_):
128131
129132 for curr in api .currency :
130133 if not api ._data .get (curr , {}).get ("tomorrow" ):
131- api ._data [curr ]["today" ] = await api .update_today (None )
134+ api ._data [curr ]["today" ] = await api .update_today ()
132135 else :
133136 api ._data [curr ]["today" ] = api ._data [curr ]["tomorrow" ]
134137 api ._data [curr ]["tomorrow" ] = {}
@@ -140,12 +143,16 @@ async def new_hr(_):
140143 _LOGGER .debug ("Called new_hr callback" )
141144 async_dispatcher_send (hass , EVENT_NEW_HOUR )
142145
143- async def new_data_cb (tdo ):
146+ @backoff .on_exception (
147+ backoff .constant ,
148+ (InvalidValueException ),
149+ logger = _LOGGER , interval = 600 , max_time = 7200 , jitter = None )
150+ async def new_data_cb (_ ):
144151 """Callback to fetch new data for tomorrows prices at 1300ish CET
145152 and notify any sensors, about the new data
146153 """
147154 # _LOGGER.debug("Called new_data_cb")
148- await api .update_tomorrow (tdo )
155+ await api .update_tomorrow ()
149156 async_dispatcher_send (hass , EVENT_NEW_PRICE )
150157
151158 # Handles futures updates
0 commit comments