jasonacox / tinytuya

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

DEBUG:ERROR Network Error: Unable to Connect - 901 - payload: null #499

Closed xyz-09 closed 1 month ago

xyz-09 commented 1 month ago

I have an issue, with debug mode i've got

It is WS2185 lightStrip with tuya :) controller

Protocol for conections is 3.5

I'v checked all crypto library: pyca/cryptography, PyCryptodomex, PyCrypto

DEBUG:Using pyca/cryptography 3.3.2 for crypto, GCM is supported
Traceback (most recent call last):
  File "***/.local/lib/python3.9/site-packages/tinytuya/core.py", line 1010, in _get_socket
    if self._negotiate_session_key():
  File "***/.local/lib/python3.9/site-packages/tinytuya/core.py", line 1435, in _negotiate_session_key
    rkey = self._send_receive_quick( self._negotiate_session_key_generate_step_1(), 2 )
  File "***/.local/lib/python3.9/site-packages/tinytuya/core.py", line 1119, in _send_receive_quick
    enc_payload = self._encode_message(payload) if type(payload) == MessagePayload else payload
  File "***/.local/lib/python3.9/site-packages/tinytuya/core.py", line 1530, in _encode_message
    data = pack_message(msg,hmac_key=self.local_key)
  File "***/.local/lib/python3.9/site-packages/tinytuya/core.py", line 484, in pack_message
    data2 = cipher.encrypt( raw, use_base64=False, pad=False, iv=True if not msg.iv else msg.iv, header=data[4:])
  File "***/.local/lib/python3.9/site-packages/tinytuya/core.py", line 295, in encrypt
    encryptor = Crypto( AES(self.key), Crypto_modes.GCM(iv) ).encryptor()
  File "/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/ciphers/algorithms.py", line 38, in __init__
    self.key = _verify_key_size(self, key)
  File "/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/ciphers/algorithms.py", line 21, in _verify_key_size
    raise ValueError(
ValueError: Invalid key size (136) for AES.
DEBUG:Using PyCryptodomex 3.9.7 for crypto, GCM is supported
Error: ValueError: Error 6 while instantiating the AES cipher
DEBUG:Using PyCryptodome 3.20.0 for crypto, GCM is supported
ValueError: Incorrect AES key length (17 bytes)
jasonacox commented 1 month ago

Hi @xyz-09 -

  1. Can you sare the link to the device? I don't find anything matching WS2185 (but I do see a WS2815).
  2. Can you share the code you are using or are you just doing a scan python -m tinytuya scan -d?
uzlonewolf commented 1 month ago

ValueError: Incorrect AES key length (17 bytes) ValueError: Invalid key size (136 [bits]) for AES. (136 is 17 bytes * 8 bits)

It looks like your device key is wrong. How did you obtain it? python -m tinytuya wizard? Looks like it could be a copy/paste error since it is 1 byte too long.

xyz-09 commented 1 month ago

Yes its WS2815 but its led strip type :smile: my mistake

The controller: info from tuya developer panel:

Output from : https://github.com/jasonacox/tinytuya/discussions/260

Payload: b'{"ip":"xxx.xxx.x.xxx","gwId":"","uuid":"","active":2,"ablilty":0,"encrypt":true,"productKey":"","version":"3.5","token":true,"wf_cfg":true,"clientLink":3}'

I run simple script from example

import tinytuya

d = tinytuya.OutletDevice(
    dev_id='DEVICE_ID_HERE',
    address='IP_ADDRESS_HERE',      # Or set to 'Auto' to auto-discover IP address
    local_key='LOCAL_KEY_HERE', 
    version=3.3)

data = d.status() 
print('set_status() result %r' % data)

d.turn_on()

d.turn_off()

I run python -m tinytuya scan and python -m tinytuya wizard

But both unsuccessful.

xyz-09 commented 1 month ago

ValueError: Incorrect AES key length (17 bytes) ValueError: Invalid key size (136 [bits]) for AES. (136 is 17 bytes * 8 bits)

It looks like your device key is wrong. How did you obtain it? python -m tinytuya wizard? Looks like it could be a copy/paste error since it is 1 byte too long.

There must have been something in it.

Today, I disconnected the controller from the app and from the power, then reconnected it to the app and to the power - thus obtaining a new key for the device. Additionally, I started a second controller Tuya, supporting protocol 3.3.

Once again, I ran python3 -m tinytuya wizard

Both devices were found without problem.

I copied the local_key again - this time not from the terminal, but from the snapshot.json file.

I managed to get the status of both devices, as well as turn them on and off - so it's a SUCCESS.

It's a pity I wasted the whole Saturday and Sunday trying to connect - without doing the basic test - turn it off and on, disconnect and reconnect the device again.

Your code is a great job :+1: You are amazing :+1: Thank you for taking the time to write and maintain your code. And Thank You for your help :1st_place_medal:

jasonacox commented 1 month ago

Thanks @xyz-09 ! I'll close this issue.