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

Err 904: Unexpected Payload from Device #482

Closed TheFirstQuestion closed 3 months ago

TheFirstQuestion commented 3 months ago

I'm trying to use tinytuya with my power strip, and keep getting {'Error': 'Unexpected Payload from Device', 'Err': '904', 'Payload': None}.

Here's my test code:

import tinytuya

tinytuya.set_debug(True)

d = tinytuya.OutletDevice(
    dev_id='12000225ccf7f50c2aa',
    address='10.112.151.118',
    local_key='K(OTq/}aduz<BotO',
    version=3.3)

print(d)
data = d.status()
print(data)

and the output:

DEBUG:TinyTuya [1.13.2]

DEBUG:Python 3.12.2 | packaged by Anaconda, Inc. | (main, Feb 27 2024, 17:35:02) [GCC 11.2.0] on linux
DEBUG:Using pyca/cryptography 42.0.5 for crypto, GCM is supported
OutletDevice( '12000225ccf7f50c2aa', address='10.112.151.118', local_key='K(OTq/}aduz<BotO', dev_type='default', connection_timeout=5, version=3.3, persist=False, cid=None, parent=None, children={} )
DEBUG:status() entry (dev_type is default)
DEBUG:final payload_dict for '12000225ccf7f50c2aa' ('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":"12000225ccf7f50c2aa","devId":"12000225ccf7f50c2aa","uid":"12000225ccf7f50c2aa","t":"1712159658"}'
DEBUG:sending payload
DEBUG:payload encrypted=b'000055aa000000010000000a00000078ab40d9a132ac064875dd35ba12ff937e2bb7b94244a7491f9b558ade0db6bdca99ed628aeafaf1e5d86ff6b451fd4e00284812b21e7239a1965243ef87c5fe05df30d1fa3768174c0860b68e1efeeca51c26fbce230414e9385365970371d9aeb0608d5f126f981c3c9f3c2888e0e81ebf526fd20000aa55'
DEBUG:received data=b'000055aa000000010000000a0000001d000000016461746120666f726d6174206572726f7271bf61a40000aa55'
DEBUG:received message=TuyaMessage(seqno=1, cmd=10, retcode=1, payload=b'data format error', crc=1908367780, crc_good=True, prefix=21930, iv=None)
DEBUG:raw unpacked message = TuyaMessage(seqno=1, cmd=10, retcode=1, payload=b'data format error', crc=1908367780, crc_good=True, prefix=21930, iv=None)
DEBUG:decode payload=b'data format error'
DEBUG:decrypting=b'data format error'
DEBUG:incomplete payload=b'data format error' (len:17)
Traceback (most recent call last):
  File "/home/steven/miniconda3/envs/Winston/lib/python3.12/site-packages/tinytuya/core.py", line 1386, in _decode_payload
    payload = cipher.decrypt(payload, False)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/steven/miniconda3/envs/Winston/lib/python3.12/site-packages/tinytuya/core.py", line 312, in decrypt
    raise ValueError("invalid length")
ValueError: invalid length
DEBUG:ERROR Unexpected Payload from Device - 904 - payload: null
DEBUG:status() received data={'Error': 'Unexpected Payload from Device', 'Err': '904', 'Payload': None}
DEBUG:Status request returned an error, is version 3.3 and local key b'K(OTq/}aduz<BotO' correct?
{'Error': 'Unexpected Payload from Device', 'Err': '904', 'Payload': None}

Here's the entry in snapshot.json:

"id": "12000225ccf7f50c2aa",
"name": "Power Strip",
"key": "K(OTq/}aduz<BotO",
"mac": "5c:cf:7f:50:c2:aa",
"uuid": "12000225ccf7f50c2aa",
"category": "cz",
"product_name": "Smart Power Strip",
"product_id": "of0VDuFcWsBTfzWt",
"biz_type": 18,
"model": "SM-SO301/306",
"sub": false,
"icon": "https://images.tuyaus.com/smart/icon/1524736914ad18sz4byzvckzb3a38m5nrk9_0.jpg",
"mapping": <omitted for brevity>,
"ip": "",
"origin": "cloud",
"ver": ""

I've tried version 3.4 and 3.5, and both give {'Error': 'Check device key or version', 'Err': '914', 'Payload': None}, so I believe 3.3 to be correct.

When following the steps in the README, everything worked fine, except the scan finds 0 devices. The wizard also couldn't find the local device: Polling local devices... [Power Strip] Error: No IP found. However, I can see the power strip in my router dashboard, and nmap shows the IP listening on port 6668, so I believe the IP is correct.

Any insight into what's going on?

jasonacox commented 3 months ago

Hi @TheFirstQuestion - Did you run python -m tinytuya wizard to get your local key? If so, it should have created a devices.json file that contains a "version" key that should help with the version. Also, with the devices.json file in your current directory you can run with force network polling mode:

python -m tinytuya scan -force

# or with debug
python -m tinytuya scan -force -d

That may be able to find the device on your network. If you still can't get the version, try 3.1 and 3.2 as well.

TheFirstQuestion commented 3 months ago

Awesome, the force scan worked perfectly! It found the device and told me that the correct version was 3.1.

Thank you so much for creating such an awesome library, and for the speedy response!