Skip to content

Commit dfd278f

Browse files
authored
Merge pull request #324 from uzlonewolf/wizard
Cloud device list and Content-Type update
2 parents d6d7b1b + c53a2d3 commit dfd278f

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

tinytuya/Cloud.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
########################################################
4343

4444
class Cloud(object):
45-
def __init__(self, apiRegion=None, apiKey=None, apiSecret=None, apiDeviceID=None, new_sign_algorithm=True, initial_token=None):
45+
def __init__(self, apiRegion=None, apiKey=None, apiSecret=None, apiDeviceID=None, new_sign_algorithm=True, initial_token=None, **extrakw):
4646
"""
4747
Tuya Cloud IoT Platform Access
4848
@@ -83,7 +83,7 @@ def __init__(self, apiRegion=None, apiKey=None, apiSecret=None, apiDeviceID=None
8383
self.error = None
8484
self.new_sign_algorithm = new_sign_algorithm
8585
self.server_time_offset = 0
86-
self.use_old_device_list = False
86+
self.use_old_device_list = True
8787

8888
if (not apiKey) or (not apiSecret):
8989
try:
@@ -127,7 +127,7 @@ def setregion(self, apiRegion=None):
127127
if self.apiRegion == "in":
128128
self.urlhost = "openapi.tuyain.com" # India Datacenter
129129

130-
def _tuyaplatform(self, uri, action='GET', post=None, ver='v1.0', recursive=False, query=None):
130+
def _tuyaplatform(self, uri, action='GET', post=None, ver='v1.0', recursive=False, query=None, content_type='application/json'):
131131
"""
132132
Handle GET and POST requests to Tuya Cloud
133133
"""
@@ -141,9 +141,10 @@ def _tuyaplatform(self, uri, action='GET', post=None, ver='v1.0', recursive=Fals
141141
headers = {}
142142
body = {}
143143
sign_url = url
144+
if content_type:
145+
headers['Content-type'] = content_type
144146
if post is not None:
145147
body = json.dumps(post)
146-
headers['Content-type'] = 'application/json'
147148
if action not in ('GET', 'POST', 'PUT', 'DELETE'):
148149
action = 'POST' if post else 'GET'
149150
if query:
@@ -198,6 +199,7 @@ def _tuyaplatform(self, uri, action='GET', post=None, ver='v1.0', recursive=Fals
198199
headers['sign'] = signature
199200
headers['t'] = str(now)
200201
headers['sign_method'] = 'HMAC-SHA256'
202+
headers['mode'] = 'cors'
201203

202204
if self.token is not None:
203205
headers['access_token'] = self.token
@@ -389,18 +391,42 @@ def getdevices(self, verbose=False):
389391
details.
390392
"""
391393
if self.apiDeviceID and self.use_old_device_list:
392-
uid = self._getuid(self.apiDeviceID)
393-
if uid is None:
394+
json_data = {}
395+
uid_list = {}
396+
397+
# apiDeviceID can be a comma-separated list, so process them all
398+
for dev_id in self.apiDeviceID.split(','):
399+
dev_id = dev_id.strip()
400+
if not dev_id:
401+
continue
402+
uid = self._getuid( dev_id )
403+
if not uid:
404+
# no user for this device?
405+
continue
406+
if isinstance( uid, dict ):
407+
# it's an error_json dict
408+
return uid
409+
else:
410+
uid_list[uid] = True
411+
412+
if not uid_list:
394413
return error_json(
395414
ERR_CLOUD,
396415
"Unable to get uid for device list"
397416
)
398-
elif isinstance( uid, dict):
399-
return uid
400417

401-
# Use UID to get list of all Devices for User
402-
uri = 'users/%s/devices' % uid
403-
json_data = self._tuyaplatform(uri)
418+
for uid in uid_list:
419+
# Use UID to get list of all Devices for User
420+
uri = 'users/%s/devices' % uid
421+
json_run = self._tuyaplatform(uri)
422+
# merge the dicts
423+
for k in json_run:
424+
if (k not in json_data) or (k != 'result'):
425+
# replace if key is not 'result'
426+
json_data[k] = json_run[k]
427+
else:
428+
# merge 'result' keys
429+
json_data[k] += json_run[k]
404430
else:
405431
json_data = self._get_all_devices()
406432
users = {}

0 commit comments

Comments
 (0)