Skip to content

Commit

Permalink
Use proper order for token refresh delay (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasddn committed Jan 17, 2025
1 parent e48565b commit c4b6d9b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions custom_components/volvo_cars/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import asyncio
from collections import deque
from collections.abc import Callable, Coroutine
from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
Expand Down Expand Up @@ -354,7 +355,7 @@ def __init__(
entry_name = entry.data.get(CONF_FRIENDLY_NAME) or entry.entry_id
self._name = f"{entry_name} - refresh token"

self._delays: list[int] = []
self._delays: deque[int] = []
self._unsub_refresh: CALLBACK_TYPE | None = None

async def async_schedule_refresh(self, init: bool = False) -> None:
Expand All @@ -369,7 +370,7 @@ async def async_schedule_refresh(self, init: bool = False) -> None:
_LOGGER.debug("%s - No token refresh schedule found", self._entry.entry_id)
return

delay = self._delays.pop()
delay = self._delays.popleft()
_LOGGER.debug("%s - Next token refresh in %ss", self._entry.entry_id, delay)

loop = self._hass.loop
Expand Down Expand Up @@ -433,10 +434,13 @@ async def _async_refresh_token(self, raise_on_auth_failed: bool = False) -> None
def _set_delays(self, expiry: int) -> None:
percentages = self._RETRY_AT_PERCENTAGES

self._delays = [int(percentages[0] * expiry)] + [
int((percentages[i] - percentages[i - 1]) * expiry)
for i in range(1, len(percentages))
]
self._delays = deque(
[int(percentages[0] * expiry)]
+ [
int((percentages[i] - percentages[i - 1]) * expiry)
for i in range(1, len(percentages))
]
)

@callback
def __wrap_handle_refresh_interval(self) -> None:
Expand Down

0 comments on commit c4b6d9b

Please sign in to comment.