Skip to content

Commit 434a9d4

Browse files
committed
fix: improve reauth
1 parent 5cfe6e0 commit 434a9d4

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

python_snoo/pubnub_async.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def __init__(self, pubnub: PubNubAsyncio, device_id: str) -> None:
4242

4343
def update_token(self, token: str):
4444
self.pubnub.config.auth_key = token
45+
if self.task:
46+
self.task.cancel() # cancel the task if it exists.
47+
self.task = None
48+
asyncio.create_task(self.run()) # restart the task
4549

4650
def presence(self, pubnub: PubNubAsyncio, presence):
4751
_LOGGER.debug("Received new presence: %s", presence)

python_snoo/snoo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ async def authorize(self) -> AuthorizationInfo:
227227
return self.tokens
228228

229229
async def schedule_reauthorization(self, snoo_expiry: int):
230+
_LOGGER.info("Snoo token has expired - reauthorizing...")
230231
try:
231232
await asyncio.sleep(snoo_expiry)
232233
await self.authorize()
@@ -235,7 +236,7 @@ async def schedule_reauthorization(self, snoo_expiry: int):
235236
self.pubnub.config.auth_token = self.tokens.snoo
236237

237238
except Exception as ex:
238-
print(f"Error during reauthorization: {ex}")
239+
_LOGGER.exception(f"Error during reauthorization: {ex}")
239240

240241
async def get_devices(self) -> list[SnooDevice]:
241242
hdrs = self.generate_snoo_auth_headers(self.tokens.aws_id)

0 commit comments

Comments
 (0)