tinytuya icon indicating copy to clipboard operation
tinytuya copied to clipboard

Python Error when running wizard

Open Brunius opened this issue 1 year ago • 10 comments

Hi all,

I'm trying to do first-time setup, following the instructions here. I'm up to step 4.

I have entered the API Key, Secret, and a device ID, copy and pasted from the Tuya website, and confirmed through scan.

I encounter the following error:

$ python -m tinytuya wizard
TinyTuya Setup Wizard [1.12.11]

    Existing settings:
        API Key=*******************************
        Secret=*********************************
        DeviceID=******************************
        Region=eu

    Use existing credentials (Y/n): y
Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\gruno\AppData\Roaming\Python\Python310\site-packages\tinytuya\__main__.py", line 76, in <module>
    wizard.wizard(color=color, retries=retries, forcescan=force, quicklist=assume_yes)
  File "C:\Users\gruno\AppData\Roaming\Python\Python310\site-packages\tinytuya\wizard.py", line 157, in wizard
    cloud = tinytuya.Cloud( **config )
  File "C:\Users\gruno\AppData\Roaming\Python\Python310\site-packages\tinytuya\Cloud.py", line 117, in __init__
    self._gettoken()
  File "C:\Users\gruno\AppData\Roaming\Python\Python310\site-packages\tinytuya\Cloud.py", line 266, in _gettoken
    "Cloud _gettoken() failed: %r" % response_dict['msg'],
KeyError: 'msg'

I have found the following issues which seem to have a similar problem:

Python ERROR when trying to run 'wizzard' #20 Error when using 'wizzard' #19

The resolution for these issues seemed to be to ensure that the correct permissions are enabled in the Tuya IoT platform. As far as I can tell, I have the correct permissions: image

Are there further permissions I should be requesting? Have I missed a step?

Any advice you can provide would be much appreciated.

Brunius avatar Sep 21 '23 04:09 Brunius

Hmm, those all blow up much later in the process, you should't be encountering errors in _gettoken(). What does this result in:

import tinytuya

c = tinytuya.Cloud(initial_token=True)
c.token = None
print(c._tuyaplatform('token?grant_type=1'))

uzlonewolf avatar Sep 21 '23 05:09 uzlonewolf

{'Error': 'Missing Tuya Cloud Key and Secret', 'Err': '909', 'Payload': "Cloud _tuyaplatform() invalid response: b'400 Bad Request'"}

Should that be pulling from the same saved credentials that wizard offers to use? Or is there an assumption that I have provided something?

Brunius avatar Sep 21 '23 07:09 Brunius

Yes, it should automatically pull them from tinytuya.json (same as the wizard) when not provided. You can also pass them in explicitly if needed:

import tinytuya

c = tinytuya.Cloud(apiRegion="eu", apiKey="...", apiSecret="...", apiDeviceID"...", initial_token=True)
c.token = None
print(c._tuyaplatform('token?grant_type=1'))

uzlonewolf avatar Sep 21 '23 07:09 uzlonewolf

Got a good result from this one -

{
	'result': {
		'access_token': '18d829aaec28a84d1c4d4f01cc66****',
		'expire_time': 7200,
		'refresh_token': 'b1095c5c447a3ad82020b70f0acf****',
		'uid': 'bay1695268924091****'
	},
	'success': True,
	't': 1695283119032,
	'tid': 'ac86a75c585411ee9a70760d0d3c****'
}

Brunius avatar Sep 21 '23 08:09 Brunius

Hmm, it should work then. If you open tinytuya.json in a text editor, is there any whitespace or funny characters inside the quotes?

uzlonewolf avatar Sep 21 '23 08:09 uzlonewolf

@jasonacox it looks like https://github.com/jasonacox/tinytuya/blob/master/tinytuya/Cloud.py#L266 should be updated to handle the cases where _tuyaplatform() returned None or an error_json() (via response_dict). I'll see if I can do something about that tomorrow if I have time. Edit: we should probably also strip whitespace/unicode or throw an error if any are found in the loaded/passed values.

uzlonewolf avatar Sep 21 '23 08:09 uzlonewolf

Hmm, it should work then. If you open tinytuya.json in a text editor, is there any whitespace or funny characters inside the quotes?

Some unicode characters:

For example, device id: "apiDeviceID": "\u001b[F\u001b[200~32624843c44f33ef51ac\u001b[201~" Should probably be :"apiDeviceID": "32624843c44f33ef51ac"

I would guess that this is due to my cut + pasting and not having the whitespace trimmed from the end?

Brunius avatar Sep 21 '23 08:09 Brunius

I've just edited tinytuya.json to remove the unicode - it all seems to work just as expected.

Brunius avatar Sep 21 '23 08:09 Brunius

Yep, that would do it. Fix that and you should be good to go. I'll add some sanity checking of the passed values tomorrow while I'm in there.

uzlonewolf avatar Sep 21 '23 08:09 uzlonewolf

Thanks @uzlonewolf - I've been traveling quite a bit so please feel free to submit the update or I'll get to it later this weekend. 🙏

jasonacox avatar Sep 23 '23 05:09 jasonacox