Skip to content

Commit ff64244

Browse files
committed
Slow down requests for device data
1 parent 6e27056 commit ff64244

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

custom_components/bestway/coordinator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Data update coordinator for the Bestway API."""
22

33
import asyncio
4-
from datetime import timedelta
4+
from datetime import datetime, timedelta
55
from logging import getLogger
66

77
from homeassistant.core import HomeAssistant
@@ -10,6 +10,7 @@
1010
from .bestway.api import BestwayApi, BestwayApiResults
1111

1212
_LOGGER = getLogger(__name__)
13+
_BINDINGS_REFRESH_INTERVAL = timedelta(minutes=10)
1314

1415

1516
class 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()

custom_components/bestway/entity.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Home Assistant entity descriptions."""
22

33
from __future__ import annotations
4+
from datetime import datetime, timedelta
45

56
from homeassistant.config_entries import ConfigEntry
67
from homeassistant.helpers.entity import DeviceInfo
@@ -10,6 +11,8 @@
1011
from .bestway.model import BestwayDevice, BestwayDeviceStatus
1112
from .const import DOMAIN
1213

14+
_OFFLINE_DEVICE_TIMEOUT = timedelta(minutes=5)
15+
1316

1417
class BestwayEntity(CoordinatorEntity[BestwayUpdateCoordinator]):
1518
"""Bestway base entity type."""
@@ -55,8 +58,9 @@ def status(self) -> BestwayDeviceStatus | None:
5558
@property
5659
def available(self) -> bool:
5760
"""Return True if entity is available."""
61+
offline_cutoff = int((datetime.now() - _OFFLINE_DEVICE_TIMEOUT).timestamp())
5862
return (
5963
self.coordinator.last_update_success
60-
and self.bestway_device is not None
61-
and self.bestway_device.is_online
64+
and self.status is not None
65+
and self.status.timestamp > offline_cutoff
6266
)

0 commit comments

Comments
 (0)