jasonacox / tinytuya

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

"Network Error: Unable to Connect" with Smart Outdoor Socket #297

Closed vfdev-5 closed 1 year ago

vfdev-5 commented 1 year ago

Hi,

First of all, thanks for the project. I'm discovering it and I'm trying to connect to my smart outdoor socket (made by greensun but powered by tuya).

When I scan for devices: python -m tinytuya scan, I can discover the socket but "local_key" is empty:


TinyTuya (Tuya device scanner) [1.11.0]

Scanning on UDP ports 6666 and 6667 and 7000 for devices for 18 seconds...

Unknown v3.3 Device                                   Product ID = keym9qkuywghyrvs  [Valid Broadcast]:
    Address = 192.168.1.60   Device ID = bf*******op (len:22)  Local Key =   Version = 3.3  Type = default, MAC = 
    No Stats for 192.168.1.60: DEVICE KEY required to poll for status
Scan completed in 18.0458 seconds                  

Scan Complete!  Found 1 devices.
Broadcasted: 1
Versions: 3.3: 1
Unknown Devices: 1

>> Saving device snapshot data to snapshot.json

When I try to connect and get status, there is an error is reported:

device_id = "bf*******op"
ip_address = "192.168.1.60"
local_key = ""
version = "3.3"

tinytuya.set_debug(True)

d = tinytuya.OutletDevice(device_id, ip_address, local_key, version=version)
data = d.status() 
print("Device status: ", data)

Output:

DEBUG:sending payload
DEBUG:Network connection error in _send_receive() - retry 6/5
Traceback (most recent call last):
  File "/opt/conda/lib/python3.9/site-packages/tinytuya/core.py", line 951, in _send_receive
    enc_payload = self._encode_message(payload) if type(payload) == MessagePayload else payload
  File "/opt/conda/lib/python3.9/site-packages/tinytuya/core.py", line 1301, in _encode_message
    payload = self.cipher.encrypt(payload, False)
  File "/opt/conda/lib/python3.9/site-packages/tinytuya/core.py", line 244, in encrypt
    cipher = AES.new(self.key, mode=AES.MODE_ECB)
  File "/opt/conda/lib/python3.9/site-packages/Crypto/Cipher/AES.py", line 228, in new
    return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
  File "/opt/conda/lib/python3.9/site-packages/Crypto/Cipher/__init__.py", line 79, in _create_cipher
    return modes[mode](factory, **kwargs)
  File "/opt/conda/lib/python3.9/site-packages/Crypto/Cipher/_mode_ecb.py", line 216, in _create_ecb_cipher
    cipher_state = factory._create_base_cipher(kwargs)
  File "/opt/conda/lib/python3.9/site-packages/Crypto/Cipher/AES.py", line 90, in _create_base_cipher
    raise ValueError("Incorrect AES key length (%d bytes)" % len(key))
ValueError: Incorrect AES key length (0 bytes)
DEBUG:Exceeded tinytuya retry limit (5)
DEBUG:Unable to connect to device 
DEBUG:ERROR Network Error: Unable to Connect - 901 - payload: null
DEBUG:status() received data={'Error': 'Network Error: Unable to Connect', 'Err': '901', 'Payload': None}

Device status:  {'Error': 'Network Error: Unable to Connect', 'Err': '901', 'Payload': None}

Any hints how to fix this issue ? Thanks

vfdev-5 commented 1 year ago

I solved this issue by fetching local_key using python3 -m tinytuya wizard and creating cloud account etc... (I was hoping that it was not necessary)

jasonacox commented 1 year ago

Thanks for opening this @vfdev-5

Yes, unfortunately that is the necessary step to decrypt the payloads. Do you have any suggestions on how we could improve the README directions? We may not have made that clear.

vfdev-5 commented 1 year ago

I think the README is rather clear if I reread it right now. Maybe, at the first time of reading, I had a question about the sentence "Local_Key - The security key required to access the Tuya device." from Tuya Device Preparation section, like how can we get it. Then I saw scan and I thought that it could be enough...

Probably a nit update could be:

- Local_Key - The security key required to access the Tuya device.
+ Local_Key - The security key required to access the Tuya device. To obtain it the first time please follow the [next steps](https://github.com/jasonacox/tinytuya#setup-wizard---getting-local-keys) (there is no other way of getting it).

and

- Setup Wizard - Getting Local Keys 
+ Setup Wizard - Getting Local Keys (Required)

Otherwise, reading the README carefully helps as well :)