Skip to content

Commit

Permalink
Add async.lock
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneNulschDE committed Mar 4, 2024
1 parent f6bf91a commit d86b97b
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions custom_components/mbapi2020/oauth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Define an object to interact with the REST API."""
from __future__ import annotations

import asyncio
from copy import deepcopy
import json
import logging
Expand Down Expand Up @@ -74,6 +75,7 @@ def __init__(
)
self.token = None
self._xsessionid = ""
self._get_token_lock = asyncio.Lock()

async def request_pin(self, email: str, nonce: str):
"""Initiate a PIN request."""
Expand Down Expand Up @@ -157,7 +159,7 @@ async def request_access_token(self, email: str, pin: str, nonce: str):

async def async_get_cached_token(self):
"""Get a cached auth token."""
# _LOGGER.debug("Start async_get_cached_token()")
_LOGGER.debug("Start async_get_cached_token()")
token_info: dict[str, any]
token_type: str = ""

Expand Down Expand Up @@ -191,27 +193,28 @@ async def async_get_cached_token(self):
token_type = "new_file"

if self.is_token_expired(token_info):
_LOGGER.debug("%s token expired -> start refresh", __name__)
if not token_info or "refresh_token" not in token_info:
_LOGGER.warning("Refresh token is missing - reauth required")
return None
async with self._get_token_lock:
_LOGGER.debug("%s token expired -> start refresh", __name__)
if not token_info or "refresh_token" not in token_info:
_LOGGER.warning("Refresh token is missing - reauth required")
return None

token_info = await self.async_refresh_access_token(token_info["refresh_token"], is_retry=False)
token_info = await self.async_refresh_access_token(token_info["refresh_token"], is_retry=False)

if not token_info and token_type == "old_file":
if os.path.exists(self._old_token_path):
_LOGGER.warning("async_get_cached_token: Delete old invalid token file")
os.remove(self._old_token_path)
if self._config_entry and self._config_entry.data and "token" in self._config_entry.data:
token_info = self._config_entry.data["token"]
token_type = "config_entry_2"
if not token_info and token_type == "old_file":
if os.path.exists(self._old_token_path):
_LOGGER.warning("async_get_cached_token: Delete old invalid token file")
os.remove(self._old_token_path)
if self._config_entry and self._config_entry.data and "token" in self._config_entry.data:
token_info = self._config_entry.data["token"]
token_type = "config_entry_2"

_LOGGER.warning("Starting Refresh token in fallback mode round 2")
token_info = await self.async_refresh_access_token(token_info["refresh_token"], is_retry=True)
_LOGGER.warning("Starting Refresh token in fallback mode round 2")
token_info = await self.async_refresh_access_token(token_info["refresh_token"], is_retry=True)

if not token_info:
_LOGGER.warning("Refresh token is missing in round 2 - reauth required")
return None
if not token_info:
_LOGGER.warning("Refresh token is missing in round 2 - reauth required")
return None

self.token = token_info
return token_info
Expand Down

0 comments on commit d86b97b

Please sign in to comment.