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 = {}
|
home_list = {}
|
||||||
tip_devices = self._miot_i18n.translate(key='config.other.devices')
|
tip_devices = self._miot_i18n.translate(key='config.other.devices')
|
||||||
# home list
|
# home list
|
||||||
for home_id, home_info in self._cc_home_info[
|
for device_source in ['home_list','share_home_list',
|
||||||
'homes']['home_list'].items():
|
'separated_shared_list']:
|
||||||
# i18n
|
for home_id, home_info in self._cc_home_info[
|
||||||
tip_central = ''
|
'homes'][device_source].items():
|
||||||
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
|
# i18n
|
||||||
tip_central = self._miot_i18n.translate(
|
tip_central = ''
|
||||||
key='config.other.found_central_gateway')
|
group_id = home_info.get('group_id', None)
|
||||||
home_info['central_did'] = mips_list[group_id].get('did', None)
|
dev_list = {
|
||||||
home_list[home_id] = (
|
device['did']: device
|
||||||
f'{home_info["home_name"]} '
|
for device in list(self._cc_home_info['devices'].values())
|
||||||
f'[ {len(dev_list)} {tip_devices} {tip_central} ]')
|
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()))
|
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:
|
if not home_selected:
|
||||||
return await self.__show_homes_select_form(
|
return await self.__show_homes_select_form(
|
||||||
'no_family_selected')
|
'no_family_selected')
|
||||||
for home_id, home_info in self._cc_home_info[
|
for device_source in ['home_list','share_home_list',
|
||||||
'homes']['home_list'].items():
|
'separated_shared_list']:
|
||||||
if home_id in home_selected:
|
for home_id, home_info in self._cc_home_info[
|
||||||
self._home_selected[home_id] = 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(
|
self._area_name_rule = user_input.get(
|
||||||
'area_name_rule', self._area_name_rule)
|
'area_name_rule', self._area_name_rule)
|
||||||
# Storage device list
|
# Storage device list
|
||||||
@ -1420,27 +1425,29 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
home_list = {}
|
home_list = {}
|
||||||
tip_devices = self._miot_i18n.translate(key='config.other.devices')
|
tip_devices = self._miot_i18n.translate(key='config.other.devices')
|
||||||
# home list
|
# home list
|
||||||
for home_id, home_info in self._cc_home_info[
|
for device_source in ['home_list','share_home_list',
|
||||||
'homes']['home_list'].items():
|
'separated_shared_list']:
|
||||||
# i18n
|
for home_id, home_info in self._cc_home_info[
|
||||||
tip_central = ''
|
'homes'][device_source].items():
|
||||||
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
|
# i18n
|
||||||
tip_central = self._miot_i18n.translate(
|
tip_central = ''
|
||||||
key='config.other.found_central_gateway')
|
group_id = home_info.get('group_id', None)
|
||||||
home_info['central_did'] = mips_list[group_id].get(
|
did_list = {
|
||||||
'did', None)
|
device['did']: device for device in list(
|
||||||
home_list[home_id] = (
|
self._cc_home_info['devices'].values())
|
||||||
f'{home_info["home_name"]} '
|
if device.get('home_id', None) == home_id}
|
||||||
f'[ {len(did_list)} {tip_devices} {tip_central} ]')
|
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
|
# Remove deleted item
|
||||||
self._home_selected_list = [
|
self._home_selected_list = [
|
||||||
home_id for home_id in 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')
|
return await self.__show_homes_select_form('no_family_selected')
|
||||||
self._ctrl_mode = user_input.get('ctrl_mode', self._ctrl_mode)
|
self._ctrl_mode = user_input.get('ctrl_mode', self._ctrl_mode)
|
||||||
self._home_selected = {}
|
self._home_selected = {}
|
||||||
for home_id, home_info in self._cc_home_info[
|
for device_source in ['home_list','share_home_list',
|
||||||
'homes']['home_list'].items():
|
'separated_shared_list']:
|
||||||
if home_id in self._home_selected_list:
|
for home_id, home_info in self._cc_home_info[
|
||||||
self._home_selected[home_id] = home_info
|
'homes'][device_source].items():
|
||||||
|
if home_id in self._home_selected_list:
|
||||||
|
self._home_selected[home_id] = home_info
|
||||||
# Get device list
|
# Get device list
|
||||||
device_list: dict = {
|
device_list: dict = {
|
||||||
did: dev_info
|
did: dev_info
|
||||||
|
@ -444,6 +444,17 @@ class MIoTHttpClient:
|
|||||||
|
|
||||||
return home_list
|
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:
|
async def get_homeinfos_async(self) -> dict:
|
||||||
res_obj = await self.__mihome_api_post_async(
|
res_obj = await self.__mihome_api_post_async(
|
||||||
url_path='/app/v2/homeroom/gethome',
|
url_path='/app/v2/homeroom/gethome',
|
||||||
@ -499,19 +510,22 @@ class MIoTHttpClient:
|
|||||||
):
|
):
|
||||||
more_list = await self.__get_dev_room_page_async(
|
more_list = await self.__get_dev_room_page_async(
|
||||||
max_id=res_obj['result']['max_id'])
|
max_id=res_obj['result']['max_id'])
|
||||||
for home_id, info in more_list.items():
|
for device_source in ['homelist', 'share_home_list']:
|
||||||
if home_id not in home_infos['homelist']:
|
for home_id, info in more_list.items():
|
||||||
_LOGGER.info('unknown home, %s, %s', home_id, info)
|
if home_id not in home_infos[device_source]:
|
||||||
continue
|
_LOGGER.info('unknown home, %s, %s', home_id, info)
|
||||||
home_infos['homelist'][home_id]['dids'].extend(info['dids'])
|
continue
|
||||||
for room_id, info in info['room_info'].items():
|
home_infos[device_source][home_id]['dids'].extend(
|
||||||
home_infos['homelist'][home_id]['room_info'].setdefault(
|
info['dids'])
|
||||||
room_id, {
|
for room_id, info in info['room_info'].items():
|
||||||
'room_id': room_id,
|
home_infos[device_source][home_id][
|
||||||
'room_name': '',
|
'room_info'].setdefault(
|
||||||
'dids': []})
|
room_id, {
|
||||||
home_infos['homelist'][home_id]['room_info'][
|
'room_id': room_id,
|
||||||
room_id]['dids'].extend(info['dids'])
|
'room_name': '',
|
||||||
|
'dids': []})
|
||||||
|
home_infos[device_source][home_id]['room_info'][
|
||||||
|
room_id]['dids'].extend(info['dids'])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'uid': uid,
|
'uid': uid,
|
||||||
@ -651,6 +665,25 @@ class MIoTHttpClient:
|
|||||||
'room_name': room_name,
|
'room_name': room_name,
|
||||||
'group_id': group_id
|
'group_id': group_id
|
||||||
} for did in room_info.get('dids', [])})
|
} 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()))
|
dids = sorted(list(devices.keys()))
|
||||||
results = await self.get_devices_with_dids_async(dids=dids)
|
results = await self.get_devices_with_dids_async(dids=dids)
|
||||||
if results is None:
|
if results is None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user