Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 85 additions & 55 deletions custom_components/xiaomi_miot/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,65 +205,83 @@ async def get_cloud_filter_schema(self, user_input, errors, schema=None, via_did
if not dvs:
errors['base'] = 'none_devices'
else:
grp = {}
vls = {}
homes = {}
fls = ['did'] if via_did else ['model', 'home_id', 'ssid', 'bssid']
for f in fls:
for d in dvs:
v = d.get(f)
if v is None:
continue
if home_id := d.get('home_id', 0):
homes.setdefault(home_id, d.get('home_name') or 'Default Home')
if f in ['did'] and v in user_input.get(f'{f}_list', []):
pass
elif home_ids and home_id not in home_ids:
continue
grp.setdefault(v, 0)
grp[v] += 1
vls.setdefault(f, {})
dnm = f'{d.get("name")}'
des = '<empty>' if v == '' else v
if f == 'home_id':
des = d.get('home_name') or des
if f in ['did']:
if MiotCloud.is_hide(d):
continue
dip = d.get('localip')
if not dip or d.get('pid') not in [0, '0', '8', '', None]:
dip = d.get('model')
vls[f][v] = f'{dnm} ({dip})'
elif f in ['model']:
if grp[v] > 1:
dnm += f' * {grp[v]}'
vls[f][v] = f'{des} ({dnm})'
else:
vls[f][v] = f'{des} ({grp[v]})'
ies = {
'exclude': 'Exclude (排除)',
'include': 'Include (包含)',
'exclude': '排除 Exclude(勾选 = 不接入这些设备)',
'include': '包含 Include(只接入勾选的设备)',
}
for f in fls:
if not vls.get(f):
continue
fk = f'filter_{f}'
fl = f'{f}_list'
lst = vls.get(f, {})
lst = dict(sorted(lst.items()))
ols = [
v
for v in user_input.get(fl, [])
if v in lst
]
schema = schema.extend({
vol.Required(fk, default=user_input.get(fk, 'exclude')): vol.In(ies),
vol.Optional(fl, default=ols): cv.multi_select(lst),
})
if via_did and homes:
if via_did:
# 按家庭分组的设备选择:每个家庭一个独立多选框,一目了然
homes = {}
for d in dvs:
did = d.get('did')
if not did or MiotCloud.is_hide(d):
continue
if home_ids and d.get('home_id') not in home_ids:
continue
hname = d.get('home_name') or 'unassigned'
dip = d.get('localip')
if not dip or d.get('pid') not in [0, '0', '8', '', None]:
dip = d.get('model')
homes.setdefault(hname, {})[did] = f'{d.get("name")} ({dip})'
schema = schema.extend({
vol.Optional('home_ids', default=[]): cv.multi_select(homes),
vol.Required('filter_did', default=user_input.get('filter_did', 'exclude')): vol.In(ies),
})
prev = user_input.get('did_list') or []
names = sorted([h for h in homes if h != 'unassigned'])
if 'unassigned' in homes:
names.append('unassigned')
for hname in names:
lst = dict(sorted(homes[hname].items()))
key = f'home__{hname}'
ols = [
v
for v in (user_input.get(key) or []) + prev
if v in lst
]
schema = schema.extend({
vol.Optional(key, default=ols): cv.multi_select(lst),
})
else:
grp = {}
vls = {}
fls = ['model', 'home_id', 'ssid', 'bssid']
for f in fls:
for d in dvs:
v = d.get(f)
if v is None:
continue
if home_id := d.get('home_id', 0):
if home_ids and home_id not in home_ids:
continue
grp.setdefault(v, 0)
grp[v] += 1
vls.setdefault(f, {})
dnm = f'{d.get("name")}'
des = '<empty>' if v == '' else v
if f == 'home_id':
des = d.get('home_name') or des
if f in ['model']:
if grp[v] > 1:
dnm += f' * {grp[v]}'
vls[f][v] = f'{des} ({dnm})'
else:
vls[f][v] = f'{des} ({grp[v]})'
for f in fls:
if not vls.get(f):
continue
fk = f'filter_{f}'
fl = f'{f}_list'
lst = vls.get(f, {})
lst = dict(sorted(lst.items()))
ols = [
v
for v in user_input.get(fl, [])
if v in lst
]
schema = schema.extend({
vol.Required(fk, default=user_input.get(fk, 'exclude')): vol.In(ies),
vol.Optional(fl, default=ols): cv.multi_select(lst),
})
tip = ''
if user_input.get(CONF_CONN_MODE) == 'local':
url = 'https://github.com/al-one/hass-xiaomi-miot/issues/100#issuecomment-855183156'
Expand Down Expand Up @@ -409,6 +427,12 @@ async def async_step_cloud_filter(self, user_input=None):
elif user_input:
if not self.config_data:
self.config_data = {}
did_list = []
for k in list(user_input.keys()):
if k.startswith('home__'):
did_list += user_input.pop(k) or []
if did_list:
user_input['did_list'] = did_list
self.config_data.update({
**(self.cloud.to_config() or {}),
**user_input,
Expand Down Expand Up @@ -1121,6 +1145,12 @@ async def async_step_cloud_filter(self, user_input=None):
}
schema = await self.get_cloud_filter_schema(user_input, errors, schema, via_did=via_did, home_ids=home_ids)
elif user_input:
did_list = []
for k in list(user_input.keys()):
if k.startswith('home__'):
did_list += user_input.pop(k) or []
if did_list:
user_input['did_list'] = did_list
self.config_data.update({
**(self.cloud.to_config() or {}),
'filter_models': self.filter_models,
Expand Down
14 changes: 7 additions & 7 deletions custom_components/xiaomi_miot/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"cloud_filter": {
"title": "Filter cloud devices",
"description": "You can include or exclude devices to integrate only the devices you want.\n{tip}",
"description": "Select only the devices you want to integrate.\nExclude mode: checked devices are left out (checking none integrates all devices); Include mode: only checked devices are integrated. To integrate gateway sub-devices, select the sub-devices directly — integrating the gateway does not auto-integrate them.\n{tip}",
"data": {
"filter_model": "Filter Device Model",
"model_list": "Device Model List",
Expand All @@ -44,10 +44,10 @@
"bssid_list": "WiFi BSSID List",
"filter_did": "Filter Device",
"did_list": "Device List",
"home_ids": "Filter Home (Leave blank to save)"
"home_ids": "Filter Home (Leave blank to save)",
"home__unassigned": "Unassigned devices (nearby/offline)"
},
"data_description": {
"filter_did": "In the exclude mode, if you do not check any option in the list, it is equivalent to including all devices.",
"home_ids": "After checking this option and submitting, you will return to the current page,\nand the device list will refresh and filter out the devices of the checked homes."
}
},
Expand Down Expand Up @@ -199,7 +199,7 @@
},
"cloud_filter": {
"title": "Filter cloud devices",
"description": "You can include or exclude devices to integrate only the devices you want.\n{tip}",
"description": "Select only the devices you want to integrate.\nExclude mode: checked devices are left out (checking none integrates all devices); Include mode: only checked devices are integrated. To integrate gateway sub-devices, select the sub-devices directly — integrating the gateway does not auto-integrate them.\n{tip}",
"data": {
"filter_model": "Filter Device Model",
"model_list": "Device Model List",
Expand All @@ -211,10 +211,10 @@
"bssid_list": "WiFi BSSID List",
"filter_did": "Filter Device",
"did_list": "Device List",
"home_ids": "Filter Home (Leave blank to save)"
"home_ids": "Filter Home (Leave blank to save)",
"home__unassigned": "Unassigned devices (nearby/offline)"
},
"data_description": {
"filter_did": "In the exclude mode, if you do not check any option in the list, it is equivalent to including all devices.",
"home_ids": "After checking this option and submitting, you will return to the current page,\nand the device list will refresh and filter out the devices of the checked homes."
}
}
Expand Down Expand Up @@ -604,4 +604,4 @@
}
}
}
}
}
14 changes: 7 additions & 7 deletions custom_components/xiaomi_miot/translations/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"cloud_filter": {
"title": "筛选设备",
"description": "你可以通过筛选只接入想要的设备。如果想集成网关子设备,请直接选择子设备,接入网关不会自动集成子设备。\n{tip}",
"description": "你可以通过筛选只接入想要的设备。\n排除模式:勾选 = 不接入(一个都不勾 = 接入全部设备);包含模式:勾选 = 接入。想集成网关子设备请直接选择子设备,接入网关不会自动集成子设备。\n{tip}",
"data": {
"filter_model": "筛选设备型号",
"model_list": "设备型号列表",
Expand All @@ -44,10 +44,10 @@
"bssid_list": "WiFi BSSID 列表",
"filter_did": "筛选设备",
"did_list": "设备列表",
"home_ids": "筛选家庭 (留空保存)"
"home_ids": "筛选家庭 (留空保存)",
"home__unassigned": "未分配家庭的设备(附近的/离线的)"
},
"data_description": {
"filter_did": "在排除模式中,如果不勾选任何设备,则相当于接入所有设备",
"home_ids": "勾选此项后提交,会回到在当前页面,设备列表会刷新并筛选出已勾选家庭的设备"
}
},
Expand Down Expand Up @@ -141,7 +141,7 @@
},
"cloud_filter": {
"title": "筛选设备",
"description": "你可以通过筛选只接入想要的设备。如果想集成网关子设备,请直接选择子设备,接入网关不会自动集成子设备。\n{tip}",
"description": "你可以通过筛选只接入想要的设备。\n排除模式:勾选 = 不接入(一个都不勾 = 接入全部设备);包含模式:勾选 = 接入。想集成网关子设备请直接选择子设备,接入网关不会自动集成子设备。\n{tip}",
"data": {
"filter_model": "筛选设备型号",
"model_list": "设备型号列表",
Expand All @@ -153,10 +153,10 @@
"bssid_list": "WiFi BSSID 列表",
"filter_did": "筛选设备",
"did_list": "设备列表",
"home_ids": "筛选家庭 (留空保存)"
"home_ids": "筛选家庭 (留空保存)",
"home__unassigned": "未分配家庭的设备(附近的/离线的)"
},
"data_description": {
"filter_did": "在排除模式中,如果不勾选任何设备,则相当于接入所有设备",
"home_ids": "勾选此项后提交,会回到在当前页面,设备列表会刷新并筛选出已勾选家庭的设备"
}
}
Expand Down Expand Up @@ -606,4 +606,4 @@
}
}
}
}
}
Binary file added docs/pr-images/new-filter-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/pr-images/old-filter-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading