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

Unexpected Payload from Device error 904, but working? #472

Open vvmichielvv opened 3 months ago

vvmichielvv commented 3 months ago

All,

Getting further into refining everything at home, I bump into the 'Unexpected Payload from Device' error. Strange thing is that I can communicate with it (fe. switching the device on), but when listening for an update (to monitor any changes on the device itself) it errors like this. I also noticed that it is with devices I bought pretty recent. Some that error are v3.3, newest are v3.4.

In all cases, the keys have special characters in them, but again, work fine when trying to switch something. So it seems the key is correct.

Any ideas?

Regards, Michiel

uzlonewolf commented 3 months ago

Can you enable debug logging (tinytuya.set_debug(True)) and post the log when it does this?

vvmichielvv commented 3 months ago

Hi!

I get the following debug:

DEBUG:_recv_all(): no data? b''
DEBUG:_recv_all(): no data? b''
DEBUG:Error decoding received data - read retry 0/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1193, in _send_receive
    rmsg = self._receive()
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1081, in _receive
    data = self._recv_all( min_len )
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1061, in _recv_all
    raise DecodeError('No data received - connection closed?')
tinytuya.core.DecodeError: No data received - connection closed?
DEBUG:_recv_all(): no data? b''
DEBUG:_recv_all(): no data? b''
DEBUG:Error decoding received data - read retry 1/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1193, in _send_receive
    rmsg = self._receive()
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1081, in _receive
    data = self._recv_all( min_len )
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1061, in _recv_all
    raise DecodeError('No data received - connection closed?')
tinytuya.core.DecodeError: No data received - connection closed?
DEBUG:_recv_all(): no data? b''
DEBUG:_recv_all(): no data? b''
DEBUG:Error decoding received data - read retry 2/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1193, in _send_receive
    rmsg = self._receive()
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1081, in _receive
    data = self._recv_all( min_len )
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1061, in _recv_all
    raise DecodeError('No data received - connection closed?')
tinytuya.core.DecodeError: No data received - connection closed?
DEBUG:_recv_all(): no data? b''
DEBUG:_recv_all(): no data? b''
DEBUG:Error decoding received data - read retry 3/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1193, in _send_receive
    rmsg = self._receive()
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1081, in _receive
    data = self._recv_all( min_len )
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1061, in _recv_all
    raise DecodeError('No data received - connection closed?')
tinytuya.core.DecodeError: No data received - connection closed?
DEBUG:_recv_all(): no data? b''
DEBUG:_recv_all(): no data? b''
DEBUG:Error decoding received data - read retry 4/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1193, in _send_receive
    rmsg = self._receive()
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1081, in _receive
    data = self._recv_all( min_len )
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1061, in _recv_all
    raise DecodeError('No data received - connection closed?')
tinytuya.core.DecodeError: No data received - connection closed?
DEBUG:_recv_all(): no data? b''
DEBUG:_recv_all(): no data? b''
DEBUG:Error decoding received data - read retry 5/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1193, in _send_receive
    rmsg = self._receive()
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1081, in _receive
    data = self._recv_all( min_len )
  File "/usr/local/lib/python3.8/dist-packages/tinytuya/core.py", line 1061, in _recv_all
    raise DecodeError('No data received - connection closed?')
tinytuya.core.DecodeError: No data received - connection closed?
DEBUG:ERROR Unexpected Payload from Device - 904 - payload: null
[TUYA]-[2024-03-13 19:38:34]-[Tuya_Lagekast: {'Error': 'Unexpected Payload from Device', 'Err': '904', 'Payload': None}]
INFO:[TUYA]-[2024-03-13 19:38:34]-[Tuya_Lagekast: {'Error': 'Unexpected Payload from Device', 'Err': '904', 'Payload': None}]
uzlonewolf commented 3 months ago

It looks like you are sending a command but not getting a response. What command are you sending, and how are you listening for updates?

vvmichielvv commented 3 months ago

Simplifying my code:

self.tuya_device = tinytuya.OutletDevice(_devId, _ip, _key)
self.tuya_device.set_version(_version)
self.tuya_device.set_socketPersistent(True)
self.tuya_device.set_socketTimeout(60) 

payload = self.tuya_device.generate_payload(tinytuya.DP_QUERY)
self.tuya_device.send(payload)

while(True):
    data = self.tuya_device.receive()

    if str(data) != "" and str(data) != "None":
        # do something with the data
    else :  
        payload = self.tuya_device.generate_payload(tinytuya.HEART_BEAT)
        self.tuya_device.send(payload)

The above initializes the Outletdevice, send a DP_QUERY one time, and then sends HEART_BEAT in a loop, checking each time if there is something to receive (i.e. someone has physically changed a dimmer with a wall control).

uzlonewolf commented 3 months ago

Okay, I see what's going on. I agree, a receive-only call that does not send anything should not raise an error if nothing is received. I'll try to get this fixed soon.