jasonacox / tinytuya

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

Cannot connect to local device #543

Open wallforfry opened 1 week ago

wallforfry commented 1 week ago

Hi !

I'm trying to connect my Avidsen Homefresh AC but it's never found by the scan command. I know that it's a v3.3 Tuya API, I've got the right key and deviceId.

I tried to run a custom script calling the find_device function. This gave me some results but idk what to do know. Is this encrypted payload normal ?

DEBUG:Force-Scan Found Device 192.168.1.13
DEBUG:payload encrypted=b'000055aa000000010000000a00000008354862b10000aa55'
DEBUG:Force-Scan Found Device 192.168.1.13
DEBUG:payload encrypted=b'000055aa000000010000000a00000028deadbeef112233445566778899aabbccddeeffb00bface112233feedbabe74f0149a707c0000aa55'
DEBUG:Force-Scan Found Device 192.168.1.13
DEBUG:payload encrypted=b'000055aa000000010000000a00000028deadbeef112233445566778899aabbccddeeffb00bface112233feedbabe74f0149a707c0000aa55'
DEBUG:Force-Scan Found Device 192.168.1.13
DEBUG:payload encrypted=b'000055aa000000010000000d000000088768bea10000aa55'
DEBUG:Force-Scan Found Device 192.168.1.13
DEBUG:payload encrypted=b'000055aa000000010000000d00000028deadbeef112233445566778899aabbccddeeffb00bface112233feedbabe74f020a819a30000aa55'
DEBUG:Scan complete with 2 devices found
DEBUG:find() is returning: {'ip': None, 'version': None, 'id': None, 'product_id': None, 'data': {}}

I tried with a status() call too, but it always ends with socket unable to connect

Also :

jasonacox commented 1 week ago

Hi @wallforfry , I would try:

import tinytuya

# Turn on debug
tinytuya.set_debug(True)

# Connect to Device
d = tinytuya.OutletDevice(
    dev_id='DEVICE_ID_HERE',
    address='192.168.1.13',    
    local_key='LOCAL_KEY_HERE', 
    version=3.3)

# Get Status
data = d.status() 
print('set_status() result %r' % data)
wallforfry commented 1 week ago

Thank's for your fast answer, just tried and got :

DEBUG:TinyTuya [1.15.1]

DEBUG:Python 3.13.0 (main, Oct  7 2024, 05:02:14) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
DEBUG:Using pyca/cryptography 43.0.1 for crypto, GCM is supported
DEBUG:status() entry (dev_type is default)
DEBUG:final payload_dict for '40330123ecfabcbec3b8' ('v3.3'/'default'): {1: {'command': {'gwId': '', 'devId': '', 'uid': '', 't': ''}}, 7: {'command': {'devId': '', 'uid': '', 't': ''}}, 8: {'command': {'gwId': '', 'devId': ''}}, 9: {'command': {'gwId': '', 'devId': ''}}, 10: {'command': {'gwId': '', 'devId': '', 'uid': '', 't': ''}}, 13: {'command': {'devId': '', 'uid': '', 't': ''}}, 16: {'command': {'devId': '', 'uid': '', 't': ''}}, 18: {'command': {'dpId': [18, 19, 20]}}, 64: {'command': {'reqType': '', 'data': {}}}}
DEBUG:building command 10 payload=b'{"gwId":"40330123ecfabcbec3b8","devId":"40330123ecfabcbec3b8","uid":"40330123ecfabcbec3b8","t":"1728632161"}'
DEBUG:sending payload
DEBUG:payload encrypted=b'000055aa000000010000000a00000078c4662892a437a0f9b3a11bbf78afdb47421335f00a9dbd1fb382b4cfd3e44377d578f25ac81a352a70ef380c322ee7932a519fef2734c5f3b2f5fe74845ee53d693f3dbe2ab05d42bb72c36668f8e325b330da25a76ddab7eed7f4a2c2ca1e5c57fa26e2643888d1d502be4001871511f1bbec5e0000aa55'
DEBUG:Network connection error in _send_receive() - retry 1/5
Traceback (most recent call last):
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1144, in _send_receive
    self.socket.sendall(enc_payload)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
BrokenPipeError: [Errno 32] Broken pipe
DEBUG:sending payload
DEBUG:payload encrypted=b'000055aa000000020000000a00000078c4662892a437a0f9b3a11bbf78afdb47421335f00a9dbd1fb382b4cfd3e44377d578f25ac81a352a70ef380c322ee7932a519fef2734c5f3b2f5fe74845ee53d693f3dbe2ab05d42bb72c36668f8e325b330da25a76ddab7eed7f4a2c2ca1e5c57fa26e2643888d1d502be40018715111fddef470000aa55'
DEBUG:Network connection error in _send_receive() - retry 2/5
Traceback (most recent call last):
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1149, in _send_receive
    rmsg = self._receive()
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1037, in _receive
    data = self._recv_all( min_len )
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1011, in _recv_all
    newdata = self.socket.recv(length)
ConnectionResetError: [Errno 54] Connection reset by peer
DEBUG:sending payload
DEBUG:payload encrypted=b'000055aa000000030000000a00000078c4662892a437a0f9b3a11bbf78afdb47421335f00a9dbd1fb382b4cfd3e44377d578f25ac81a352a70ef380c322ee7932a519fef2734c5f3b2f5fe74845ee53d693f3dbe2ab05d42bb72c36668f8e325b330da25a76ddab7eed7f4a2c2ca1e5c57fa26e2643888d1d502be4001871511f32f138f0000aa55'
DEBUG:Network connection error in _send_receive() - retry 3/5
Traceback (most recent call last):
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1149, in _send_receive
    rmsg = self._receive()
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1037, in _receive
    data = self._recv_all( min_len )
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1011, in _recv_all
    newdata = self.socket.recv(length)
ConnectionResetError: [Errno 54] Connection reset by peer
DEBUG:sending payload
DEBUG:payload encrypted=b'000055aa000000040000000a00000078c4662892a437a0f9b3a11bbf78afdb47421335f00a9dbd1fb382b4cfd3e44377d578f25ac81a352a70ef380c322ee7932a519fef2734c5f3b2f5fe74845ee53d693f3dbe2ab05d42bb72c36668f8e325b330da25a76ddab7eed7f4a2c2ca1e5c57fa26e2643888d1d502be40018715111860ef340000aa55'
DEBUG:Network connection error in _send_receive() - retry 4/5
Traceback (most recent call last):
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1149, in _send_receive
    rmsg = self._receive()
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1037, in _receive
    data = self._recv_all( min_len )
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1011, in _recv_all
    newdata = self.socket.recv(length)
ConnectionResetError: [Errno 54] Connection reset by peer
DEBUG:sending payload
DEBUG:payload encrypted=b'000055aa000000050000000a00000078c4662892a437a0f9b3a11bbf78afdb47421335f00a9dbd1fb382b4cfd3e44377d578f25ac81a352a70ef380c322ee7932a519fef2734c5f3b2f5fe74845ee53d693f3dbe2ab05d42bb72c36668f8e325b330da25a76ddab7eed7f4a2c2ca1e5c57fa26e2643888d1d502be4001871511f49213fc0000aa55'
DEBUG:Network connection error in _send_receive() - retry 5/5
Traceback (most recent call last):
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1149, in _send_receive
    rmsg = self._receive()
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1037, in _receive
    data = self._recv_all( min_len )
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1011, in _recv_all
    newdata = self.socket.recv(length)
ConnectionResetError: [Errno 54] Connection reset by peer
DEBUG:sending payload
DEBUG:payload encrypted=b'000055aa000000060000000a00000078c4662892a437a0f9b3a11bbf78afdb47421335f00a9dbd1fb382b4cfd3e44377d578f25ac81a352a70ef380c322ee7932a519fef2734c5f3b2f5fe74845ee53d693f3dbe2ab05d42bb72c36668f8e325b330da25a76ddab7eed7f4a2c2ca1e5c57fa26e2643888d1d502be40018715111af410e50000aa55'
DEBUG:Network connection error in _send_receive() - retry 6/5
Traceback (most recent call last):
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1149, in _send_receive
    rmsg = self._receive()
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1037, in _receive
    data = self._recv_all( min_len )
  File "/private/tmp/venv/lib/python3.13/site-packages/tinytuya/core.py", line 1011, in _recv_all
    newdata = self.socket.recv(length)
ConnectionResetError: [Errno 54] Connection reset by peer
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}
set_status() result {'Error': 'Network Error: Unable to Connect', 'Err': '901', 'Payload': None}

Side note: when I used localtuya before, I had to put dp 1 in the reset field:

Setting the 'DPIDs to send in RESET command' is optional. It is used when a device doesn't respond to any Tuya commands after a power cycle, but can be connected to (zombie state). This scenario mostly occurs when the device is blocked from accessing the internet. The DPids will vary between devices, but typically "18,19,20" is used. If the wrong entries are added here, then the device may not come out of the zombie state. Typically only sensor DPIDs entered here.

But I think that your "default" dev_type already handle that

uzlonewolf commented 1 week ago

How do you know it's a v3.3 device? That error's looking like it's either a different version or there is already a connection to it open from another program. A lot of devices stop broadcasting discovery packets when a connection is made, which would also explain the "not found by scan" issue.

wallforfry commented 1 week ago

@uzlonewolf I previously connect it with localtuya which totally works with 3.3 version.

Then I reset the device to renew the localkey and stop all others connections (also closed the smartlife mobile app just after pairing). I'll try to do it again multiple times just in case 🤞