mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2025-05-03 23:05:04 +08:00
feat: import shared devices (#1021)
This commit is contained in:
parent
7a459de766
commit
3abccc2491
@ -565,27 +565,30 @@ class XiaomiMihomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
home_list = {}
|
||||
tip_devices = self._miot_i18n.translate(key='config.other.devices')
|
||||
# home list
|
||||
for home_id, home_info in self._cc_home_info[
|
||||
'homes']['home_list'].items():
|
||||
# i18n
|
||||
tip_central = ''
|
||||
group_id = home_info.get('group_id', None)
|
||||
dev_list = {
|
||||
device['did']: device
|
||||
for device in list(self._cc_home_info['devices'].values())
|
||||
if device.get('home_id', None) == home_id}
|
||||
if (
|
||||
mips_list
|
||||
and group_id in mips_list
|
||||
and mips_list[group_id].get('did', None) in dev_list
|
||||
):
|
||||
for device_source in ['home_list','share_home_list',
|
||||
'separated_shared_list']:
|
||||
for home_id, home_info in self._cc_home_info[
|
||||
'homes'][device_source].items():
|
||||
# i18n
|
||||
tip_central = self._miot_i18n.translate(
|
||||
key='config.other.found_central_gateway')
|
||||
home_info['central_did'] = mips_list[group_id].get('did', None)
|
||||
home_list[home_id] = (
|
||||
f'{home_info["home_name"]} '
|
||||
f'[ {len(dev_list)} {tip_devices} {tip_central} ]')
|
||||
tip_central = ''
|
||||
group_id = home_info.get('group_id', None)
|
||||
dev_list = {
|
||||
device['did']: device
|
||||
for device in list(self._cc_home_info['devices'].values())
|
||||
if device.get('home_id', None) == home_id}
|
||||
if (
|
||||
mips_list
|
||||
and group_id in mips_list
|
||||
and mips_list[group_id].get('did', None) in dev_list
|
||||
):
|
||||
# i18n
|
||||
tip_central = self._miot_i18n.translate(
|
||||
key='config.other.found_central_gateway')
|
||||
home_info['central_did'] = mips_list[group_id].get(
|
||||
'did', None)
|
||||
home_list[home_id] = (
|
||||
f'{home_info["home_name"]} '
|
||||
f'[ {len(dev_list)} {tip_devices} {tip_central} ]')
|
||||
|
||||
self._cc_home_list_show = dict(sorted(home_list.items()))
|
||||
|
||||
@ -660,10 +663,12 @@ class XiaomiMihomeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
if not home_selected:
|
||||
return await self.__show_homes_select_form(
|
||||
'no_family_selected')
|
||||
for home_id, home_info in self._cc_home_info[
|
||||
'homes']['home_list'].items():
|
||||
if home_id in home_selected:
|
||||
self._home_selected[home_id] = home_info
|
||||
for device_source in ['home_list','share_home_list',
|
||||
'separated_shared_list']:
|
||||
for home_id, home_info in self._cc_home_info[
|
||||
'homes'][device_source].items():
|
||||
if home_id in home_selected:
|
||||
self._home_selected[home_id] = home_info
|
||||
self._area_name_rule = user_input.get(
|
||||
'area_name_rule', self._area_name_rule)
|
||||
# Storage device list
|
||||
@ -1420,27 +1425,29 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
||||
home_list = {}
|
||||
tip_devices = self._miot_i18n.translate(key='config.other.devices')
|
||||
# home list
|
||||
for home_id, home_info in self._cc_home_info[
|
||||
'homes']['home_list'].items():
|
||||
# i18n
|
||||
tip_central = ''
|
||||
group_id = home_info.get('group_id', None)
|
||||
did_list = {
|
||||
device['did']: device for device in list(
|
||||
self._cc_home_info['devices'].values())
|
||||
if device.get('home_id', None) == home_id}
|
||||
if (
|
||||
group_id in mips_list
|
||||
and mips_list[group_id].get('did', None) in did_list
|
||||
):
|
||||
for device_source in ['home_list','share_home_list',
|
||||
'separated_shared_list']:
|
||||
for home_id, home_info in self._cc_home_info[
|
||||
'homes'][device_source].items():
|
||||
# i18n
|
||||
tip_central = self._miot_i18n.translate(
|
||||
key='config.other.found_central_gateway')
|
||||
home_info['central_did'] = mips_list[group_id].get(
|
||||
'did', None)
|
||||
home_list[home_id] = (
|
||||
f'{home_info["home_name"]} '
|
||||
f'[ {len(did_list)} {tip_devices} {tip_central} ]')
|
||||
tip_central = ''
|
||||
group_id = home_info.get('group_id', None)
|
||||
did_list = {
|
||||
device['did']: device for device in list(
|
||||
self._cc_home_info['devices'].values())
|
||||
if device.get('home_id', None) == home_id}
|
||||
if (
|
||||
group_id in mips_list
|
||||
and mips_list[group_id].get('did', None) in did_list
|
||||
):
|
||||
# i18n
|
||||
tip_central = self._miot_i18n.translate(
|
||||
key='config.other.found_central_gateway')
|
||||
home_info['central_did'] = mips_list[group_id].get(
|
||||
'did', None)
|
||||
home_list[home_id] = (
|
||||
f'{home_info["home_name"]} '
|
||||
f'[ {len(did_list)} {tip_devices} {tip_central} ]')
|
||||
# Remove deleted item
|
||||
self._home_selected_list = [
|
||||
home_id for home_id in self._home_selected_list
|
||||
@ -1460,10 +1467,12 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
||||
return await self.__show_homes_select_form('no_family_selected')
|
||||
self._ctrl_mode = user_input.get('ctrl_mode', self._ctrl_mode)
|
||||
self._home_selected = {}
|
||||
for home_id, home_info in self._cc_home_info[
|
||||
'homes']['home_list'].items():
|
||||
if home_id in self._home_selected_list:
|
||||
self._home_selected[home_id] = home_info
|
||||
for device_source in ['home_list','share_home_list',
|
||||
'separated_shared_list']:
|
||||
for home_id, home_info in self._cc_home_info[
|
||||
'homes'][device_source].items():
|
||||
if home_id in self._home_selected_list:
|
||||
self._home_selected[home_id] = home_info
|
||||
# Get device list
|
||||
device_list: dict = {
|
||||
did: dev_info
|
||||
|
@ -444,6 +444,17 @@ class MIoTHttpClient:
|
||||
|
||||
return home_list
|
||||
|
||||
async def get_separated_shared_devices_async(self) -> dict[str, dict]:
|
||||
separated_shared_devices: dict = {}
|
||||
device_list: dict[str, dict] = await self.__get_device_list_page_async(
|
||||
dids=[], start_did=None)
|
||||
for did, value in device_list.items():
|
||||
if value['owner'] is not None and ('userid' in value['owner']) and (
|
||||
'nickname' in value['owner']
|
||||
):
|
||||
separated_shared_devices.setdefault(did, value['owner'])
|
||||
return separated_shared_devices
|
||||
|
||||
async def get_homeinfos_async(self) -> dict:
|
||||
res_obj = await self.__mihome_api_post_async(
|
||||
url_path='/app/v2/homeroom/gethome',
|
||||
@ -499,19 +510,22 @@ class MIoTHttpClient:
|
||||
):
|
||||
more_list = await self.__get_dev_room_page_async(
|
||||
max_id=res_obj['result']['max_id'])
|
||||
for home_id, info in more_list.items():
|
||||
if home_id not in home_infos['homelist']:
|
||||
_LOGGER.info('unknown home, %s, %s', home_id, info)
|
||||
continue
|
||||
home_infos['homelist'][home_id]['dids'].extend(info['dids'])
|
||||
for room_id, info in info['room_info'].items():
|
||||
home_infos['homelist'][home_id]['room_info'].setdefault(
|
||||
room_id, {
|
||||
'room_id': room_id,
|
||||
'room_name': '',
|
||||
'dids': []})
|
||||
home_infos['homelist'][home_id]['room_info'][
|
||||
room_id]['dids'].extend(info['dids'])
|
||||
for device_source in ['homelist', 'share_home_list']:
|
||||
for home_id, info in more_list.items():
|
||||
if home_id not in home_infos[device_source]:
|
||||
_LOGGER.info('unknown home, %s, %s', home_id, info)
|
||||
continue
|
||||
home_infos[device_source][home_id]['dids'].extend(
|
||||
info['dids'])
|
||||
for room_id, info in info['room_info'].items():
|
||||
home_infos[device_source][home_id][
|
||||
'room_info'].setdefault(
|
||||
room_id, {
|
||||
'room_id': room_id,
|
||||
'room_name': '',
|
||||
'dids': []})
|
||||
home_infos[device_source][home_id]['room_info'][
|
||||
room_id]['dids'].extend(info['dids'])
|
||||
|
||||
return {
|
||||
'uid': uid,
|
||||
@ -651,6 +665,25 @@ class MIoTHttpClient:
|
||||
'room_name': room_name,
|
||||
'group_id': group_id
|
||||
} for did in room_info.get('dids', [])})
|
||||
separated_shared_devices: dict = (
|
||||
await self.get_separated_shared_devices_async())
|
||||
if separated_shared_devices:
|
||||
homes.setdefault('separated_shared_list', {})
|
||||
for did, owner in separated_shared_devices.items():
|
||||
owner_id = str(owner['userid'])
|
||||
homes['separated_shared_list'].setdefault(owner_id,{
|
||||
'home_name': owner['nickname'],
|
||||
'uid': owner_id,
|
||||
'group_id': 'NotSupport',
|
||||
'room_info': {'shared_device': 'shared_device'}
|
||||
})
|
||||
devices.update({did: {
|
||||
'home_id': owner_id,
|
||||
'home_name': owner['nickname'],
|
||||
'room_id': 'shared_device',
|
||||
'room_name': 'shared_device',
|
||||
'group_id': 'NotSupport'
|
||||
}})
|
||||
dids = sorted(list(devices.keys()))
|
||||
results = await self.get_devices_with_dids_async(dids=dids)
|
||||
if results is None:
|
||||
|
Loading…
x
Reference in New Issue
Block a user