mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2025-04-04 00:35:33 +08:00
perf: update token and cert refresh logic
This commit is contained in:
parent
051271af6d
commit
e8b681bc66
@ -61,6 +61,7 @@ class CtrlMode(Enum):
|
|||||||
class MIoTClient:
|
class MIoTClient:
|
||||||
"""MIoT client instance."""
|
"""MIoT client instance."""
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
|
# pylint: disable=broad-exception-caught
|
||||||
_main_loop: asyncio.AbstractEventLoop
|
_main_loop: asyncio.AbstractEventLoop
|
||||||
|
|
||||||
_uid: str
|
_uid: str
|
||||||
@ -418,7 +419,8 @@ class MIoTClient:
|
|||||||
):
|
):
|
||||||
raise MIoTClientError('invalid auth info')
|
raise MIoTClientError('invalid auth info')
|
||||||
# Determine whether to update token
|
# Determine whether to update token
|
||||||
if (auth_info['expires_ts']-60) <= int(time.time()):
|
refresh_time = int(auth_info['expires_ts'] - time.time())
|
||||||
|
if refresh_time <= 60:
|
||||||
valid_auth_info = await self._oauth.refresh_access_token_async(
|
valid_auth_info = await self._oauth.refresh_access_token_async(
|
||||||
refresh_token=auth_info['refresh_token'])
|
refresh_token=auth_info['refresh_token'])
|
||||||
auth_info = valid_auth_info
|
auth_info = valid_auth_info
|
||||||
@ -436,10 +438,9 @@ class MIoTClient:
|
|||||||
_LOGGER.info(
|
_LOGGER.info(
|
||||||
'refresh oauth info, get new access_token, %s',
|
'refresh oauth info, get new access_token, %s',
|
||||||
auth_info)
|
auth_info)
|
||||||
|
refresh_time = int(auth_info['expires_ts'] - time.time())
|
||||||
refresh_time = int(auth_info['expires_ts'] - time.time())
|
if refresh_time <= 0:
|
||||||
if refresh_time <= 0:
|
raise MIoTClientError('invalid expires time')
|
||||||
raise MIoTClientError('invalid expires time')
|
|
||||||
self.__show_client_error_notify(None, 'oauth_info')
|
self.__show_client_error_notify(None, 'oauth_info')
|
||||||
self.__request_refresh_auth_info(refresh_time)
|
self.__request_refresh_auth_info(refresh_time)
|
||||||
|
|
||||||
@ -447,7 +448,7 @@ class MIoTClient:
|
|||||||
'refresh oauth info (%s, %s) after %ds',
|
'refresh oauth info (%s, %s) after %ds',
|
||||||
self._uid, self._cloud_server, refresh_time)
|
self._uid, self._cloud_server, refresh_time)
|
||||||
return True
|
return True
|
||||||
except Exception as err: # pylint: disable=broad-exception-caught
|
except Exception as err:
|
||||||
self.__show_client_error_notify(
|
self.__show_client_error_notify(
|
||||||
message=self._i18n.translate('miot.client.invalid_oauth_info'),
|
message=self._i18n.translate('miot.client.invalid_oauth_info'),
|
||||||
notify_key='oauth_info')
|
notify_key='oauth_info')
|
||||||
@ -462,12 +463,12 @@ class MIoTClient:
|
|||||||
return True
|
return True
|
||||||
if not await self._cert.verify_ca_cert_async():
|
if not await self._cert.verify_ca_cert_async():
|
||||||
raise MIoTClientError('ca cert is not ready')
|
raise MIoTClientError('ca cert is not ready')
|
||||||
if (
|
refresh_time = (
|
||||||
await self._cert.user_cert_remaining_time_async() <
|
await self._cert.user_cert_remaining_time_async() -
|
||||||
MIHOME_CERT_EXPIRE_MARGIN
|
MIHOME_CERT_EXPIRE_MARGIN)
|
||||||
):
|
if refresh_time <= 60:
|
||||||
user_key = await self._cert.load_user_key_async()
|
user_key = await self._cert.load_user_key_async()
|
||||||
if user_key is None:
|
if not user_key:
|
||||||
user_key = self._cert.gen_user_key()
|
user_key = self._cert.gen_user_key()
|
||||||
if not await self._cert.update_user_key_async(key=user_key):
|
if not await self._cert.update_user_key_async(key=user_key):
|
||||||
raise MIoTClientError('update_user_key_async failed')
|
raise MIoTClientError('update_user_key_async failed')
|
||||||
@ -477,13 +478,12 @@ class MIoTClient:
|
|||||||
if not await self._cert.update_user_cert_async(cert=crt_str):
|
if not await self._cert.update_user_cert_async(cert=crt_str):
|
||||||
raise MIoTClientError('update user cert error')
|
raise MIoTClientError('update user cert error')
|
||||||
_LOGGER.info('update_user_cert_async, %s', crt_str)
|
_LOGGER.info('update_user_cert_async, %s', crt_str)
|
||||||
|
# Create cert update task
|
||||||
# Create cert update task
|
refresh_time = (
|
||||||
refresh_time = (
|
await self._cert.user_cert_remaining_time_async() -
|
||||||
await self._cert.user_cert_remaining_time_async() -
|
MIHOME_CERT_EXPIRE_MARGIN)
|
||||||
MIHOME_CERT_EXPIRE_MARGIN)
|
if refresh_time <= 0:
|
||||||
if refresh_time <= 0:
|
raise MIoTClientError('invalid refresh time')
|
||||||
raise MIoTClientError('invalid refresh time')
|
|
||||||
self.__show_client_error_notify(None, 'user_cert')
|
self.__show_client_error_notify(None, 'user_cert')
|
||||||
self.__request_refresh_user_cert(refresh_time)
|
self.__request_refresh_user_cert(refresh_time)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user