Skip to content

Commit

Permalink
Fix SharkIQ token expiration (home-assistant#89357)
Browse files Browse the repository at this point in the history
  • Loading branch information
funkybunch authored Apr 16, 2023
1 parent 9d68cdc commit 0cf29f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion homeassistant/components/sharkiq/update_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import asyncio
from datetime import datetime, timedelta

from async_timeout import timeout
from sharkiq import (
Expand Down Expand Up @@ -60,6 +61,13 @@ async def _async_update_vacuum(sharkiq: SharkIqVacuum) -> None:
async def _async_update_data(self) -> bool:
"""Update data device by device."""
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()
self._online_dsns = {
v["dsn"]
Expand All @@ -78,7 +86,7 @@ async def _async_update_data(self) -> bool:
LOGGER.debug("Bad auth state. Attempting re-auth", exc_info=err)
raise ConfigEntryAuthFailed from 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

return True
18 changes: 18 additions & 0 deletions tests/components/sharkiq/test_vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from collections.abc import Iterable
from copy import deepcopy
from datetime import datetime, timedelta
import enum
from typing import Any
from unittest.mock import patch
Expand Down Expand Up @@ -72,9 +73,14 @@
class MockAyla(AylaApi):
"""Mocked AylaApi that doesn't do anything."""

desired_expiry = False

async def async_sign_in(self):
"""Instead of signing in, just return."""

async def async_refresh_auth(self):
"""Instead of refreshing auth, just return."""

async def async_sign_out(self):
"""Instead of signing out, just return."""

Expand All @@ -92,6 +98,18 @@ async def async_get_devices(self, update: bool = True) -> list[SharkIqVacuum]:
async def async_request(self, http_method: str, url: str, **kwargs):
"""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):
"""Mocked SharkIqVacuum that won't hit the API."""
Expand Down

0 comments on commit 0cf29f0

Please sign in to comment.