mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 10:47:10 +00:00
Fix SharkIQ token expiration (#89357)
This commit is contained in:
parent
47398f03dd
commit
2bda40d352
@ -2,6 +2,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from async_timeout import timeout
|
from async_timeout import timeout
|
||||||
from sharkiq import (
|
from sharkiq import (
|
||||||
@ -60,6 +61,13 @@ class SharkIqUpdateCoordinator(DataUpdateCoordinator[bool]):
|
|||||||
async def _async_update_data(self) -> bool:
|
async def _async_update_data(self) -> bool:
|
||||||
"""Update data device by device."""
|
"""Update data device by device."""
|
||||||
try:
|
try:
|
||||||
|
if self.ayla_api.token_expiring_soon:
|
||||||
|
await self.ayla_api.async_refresh_auth()
|
||||||
|
elif datetime.now() > self.ayla_api.auth_expiration - timedelta(
|
||||||
|
seconds=600
|
||||||
|
):
|
||||||
|
await self.ayla_api.async_refresh_auth()
|
||||||
|
|
||||||
all_vacuums = await self.ayla_api.async_list_devices()
|
all_vacuums = await self.ayla_api.async_list_devices()
|
||||||
self._online_dsns = {
|
self._online_dsns = {
|
||||||
v["dsn"]
|
v["dsn"]
|
||||||
@ -78,7 +86,7 @@ class SharkIqUpdateCoordinator(DataUpdateCoordinator[bool]):
|
|||||||
LOGGER.debug("Bad auth state. Attempting re-auth", exc_info=err)
|
LOGGER.debug("Bad auth state. Attempting re-auth", exc_info=err)
|
||||||
raise ConfigEntryAuthFailed from err
|
raise ConfigEntryAuthFailed from err
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
LOGGER.exception("Unexpected error updating SharkIQ")
|
LOGGER.exception("Unexpected error updating SharkIQ. Attempting re-auth")
|
||||||
raise UpdateFailed(err) from err
|
raise UpdateFailed(err) from err
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
from datetime import datetime, timedelta
|
||||||
import enum
|
import enum
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
@ -72,9 +73,14 @@ EXPECTED_FEATURES = (
|
|||||||
class MockAyla(AylaApi):
|
class MockAyla(AylaApi):
|
||||||
"""Mocked AylaApi that doesn't do anything."""
|
"""Mocked AylaApi that doesn't do anything."""
|
||||||
|
|
||||||
|
desired_expiry = False
|
||||||
|
|
||||||
async def async_sign_in(self):
|
async def async_sign_in(self):
|
||||||
"""Instead of signing in, just return."""
|
"""Instead of signing in, just return."""
|
||||||
|
|
||||||
|
async def async_refresh_auth(self):
|
||||||
|
"""Instead of refreshing auth, just return."""
|
||||||
|
|
||||||
async def async_list_devices(self) -> list[dict]:
|
async def async_list_devices(self) -> list[dict]:
|
||||||
"""Return the device list."""
|
"""Return the device list."""
|
||||||
return [SHARK_DEVICE_DICT]
|
return [SHARK_DEVICE_DICT]
|
||||||
@ -89,6 +95,18 @@ class MockAyla(AylaApi):
|
|||||||
async def async_request(self, http_method: str, url: str, **kwargs):
|
async def async_request(self, http_method: str, url: str, **kwargs):
|
||||||
"""Don't make an HTTP request."""
|
"""Don't make an HTTP request."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def token_expiring_soon(self) -> bool:
|
||||||
|
"""Toggling Property for Token Expiration Flag."""
|
||||||
|
# Alternate expiry flag for each test
|
||||||
|
self.desired_expiry = not self.desired_expiry
|
||||||
|
return self.desired_expiry
|
||||||
|
|
||||||
|
@property
|
||||||
|
def auth_expiration(self) -> datetime:
|
||||||
|
"""Sample expiration timestamp that is always 1200 seconds behind now()."""
|
||||||
|
return datetime.now() - timedelta(seconds=1200)
|
||||||
|
|
||||||
|
|
||||||
class MockShark(SharkIqVacuum):
|
class MockShark(SharkIqVacuum):
|
||||||
"""Mocked SharkIqVacuum that won't hit the API."""
|
"""Mocked SharkIqVacuum that won't hit the API."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user