mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2026-01-17 07:10:44 +08:00
fix: fix miot http type error
This commit is contained in:
parent
365f4e57d8
commit
b14fb6b4cf
@ -224,20 +224,20 @@ class MIoTHttpClient:
|
|||||||
_client_id: str
|
_client_id: str
|
||||||
_access_token: str
|
_access_token: str
|
||||||
|
|
||||||
_get_prop_timer: asyncio.TimerHandle
|
_get_prop_timer: Optional[asyncio.TimerHandle]
|
||||||
_get_prop_list: dict[str, dict[str, asyncio.Future | str | bool]]
|
_get_prop_list: dict[str, dict]
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, cloud_server: str, client_id: str, access_token: str,
|
self, cloud_server: str, client_id: str, access_token: str,
|
||||||
loop: Optional[asyncio.AbstractEventLoop] = None
|
loop: Optional[asyncio.AbstractEventLoop] = None
|
||||||
) -> None:
|
) -> None:
|
||||||
self._main_loop = loop or asyncio.get_running_loop()
|
self._main_loop = loop or asyncio.get_running_loop()
|
||||||
self._host = None
|
self._host = DEFAULT_OAUTH2_API_HOST
|
||||||
self._base_url = None
|
self._base_url = ''
|
||||||
self._client_id = None
|
self._client_id = ''
|
||||||
self._access_token = None
|
self._access_token = ''
|
||||||
|
|
||||||
self._get_prop_timer: asyncio.TimerHandle = None
|
self._get_prop_timer = None
|
||||||
self._get_prop_list = {}
|
self._get_prop_list = {}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -258,8 +258,9 @@ class MIoTHttpClient:
|
|||||||
self._get_prop_timer.cancel()
|
self._get_prop_timer.cancel()
|
||||||
self._get_prop_timer = None
|
self._get_prop_timer = None
|
||||||
for item in self._get_prop_list.values():
|
for item in self._get_prop_list.values():
|
||||||
fut: asyncio.Future = item.get('fut')
|
fut: Optional[asyncio.Future] = item.get('fut', None)
|
||||||
fut.cancel()
|
if fut:
|
||||||
|
fut.cancel()
|
||||||
self._get_prop_list.clear()
|
self._get_prop_list.clear()
|
||||||
if self._session and not self._session.closed:
|
if self._session and not self._session.closed:
|
||||||
await self._session.close()
|
await self._session.close()
|
||||||
@ -270,9 +271,7 @@ class MIoTHttpClient:
|
|||||||
access_token: Optional[str] = None
|
access_token: Optional[str] = None
|
||||||
) -> None:
|
) -> None:
|
||||||
if isinstance(cloud_server, str):
|
if isinstance(cloud_server, str):
|
||||||
if cloud_server == 'cn':
|
if cloud_server != 'cn':
|
||||||
self._host = DEFAULT_OAUTH2_API_HOST
|
|
||||||
else:
|
|
||||||
self._host = f'{cloud_server}.{DEFAULT_OAUTH2_API_HOST}'
|
self._host = f'{cloud_server}.{DEFAULT_OAUTH2_API_HOST}'
|
||||||
self._base_url = f'https://{self._host}'
|
self._base_url = f'https://{self._host}'
|
||||||
if isinstance(client_id, str):
|
if isinstance(client_id, str):
|
||||||
@ -350,8 +349,8 @@ class MIoTHttpClient:
|
|||||||
async def get_user_info_async(self) -> dict:
|
async def get_user_info_async(self) -> dict:
|
||||||
http_res = await self._session.get(
|
http_res = await self._session.get(
|
||||||
url='https://open.account.xiaomi.com/user/profile',
|
url='https://open.account.xiaomi.com/user/profile',
|
||||||
params={'clientId': self._client_id,
|
params={
|
||||||
'token': self._access_token},
|
'clientId': self._client_id, 'token': self._access_token},
|
||||||
headers={'content-type': 'application/x-www-form-urlencoded'},
|
headers={'content-type': 'application/x-www-form-urlencoded'},
|
||||||
timeout=MIHOME_HTTP_API_TIMEOUT
|
timeout=MIHOME_HTTP_API_TIMEOUT
|
||||||
)
|
)
|
||||||
@ -386,7 +385,9 @@ class MIoTHttpClient:
|
|||||||
|
|
||||||
return cert
|
return cert
|
||||||
|
|
||||||
async def __get_dev_room_page_async(self, max_id: str = None) -> dict:
|
async def __get_dev_room_page_async(
|
||||||
|
self, max_id: Optional[str] = None
|
||||||
|
) -> dict:
|
||||||
res_obj = await self.__mihome_api_post_async(
|
res_obj = await self.__mihome_api_post_async(
|
||||||
url_path='/app/v2/homeroom/get_dev_room_page',
|
url_path='/app/v2/homeroom/get_dev_room_page',
|
||||||
data={
|
data={
|
||||||
@ -442,7 +443,7 @@ class MIoTHttpClient:
|
|||||||
if 'result' not in res_obj:
|
if 'result' not in res_obj:
|
||||||
raise MIoTHttpError('invalid response result')
|
raise MIoTHttpError('invalid response result')
|
||||||
|
|
||||||
uid: str = None
|
uid: Optional[str] = None
|
||||||
home_infos: dict = {}
|
home_infos: dict = {}
|
||||||
for device_source in ['homelist', 'share_home_list']:
|
for device_source in ['homelist', 'share_home_list']:
|
||||||
home_infos.setdefault(device_source, {})
|
home_infos.setdefault(device_source, {})
|
||||||
@ -507,7 +508,7 @@ class MIoTHttpClient:
|
|||||||
return (await self.get_homeinfos_async()).get('uid', None)
|
return (await self.get_homeinfos_async()).get('uid', None)
|
||||||
|
|
||||||
async def __get_device_list_page_async(
|
async def __get_device_list_page_async(
|
||||||
self, dids: list[str], start_did: str = None
|
self, dids: list[str], start_did: Optional[str] = None
|
||||||
) -> dict[str, dict]:
|
) -> dict[str, dict]:
|
||||||
req_data: dict = {
|
req_data: dict = {
|
||||||
'limit': 200,
|
'limit': 200,
|
||||||
@ -575,9 +576,9 @@ class MIoTHttpClient:
|
|||||||
|
|
||||||
async def get_devices_with_dids_async(
|
async def get_devices_with_dids_async(
|
||||||
self, dids: list[str]
|
self, dids: list[str]
|
||||||
) -> dict[str, dict]:
|
) -> Optional[dict[str, dict]]:
|
||||||
results: list[dict[str, dict]] = await asyncio.gather(
|
results: list[dict[str, dict]] = await asyncio.gather(
|
||||||
*[self.__get_device_list_page_async(dids[index:index+150])
|
*[self.__get_device_list_page_async(dids=dids[index:index+150])
|
||||||
for index in range(0, len(dids), 150)])
|
for index in range(0, len(dids), 150)])
|
||||||
devices = {}
|
devices = {}
|
||||||
for result in results:
|
for result in results:
|
||||||
@ -587,7 +588,7 @@ class MIoTHttpClient:
|
|||||||
return devices
|
return devices
|
||||||
|
|
||||||
async def get_devices_async(
|
async def get_devices_async(
|
||||||
self, home_ids: list[str] = None
|
self, home_ids: Optional[list[str]] = None
|
||||||
) -> dict[str, dict]:
|
) -> dict[str, dict]:
|
||||||
homeinfos = await self.get_homeinfos_async()
|
homeinfos = await self.get_homeinfos_async()
|
||||||
homes: dict[str, dict[str, Any]] = {}
|
homes: dict[str, dict[str, Any]] = {}
|
||||||
@ -627,8 +628,9 @@ class MIoTHttpClient:
|
|||||||
'group_id': group_id
|
'group_id': group_id
|
||||||
} for did in room_info.get('dids', [])})
|
} for did in room_info.get('dids', [])})
|
||||||
dids = sorted(list(devices.keys()))
|
dids = sorted(list(devices.keys()))
|
||||||
results: dict[str, dict] = await self.get_devices_with_dids_async(
|
results = await self.get_devices_with_dids_async(dids=dids)
|
||||||
dids=dids)
|
if results is None:
|
||||||
|
raise MIoTHttpError('get devices failed')
|
||||||
for did in dids:
|
for did in dids:
|
||||||
if did not in results:
|
if did not in results:
|
||||||
devices.pop(did, None)
|
devices.pop(did, None)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user