Skip to content

Commit 4e63111

Browse files
authored
Merge pull request #296 from uzlonewolf/wizard
Tighten up parent device detection in the Wizard
2 parents 97d2d66 + 768e0f1 commit 4e63111

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

tinytuya/wizard.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,33 @@ def wizard(color=True, retries=None, forcescan=False, nocloud=False):
157157
# Filter to only Name, ID and Key, IP and mac-address
158158
tuyadevices = cloud.filter_devices( json_data['result'] )
159159

160+
# The device list does not tell us which device is the parent for a sub-device, so we need to try and figure it out
161+
# The only link between parent and child appears to be the local key
162+
# if 'parent' not in device: device is not a sub-device
163+
# if 'parent' in device: device is a sub-device
164+
# if device['parent'] == '': device is a sub-device with an unknown parent
165+
# else: device['parent'] == device_id of parent
160166
for dev in tuyadevices:
161-
if 'sub' in dev and dev['sub'] and 'key' in dev:
162-
found = False
163-
for parent in tuyadevices:
164-
# the local key seems to be the only way of identifying the parent device
165-
if 'key' in parent and 'id' in parent and dev['key'] == parent['key']:
166-
found = parent
167-
break
168-
if found:
169-
dev['parent'] = found['id']
167+
if 'sub' in dev and dev['sub']:
168+
if 'parent' not in dev:
169+
# Set 'parent' to an empty string in case we can't find it
170+
dev['parent'] = ''
171+
172+
# Only try to find the parent if the device has a local key
173+
if 'key' in dev and dev['key']:
174+
if 'id' not in dev:
175+
dev['id'] = ''
176+
found = False
177+
# Loop through all devices again to try and find a non-sub-device with the same local key
178+
for parent in tuyadevices:
179+
if 'id' not in parent or parent['id'] == dev['id']:
180+
continue
181+
# Check for matching local keys and if device is not a sub-device then assume we found the parent
182+
if 'key' in parent and parent['key'] and dev['key'] == parent['key'] and ( 'sub' not in parent or not parent['sub']):
183+
found = parent
184+
break
185+
if found:
186+
dev['parent'] = found['id']
170187

171188
# Display device list
172189
print("\n\n" + bold + "Device Listing\n" + dim)

0 commit comments

Comments
 (0)