11"""Data update coordinator for the Bestway API."""
22
33import asyncio
4- from datetime import timedelta
4+ from datetime import datetime , timedelta
55from logging import getLogger
66
77from homeassistant .core import HomeAssistant
1010from .bestway .api import BestwayApi , BestwayApiResults
1111
1212_LOGGER = getLogger (__name__ )
13+ _BINDINGS_REFRESH_INTERVAL = timedelta (minutes = 10 )
1314
1415
1516class BestwayUpdateCoordinator (DataUpdateCoordinator [BestwayApiResults ]):
@@ -24,6 +25,7 @@ def __init__(self, hass: HomeAssistant, api: BestwayApi) -> None:
2425 update_interval = timedelta (seconds = 30 ),
2526 )
2627 self .api = api
28+ self .last_bindings_refresh = datetime .min
2729
2830 async def _async_update_data (self ) -> BestwayApiResults :
2931 """Fetch data from API endpoint.
@@ -32,5 +34,9 @@ async def _async_update_data(self) -> BestwayApiResults:
3234 so entities can quickly look up their data.
3335 """
3436 async with asyncio .timeout (10 ):
35- await self .api .refresh_bindings ()
37+ # Refresh the device list at a slower rate
38+ # This may help with rate limiting
39+ if self .last_bindings_refresh + _BINDINGS_REFRESH_INTERVAL < datetime .now ():
40+ await self .api .refresh_bindings ()
41+ self .last_bindings_refresh = datetime .now ()
3642 return await self .api .fetch_data ()
0 commit comments