def api_session(self, **kwargs):
if not self.service_token or not self.user_id:
raise MiCloudException('Cannot execute request. service token or userId missing. Make sure to login.')
if kwargs.get('async'):
if not (session := self.async_session):
session = async_create_clientsession(
self.hass,
headers=self.api_headers(),
cookies=self.api_cookies(),
)
self.async_session = session
else:
session = requests.Session()
session.headers.update(self.api_headers())
session.cookies.update(self.api_cookies())
return session
if not (session := self.async_session) 这里不会满足,因此还在用旧的session继续请求,而旧的session里存着旧的service_token
在请求业务接口返回httpcode=401 或 code=3 后,有调用
_logout
https://github.com/al-one/hass-xiaomi-miot/blob/master/custom_components/xiaomi_miot/core/xiaomi_cloud.py#L235https://github.com/al-one/hass-xiaomi-miot/blob/master/custom_components/xiaomi_miot/core/xiaomi_cloud.py#L783
_logout
会将内存里的service_token
置为None,在下次请求业务接口时判断为空调用登录函数获取新service_token
。但新的
service_token
没有生效,因为 https://github.com/al-one/hass-xiaomi-miot/blob/master/custom_components/xiaomi_miot/core/xiaomi_cloud.py#L664if not (session := self.async_session)
这里不会满足,因此还在用旧的session
继续请求,而旧的session
里存着旧的service_token