jasonacox / tinytuya

Python API for Tuya WiFi smart devices using a direct local area network (LAN) connection or the cloud (TuyaCloud API).
MIT License
868 stars 157 forks source link

Python Error when running wizard #409

Open Brunius opened 9 months ago

Brunius commented 9 months ago

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.

uzlonewolf commented 9 months ago

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'))
Brunius commented 9 months ago
{'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?

uzlonewolf commented 9 months ago

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'))
Brunius commented 9 months ago

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****'
}
uzlonewolf commented 9 months ago

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 commented 9 months ago

@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.

Brunius commented 9 months ago

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 commented 9 months ago

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

uzlonewolf commented 9 months ago

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.

jasonacox commented 9 months ago

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. 🙏