rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.84k stars 545 forks source link

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xYY in position Z: invalid start (or continuation) byte #424

Open rospogrigio opened 3 years ago

rospogrigio commented 3 years ago

@postlund , I have some more info regarding the error in the subject, that was reported in several Issues: #330, #313, #282, #201, #193 . I had to reset and re-pair one of my type_0d cover devices. As we all know, its localKey changed in the process but when I updated it the cover remained unavailable. In the logs I found that error. Now, curious facts: 1) the tuyadebug scripts were working fine, and very repeatably. No issues at all 2) I tried rebooting the whole RPi but the issue was still there, very repeatable as well. Didn't try to power off-and-on the cover, though, by bad 3) since it has always worked well before the new pairing, I re-paired it again (new localKey again): now it works flawlessly again

So, I don't know what generates this situation, but I believe it has to be something with your implementation of pytuya because the one included in tuyadebug was not giving the same error. I am attaching the logs in case you are able to investigate.

Failing device:

2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Started heartbeat loop
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Sending command heartbeat (device type: type_0a)
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Send payload: b'{}'
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Waiting for sequence number -100
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Sending command status (device type: type_0a)
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Send payload: b'{"gwId":"bf8...njv","devId":"bf8...njv"}'
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Waiting for sequence number 1
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Got heartbeat response
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Dispatching message TuyaMessage(seqno=1, cmd=10, retcode=1, payload=b'{+m\x89|\xdbh\x82\x96\x003\x99\xfa8\xc5\xc5\x07\x90Wkv\xd0\xd2\xf1\xe87\xde\xd8\x99\xfdF\xf8', crc=1240763341)
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Dispatching sequence number 1
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Decrypted payload: {}
2021-03-28 15:43:48 ERROR (MainThread) [custom_components.localtuya.common] [bf8...njv] Connect to 192.168.1.9 failed
Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 149, in _make_connection
    status = await self._interface.status()
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 472, in status
    status = await self.exchange(STATUS)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 457, in exchange
    payload = self._decode_payload(msg.payload)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 560, in _decode_payload
    payload = payload.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 3: invalid start byte
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Closing connection
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf8...njv] Stopped heartbeat loop

Succeeding device:

2021-03-28 15:43:47 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Started heartbeat loop
2021-03-28 15:43:47 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Sending command heartbeat (device type: type_0a)
2021-03-28 15:43:47 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Send payload: b'{}'
2021-03-28 15:43:47 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Waiting for sequence number -100
2021-03-28 15:43:47 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Sending command status (device type: type_0a)
2021-03-28 15:43:47 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Send payload: b'{"gwId":"bfe...esy","devId":"bfe...esy"}'
2021-03-28 15:43:47 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Waiting for sequence number 1
2021-03-28 15:43:47 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2021-03-28 15:43:47 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Got heartbeat response
2021-03-28 15:43:47 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Dispatching message TuyaMessage(seqno=1, cmd=10, retcode=1, payload=b'\xee\nZ\x17\x99\xee\x1e8\x92\xb2r\xae\x06\xeb.Ms\xb3\xc7\x1a&\x00g\xd1\x93\x0b\x98\x1b]\x9f\xd4I', crc=2048315308)
2021-03-28 15:43:47 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Dispatching sequence number 1
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Decrypted payload: {}
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] switching to dev_type type_0d
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Re-send status due to device type change (type_0a -> type_0d)
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Sending command status (device type: type_0d)
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Send payload: b'{"devId":"bfe...esy","uid":"bfe...esy","t":"1616939028","dps":{"1":null,"101":null}}'
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Waiting for sequence number 2
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Dispatching message TuyaMessage(seqno=0, cmd=8, retcode=0, payload=b"3.3\x00\x00\x00\x00\x00\x00\x00=\x00\x00\x00\x01\xee\xe7\x8b\\\x068\xa3;\x14\x0b'?j\xbdv\x88\xd6\x0f\xbb\x05=\xb8`\x04b\x89\x00\x99l\xe9\x80\xd0\xa5\xce\xf2:\x12\xca\xf9\xf4mK\xa0h\x9a\x01\xd6t", crc=3545427322)
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Got status update
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Decrypted payload: {"dps":{"1":"stop","101":false},"t":1616939027}
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Dispatching message TuyaMessage(seqno=2, cmd=13, retcode=0, payload=b'', crc=2380353348)
2021-03-28 15:43:48 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bfe...esy] Dispatching sequence number 2

Hope it might help, bye! Edit: now my device works so I cannot test any solution, but most of the people in the Issues I've mentioned are probably still struggling with this problem so they might test on their hardware.

postlund commented 3 years ago

Hmm, that's interesting. In the failing example, retcode is 1 which indicates an error. I think we need to get data that successfully decodes with tuyadebug but don't work with my changes, so we can compare. Basically raw data sent and received as well as the local key. Do you think you can reproduce this behavior by re-provisioning a device?

rospogrigio commented 3 years ago

Sorry but at the moment I am too busy to play with this but I believe that KixMan28 is still in this situation (see https://github.com/rospogrigio/localtuya/issues/330#issuecomment-769681941 in #330 ) so maybe he can provide the data you neeed. Moreover, I experienced it only once so it could need several re-pairings before the issue comes out again so it would be better to use a device that is already showing the problem. Maybe after Easter I'll have some more time if you don't succeed with KixMan28. Let me know how it goes, bye!

KixMan28 commented 3 years ago

@postlund What information do you need from my side to have this moving forward?

KixMan28 commented 3 years ago

@postlund @rospogrigio do you need anything from my side to have this solved?

aug70 commented 3 years ago

Is this ticket looked at? I'm getting below;

2021-07-09 01:36:14 ERROR (MainThread) [custom_components.localtuya.pytuya] [eb9...dxb] Failed to get status: 'utf-8' codec can't decode bytes in position 6-8: invalid continuation byte
Traceback (most recent call last):
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 510, in detect_available_dps
    data = await self.status()
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 472, in status
    status = await self.exchange(STATUS)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 457, in exchange
    payload = self._decode_payload(msg.payload)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 544, in _decode_payload
    payload = self.cipher.decrypt(payload, False)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 196, in decrypt
    return self._unpad(decryptor.update(enc) + decryptor.finalize()).decode()
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 6-8: invalid continuation byte
KixMan28 commented 3 years ago

@aug70 I don't think so. See my posts. No reply on them. I have started earlier an issue ( #330 ) which is mentioned here, but nothing happens neither there or here.

KixMan28 commented 3 years ago

@aug70 I don't think so. See my posts. No reply on them. I have started earlier an issue ( #330 ) which is mentioned here, but nothing happens neither there or here.

rospogrigio commented 3 years ago

Sorry guys but this goes beyond my knowledge and skills, so I'm afraid only @postlund can give a hand but he looks very overwhelmed recently. Moreover, my devices do not have this problem so I cannot test it: it happened once, but after I re-paired the device the problem disappeared, so my suggestion is to try re-pairing. If it persists, then it's only postlund that can provide a fix. Sorry for not being able to help more, bye...

aug70 commented 3 years ago

@KixMan28 yes I saw your posts today. Thanks for your detailed input.

@aug70 I don't think so. See my posts. No reply on them. I have started earlier an issue ( #330 ) which is mentioned here, but nothing happens neither there or here.

aug70 commented 3 years ago

@postlund I have been trying to get a smart plug integrated with LocalTuya and I have seen different failures. One of them was "... invalid continuation byte ...", like mentioned in this post. Another one was something like bad payload (I'm not at home at the moment, can't find the exact error message). I got the device working with LocalTuya only for once but when plugged off the device, it started failing again.

abonforti commented 2 years ago

Facing the same problem with a chinese smart plug:

[bf8...vmr] Connect to 192.168.X.X failed

Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 149, in _make_connection
    status = await self._interface.status()
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 472, in status
    status = await self.exchange(STATUS)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 457, in exchange
    payload = self._decode_payload(msg.payload)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 557, in _decode_payload
    payload = payload.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 1: invalid start byte

Funny thing is that it's sibling plug is getting integrated without any problem. They both have the same firmware version (1.0.6), as I can see from the Smart Life app. They're branded "Nooie IT-PE10-4-ZY ", even if from the tuya panel I can see: "PE10-Nooie-AH-欧规-插座"

shtrom commented 2 years ago

I'm having this issue on and off with a Mirabella Genio-branded light globe (GENIO A60 9W LED GLS BK, I002605). This is with Home Assistant 2021.9.6 [update: same on 2021.11.1] on Docker, with localtuya installed via HACS 1.16.0 (it says localtuya is at v3.2.3).

Somehow, it seems my mileage varies depending on whether I allow outbound internet access to the light (with or without DNS access). The type also varies: type_0a with Internet access, type_0d without.

Also worth noting is that HA and the light globe aren't on the same LAN segment. All traffic from HA to the IoT segment is allowed, but only related/established from the IoT segment to HA is let through. Notably, the light globe broadcasts messages to 255.255.255.255 those aren't allowed through.

At some point during my tests, I managed to get HA to find some DPs, but I was crucially missing the On/Off switch (id 20). tuyadebug confirmed that the DP was no longer advertised. I haven't been able to reproduce this case, but will keep trying.

@postlund does the below contain enough payload data for you?

Full internet access

It works fine with tuyadebug

$ ./test.py <DEVICE_ID> <IP_ADDRESS> <LOCAL_KEY> 3.3       
INFO:localtuya:localtuya version 1.0.0
INFO:localtuya:Python 3.9.7 (default, Sep 24 2021, 09:43:00) 
[GCC 10.3.0] on linux
INFO:localtuya:Using pytuya version '8.1.0'
INFO:localtuya:Detecting list of available DPS of device <DEVICE_ID> [<IP_ADDRESS>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<DEVICE_ID>","devId":"<DEVICE_ID>"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b'{W\xa3\xf0\xd3\x04\x84\x9eg\xf1\x1do\xdf\x0f\xae;o\xd0\xcbVLU5e\x85\x07\xbd\x80\xfd\x95\x01`u\xb4\x1e\xeb.\xe5\xca\xc9\x0e\xe7]2\xec\x0c\xf8\xaa\xf6\xaf\xd4\x13\xd0_\x1a\n\xd4\x90\x03\x84sR\x02\xf7\x8a+\xb0X\xb8\xe1\xafD;\x01Uy\xc4`$o\x03\xef\x9d\xa5\x07\xd2y\xb7\xb9u\xf9\xea\xc9\xe3\x7f\xd0'
DEBUG:localtuya.pytuya:decrypted result='{"dps":{"20":true,"21":"white","22":1000,"25":"030e0d0000000000000001f403e8","26":0}}'
AVAILABLE DPS ARE [{'20': True, '21': 'white', '22': 1000, '25': '030e0d0000000000000001f403e8', '26': 0}]
INFO:localtuya:COMPLETE response from device <DEVICE_ID> [<IP_ADDRESS>].
INFO:localtuya:Detecting list of available DPS of device <DEVICE_ID> [<IP_ADDRESS>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<DEVICE_ID>","devId":"<DEVICE_ID>"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b'{W\xa3\xf0\xd3\x04\x84\x9eg\xf1\x1do\xdf\x0f\xae;o\xd0\xcbVLU5e\x85\x07\xbd\x80\xfd\x95\x01`u\xb4\x1e\xeb.\xe5\xca\xc9\x0e\xe7]2\xec\x0c\xf8\xaa\xf6\xaf\xd4\x13\xd0_\x1a\n\xd4\x90\x03\x84sR\x02\xf7\x8a+\xb0X\xb8\xe1\xafD;\x01Uy\xc4`$o\x03\xef\x9d\xa5\x07\xd2y\xb7\xb9u\xf9\xea\xc9\xe3\x7f\xd0'
DEBUG:localtuya.pytuya:decrypted result='{"dps":{"20":true,"21":"white","22":1000,"25":"030e0d0000000000000001f403e8","26":0}}'
AVAILABLE DPS ARE [{'20': True, '21': 'white', '22': 1000, '25': '030e0d0000000000000001f403e8', '26': 0}]
INFO:localtuya:COMPLETE response from device <DEVICE_ID> [<IP_ADDRESS>].
INFO:localtuya:Detecting list of available DPS of device <DEVICE_ID> [<IP_ADDRESS>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<DEVICE_ID>","devId":"<DEVICE_ID>"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b'{W\xa3\xf0\xd3\x04\x84\x9eg\xf1\x1do\xdf\x0f\xae;o\xd0\xcbVLU5e\x85\x07\xbd\x80\xfd\x95\x01`u\xb4\x1e\xeb.\xe5\xca\xc9\x0e\xe7]2\xec\x0c\xf8\xaa\xf6\xaf\xd4\x13\xd0_\x1a\n\xd4\x90\x03\x84sR\x02\xf7\x8a+\xb0X\xb8\xe1\xafD;\x01Uy\xc4`$o\x03\xef\x9d\xa5\x07\xd2y\xb7\xb9u\xf9\xea\xc9\xe3\x7f\xd0'
DEBUG:localtuya.pytuya:decrypted result='{"dps":{"20":true,"21":"white","22":1000,"25":"030e0d0000000000000001f403e8","26":0}}'
AVAILABLE DPS ARE [{'20': True, '21': 'white', '22': 1000, '25': '030e0d0000000000000001f403e8', '26': 0}]
INFO:localtuya:COMPLETE response from device <DEVICE_ID> [<IP_ADDRESS>].
INFO:localtuya:TIMEOUT: No response from device <DEVICE_ID> [<IP_ADDRESS>] after 2 attempts.

but not with HA/localtuya (docker):

homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Sending command heartbeat (device type: type_0a)
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Send payload: b'{}'
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Waiting for sequence number -100
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Sending command status (device type: type_0a)
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Send payload: b'{"gwId":"<DEVICE_ID>","devId":"<DEVICE_ID>"}'
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Waiting for sequence number 1
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Got heartbeat response
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Decrypted payload: {}
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching message TuyaMessage(seqno=1, cmd=10, retcode=0, payload=b'{W\xa3\xf0\xd3\x04\x84\x9eg\xf1\x1do\xdf\x0f\xae;o\xd0\xcbVLU5e\x85\x07\xbd\x80\xfd\x95\x01`u\xb4\x1e\xeb.\xe5\xca\xc9\x0e\xe7]2\xec\x0c\xf8\xaa\xf6\xaf\xd4\x13\xd0_\x1a\n\xd4\x90\x03\x84sR\x02\xf7\x8a+\xb0X\xb8\xe1\xafD;\x01Uy\xc4`$o\x03\xef\x9d\xa5\x07\xd2y\xb7\xb9u\xf9\xea\xc9\xe3\x7f\xd0', crc=2267913001)
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching sequence number 1
homeassistant    | 2021-11-06 22:07:00 ERROR (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Failed to get status: 'utf-8' codec can't decode byte 0xa3 in position 2: invalid start byte
homeassistant    | Traceback (most recent call last):
homeassistant    |   File "/config/custom_components/localtuya/pytuya/__init__.py", line 510, in detect_available_dps
homeassistant    |     data = await self.status()
homeassistant    |   File "/config/custom_components/localtuya/pytuya/__init__.py", line 472, in status
homeassistant    |     status = await self.exchange(STATUS)
homeassistant    |   File "/config/custom_components/localtuya/pytuya/__init__.py", line 457, in exchange
homeassistant    |     payload = self._decode_payload(msg.payload)
homeassistant    |   File "/config/custom_components/localtuya/pytuya/__init__.py", line 557, in _decode_payload
homeassistant    |     payload = payload.decode()
homeassistant    | UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 2: invalid start byte
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Closing connection
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Stopped heartbeat loop
homeassistant    | 2021-11-06 22:07:00 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Connection lost: None

No internet access

After a power-cycle with a 60s wait before turning back on. DNS traffic to router disabled (but I still see traffic for remote IP addresses, so I'm thinking that 60s might not be long enough to clear the globe's memory; I'll retry tomorrow and update; UPDATE: same behaviour after ~10 hours off).

tuyadebug find different, but senseless DPs (and type_0d):

$ ./test.py <DEVICE_ID> <IP_ADDRESS> 8537f0e2ed4286e4 3.3
INFO:localtuya:localtuya version 1.0.0
INFO:localtuya:Python 3.9.7 (default, Sep 24 2021, 09:43:00) 
[GCC 10.3.0] on linux
INFO:localtuya:Using pytuya version '8.1.0'
INFO:localtuya:Detecting list of available DPS of device <DEVICE_ID> [<IP_ADDRESS>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<DEVICE_ID>","devId":"<DEVICE_ID>"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b'\xf2\xe5\xeb\x86\xd4]\x97\x1b\x1d\xc8\xef\xaaU\x95\x9f\x1e\x80G\x98A\x13\xd6\xdb\xc0\x83\x15!4\x1f\x1c( '
DEBUG:localtuya.pytuya:'data unvalid' error detected: switching to dev_type 'type_0d'
DEBUG:localtuya.pytuya:Re-send status due to device type change (type_0a -> type_0d)
DEBUG:localtuya.pytuya:Sending command status (device type: type_0d)
DEBUG:localtuya.pytuya:paylod=b'{"devId":"<DEVICE_ID>","uid":"<DEVICE_ID>","t":"1636198739","dps":{"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null,"9":null,"10":null}}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:READING AGAIN!
DEBUG:localtuya.pytuya:Failed to connect to <IP_ADDRESS>. Raising Exception.
WARNING:localtuya.pytuya:Failed to get status: timed out
INFO:localtuya:Detecting list of available DPS of device <DEVICE_ID> [<IP_ADDRESS>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<DEVICE_ID>","devId":"<DEVICE_ID>"}'
DEBUG:localtuya.pytuya:Failed to connect to <IP_ADDRESS>. Raising Exception.
WARNING:localtuya.pytuya:Failed to get status: timed out
INFO:localtuya:Detecting list of available DPS of device <DEVICE_ID> [<IP_ADDRESS>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<DEVICE_ID>","devId":"<DEVICE_ID>"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b'\xf2\xe5\xeb\x86\xd4]\x97\x1b\x1d\xc8\xef\xaaU\x95\x9f\x1e\x80G\x98A\x13\xd6\xdb\xc0\x83\x15!4\x1f\x1c( '
DEBUG:localtuya.pytuya:'data unvalid' error detected: switching to dev_type 'type_0d'
DEBUG:localtuya.pytuya:Re-send status due to device type change (type_0a -> type_0d)
DEBUG:localtuya.pytuya:Sending command status (device type: type_0d)
DEBUG:localtuya.pytuya:paylod=b'{"devId":"<DEVICE_ID>","uid":"<DEVICE_ID>","t":"1636198754","dps":{"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null,"9":null,"10":null}}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:READING AGAIN!
DEBUG:localtuya.pytuya:Failed to connect to <IP_ADDRESS>. Raising Exception.
WARNING:localtuya.pytuya:Failed to get status: timed out
INFO:localtuya:TIMEOUT: No response from device <DEVICE_ID> [<IP_ADDRESS>] after 2 attempts.

HA complains that “Connection to device succeeded but no datapoints found, please try again. Create a new issue and include debug logs if problem persists.”

homeassistant    | 2021-11-06 22:40:56 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Started heartbeat loop                                       
homeassistant    | 2021-11-06 22:40:56 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Sending command heartbeat (device type: type_0a)             
homeassistant    | 2021-11-06 22:40:56 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Send payload: b'{}'                                          
homeassistant    | 2021-11-06 22:40:56 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Waiting for sequence number -100                             
homeassistant    | 2021-11-06 22:40:56 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Sending command status (device type: type_0a)                
homeassistant    | 2021-11-06 22:40:56 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Send payload: b'{"gwId":"<DEVICE_ID>","devId":"eba
fe767a15e74b8d5eknd"}'                                                                                                                                                  
homeassistant    | 2021-11-06 22:40:56 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Waiting for sequence number 1                                
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, pa
yload=b'', crc=2958142211)                                                                                                                                              
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Got heartbeat response                                       
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Decrypted payload: {}                                        
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching message TuyaMessage(seqno=1, cmd=10, retcode=1, p
ayload=b'\xf2\xe5\xeb\x86\xd4]\x97\x1b\x1d\xc8\xef\xaaU\x95\x9f\x1e\x80G\x98A\x13\xd6\xdb\xc0\x83\x15!4\x1f\x1c( ', crc=3280231085)                                     
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching sequence number 1                                
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] switching to dev_type type_0d                                
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Re-send status due to device type change (type_0a -> type_0d)
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Sending command status (device type: type_0d)                
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Send payload: b'{"devId":"<DEVICE_ID>","uid":"ebaf
e767a15e74b8d5eknd","t":"1636198857","dps":{"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null,"9":null,"10":null}}'                               
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Waiting for sequence number 2                                
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching message TuyaMessage(seqno=2, cmd=13, retcode=0, p
ayload=b'', crc=2380353348)                                                                                                                                             
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching sequence number 2                                
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Decrypted payload: {}
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Sending command status (device type: type_0d)
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Send payload: b'{"devId":"<DEVICE_ID>","uid":"<DEVICE_ID>","t":"1636198857","dps":{"1":null,"11":null,"12":null,"13":null,"14":null,"15":null,"16":null,"17":null,"18":null,"19":null,"20":null}}'
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Waiting for sequence number 3
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching message TuyaMessage(seqno=3, cmd=13, retcode=0, payload=b'', crc=1350014657)
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching sequence number 3
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Decrypted payload: {}
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Sending command status (device type: type_0d)
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Send payload: b'{"devId":"<DEVICE_ID>","uid":"<DEVICE_ID>","t":"1636198857","dps":{"1":null,"21":null,"22":null,"23":null,"24":null,"25":null,"26":null,"27":null,"28":null,"29":null,"30":null}}'
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Waiting for sequence number 4
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching message TuyaMessage(seqno=4, cmd=13, retcode=0, payload=b'', crc=755273881)
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching sequence number 4
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Decrypted payload: {}
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Sending command status (device type: type_0d)
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Send payload: b'{"devId":"<DEVICE_ID>","uid":"<DEVICE_ID>","t":"1636198857","dps":{"1":null,"100":null,"101":null,"102":null,"103":null,"104":null,"105":null,"106":null,"107":null,"108":null,"109":null,"110":null}}'
homeassistant    | 2021-11-06 22:40:57 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Waiting for sequence number 5
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching message TuyaMessage(seqno=0, cmd=8, retcode=0, payload=b'3.3\x00\x00\x00\x00\x00\x00]F\x00\x00\x00\x01WIB\xd0r&\x81\xa5\xac\x0b\x13\x95\n\x0f$\x99\x94\xb5\x80\xd4\xf0+Z!-\x0f\xdaP{\x9f\x1fbU\xe8\x93cH\x94\xc2\xf4\xaf\xc2\xe4\xc2\xd2\xe8g\x92', crc=3767294985)
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Got status update
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Decrypted payload: {"dps":{},"type":"query","t":130}
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching message TuyaMessage(seqno=0, cmd=8, retcode=0, payload=b'3.3\x00\x00\x00\x00\x00\x00]G\x00\x00\x00\x01WIB\xd0r&\x81\xa5\xac\x0b\x13\x95\n\x0f$\x99\x94\xb5\x80\xd4\xf0+Z!-\x0f\xdaP{\x9f\x1fbU\xe8\x93cH\x94\xc2\xf4\xaf\xc2\xe4\xc2\xd2\xe8g\x92', crc=2656365640)
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Got status update
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Decrypted payload: {"dps":{},"type":"query","t":130}
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching message TuyaMessage(seqno=5, cmd=13, retcode=0, payload=b'', crc=4036118812)
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Dispatching sequence number 5
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Decrypted payload: {}
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Detected dps: {}
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Closing connection
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Stopped heartbeat loop
homeassistant    | 2021-11-06 22:40:58 DEBUG (MainThread) [custom_components.localtuya.pytuya] [eba...knd] Connection lost: None
rospogrigio commented 2 years ago

Gosh @shtrom this is really shocking: I would never imagine a device could behave both as a type_0a and a sa type_0d device depending on the situation! Sorry but I can't help you on this, maybe @postlund can give some help but he seems to be very busy (as most of us are, actually).

shtrom commented 2 years ago

Shocking, huh? (:

To be honest, I'm not sure what those types are, couldn't find anything in the code from a cursory glance. What are the differences?

That said, this seems to vary depending on whether any functional internet access was available when the globe was turned on. Otherwise, the DPs found seem nonsensical, regardless of whether DNS is accessible or not (blocking both TCP and UDP).

It's a fairly recent globe, say mid-2021. I could not flash it with Tasmota, and I fear it might have updated the firmware to something even more recent when I paired it to Tuya Smart. Still, they can't be completely disabling offline operation... I guess this is a completely separate issue at this point.

Nonetheless, there is a discrepancy between localtuya and test.py in tuyadebug. The latter is happy and finds DPs as desired when the globe is allowed internet access. I assume they are not using the same lib/version?

If you can share any pointers or ideas about the differences between the two, I could try to dig in myself [time allowing, of course]. -- Olivier Mehani @.***> Sent from my mobile, please excuse my brevity.

rospogrigio commented 2 years ago

Shocking, huh? (: To be honest, I'm not sure what those types are, couldn't find anything in the code from a cursory glance. What are the differences?

In PR #6 you will find a (pretty long) discussion that explains what type_0a and type_0d are and how they behave differently.

Nonetheless, there is a discrepancy between localtuya and test.py in tuyadebug. The latter is happy and finds DPs as desired when the globe is allowed internet access. I assume they are not using the same lib/version?

test.py is still using some old code, localtuya has been developed more recently and is more refined, however probably this behavior is some corner case not correctly handled. Localtuya's code is from postlund and is a bit too complicated for me to handle, however by comparing the behaviors of the two codes you might end up understanding what is going wrong. When I had this issue, I was having it with both codes so I cannot be of any help (re-pairing the device solved the issue, for me).

shtrom commented 2 years ago

Ok, on a hunch, I commented those two lines https://github.com/rospogrigio/localtuya/blob/master/custom_components/localtuya/pytuya/__init__.py#L532-L533 because

  1. they were'nt in tuyadebug
  2. my payload actually starts with {
  3. they seemed to skip some decyphering, which might lead to non-unicode characters downstream

and...

🎉 (using hints from #374 and #33); ~I can't seem to get a brightness entity, though... Just an on/off switch.~

Screenshot from 2021-11-09 23-15-09

Now to disconnect it from the cloud...

shtrom commented 2 years ago

FWIW, those lines came from this commit https://github.com/rospogrigio/localtuya/commit/e6538a46810f8d62e6f8ee26084d6c38d752da72, but it somehow appears like, despite being encrypted, my globe still responds with a leading { in the payload

hugoideler commented 2 years ago

Hi, I also encountered a UnicodeDecodeError, though I'm not sure if the root cause is the same. (If I need to split it off in a separate ticket, then let me know.)

The error I encountered is:

2021-12-05 10:02:38 ERROR (MainThread) [custom_components.localtuya.pytuya] [bf2...pbc] Failed to get status: 'utf-8' codec can't decode byte 0xe9 in position 52: invalid continuation byte
Traceback (most recent call last):
File "/config/custom_components/localtuya/pytuya/__init__.py", line 510, in detect_available_dps
data = await self.status()
File "/config/custom_components/localtuya/pytuya/__init__.py", line 472, in status
status = await self.exchange(STATUS)
File "/config/custom_components/localtuya/pytuya/__init__.py", line 457, in exchange
payload = self._decode_payload(msg.payload)
File "/config/custom_components/localtuya/pytuya/__init__.py", line 544, in _decode_payload
payload = self.cipher.decrypt(payload, False)
File "/config/custom_components/localtuya/pytuya/__init__.py", line 196, in decrypt
return self._unpad(decryptor.update(enc) + decryptor.finalize()).decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 52: invalid continuation byte

I encountered this while installing a Tuya pet feeder (an identical of one that I installed earlier, but maybe the firmware differs). Tuya product category: cwwsq.

Tuya-cli was able to use the device without problems:

$ DEBUG=* tuya-cli set --ip <ip> --id <id> --key <key> --protocol-version 3.3 --dps 3 --set True
  TuyAPI IP and ID are already both resolved. +0ms
  TuyAPI Connecting to <ip>... +2ms
  TuyAPI Socket connected. +8ms
  TuyAPI SET Payload: +1ms
  TuyAPI {
  TuyAPI   devId: '<id>',
  TuyAPI   gwId: '<id>',
  TuyAPI   uid: '',
  TuyAPI   t: 1638697231,
  TuyAPI   dps: { '3': true }
  TuyAPI } +0ms
  TuyAPI Received data: 000055aa00000001000000070000000c00000000a505a9140000aa55 +288ms
  TuyAPI Parsed: +1ms
  TuyAPI { payload: false, leftover: false, commandByte: 7, sequenceN: 1 } +0ms
  TuyAPI Got SET ack. +0ms
  TuyAPI Received data: 000055aa00000000000000080000003b00000000332e3300000000000092fc00000001dd1d356ad96d41691f38cc5920cb2e63d37aeca573c4afd5758695bbbdad07ef4e6789be0000aa55 +2s
  TuyAPI Parsed: +1ms
  TuyAPI {
  TuyAPI   payload: { dps: { '15': 1 }, t: 1638697232 },
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 8,
  TuyAPI   sequenceN: 0
  TuyAPI } +0ms
  TuyAPI Received DP_REFRESH packet. +0ms
  TuyAPI Disconnect +1ms
Set succeeded.
  TuyAPI Socket closed: <ip> +1ms

My workaround has been to patch the decode() function in decrypt() to use latin-1:

return self._unpad(decryptor.update(enc) + decryptor.finalize()).decode('latin-1')

This is not a proper solution (clearly), but it works for me for the time being.

The output of test.py from tuyadebug (with the patched function is):

$ ./test.py <id> <ip> <key> 3.3
INFO:localtuya:localtuya version 1.0.0
INFO:localtuya:Python 3.9.9 (main, Nov 20 2021, 21:30:06) 
[GCC 11.1.0] on linux
INFO:localtuya:Using pytuya version '8.1.0'
INFO:localtuya:Detecting list of available DPS of device <id> [<ip>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<id>","devId":"<id>"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b'\x8b\xd0\x9e2\x91\xfed\x02\xf6\xca\x99\xcf>yM@\x0c{\xc3\xa2\xb0LH)\x03\x08\xe6\x1c\x1bO%z\x17\xd8-\x17~\xf1\x97\xeb\x90NU\xf3\xca\xb8\xc5\x07W\x1b\x86Y\x9a\x9d\xbc\xd4\xbf\x02\xceg\xee\x16W\xaa!\xe2\xff\xf1\xf5X\x8a\x82\\Ji6XB,\xe6\x81\xe4\x8d\xdb\xde<\xfa\xeb\x02\x97w2\xce\x0f\xfa\x92\x8f\xfdg\xcaA\x18\xd2f\xb33o\n\x03\x06f;\xd8c\xda\xef$A\xb8.\x9e\xf6\x08\x16+?\x1dS\xbf$4\xf2i\xe8\xb4\xc6\xcd\x0c\xac\xfd\x06\xabB\xcc'
DEBUG:localtuya.pytuya:decrypted result='{"dps":{"3":1,"14":2,"15":1,"101":1,"102":true,"103":true,"104":"éð\\u000fïó\\b\x80ïó\\t\x81rFOð\\u0003\\u0003\xa0ñ\x80\\u0004\x84ó\\b\x88\\u0001L GpG\\u0019","105":0}}'
AVAILABLE DPS ARE [{'3': 1, '14': 2, '15': 1, '101': 1, '102': True, '103': True, '104': 'éð\x0fïó\x08\x80ïó\t\x81rFOð\x03\x03\xa0ñ\x80\x04\x84ó\x08\x88\x01L GpG\x19', '105': 0}]
INFO:localtuya:COMPLETE response from device <id> [<ip>].
INFO:localtuya:Detecting list of available DPS of device <id> [<ip>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<id>","devId":"<id>"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b'\x8b\xd0\x9e2\x91\xfed\x02\xf6\xca\x99\xcf>yM@\x0c{\xc3\xa2\xb0LH)\x03\x08\xe6\x1c\x1bO%z\x17\xd8-\x17~\xf1\x97\xeb\x90NU\xf3\xca\xb8\xc5\x07W\x1b\x86Y\x9a\x9d\xbc\xd4\xbf\x02\xceg\xee\x16W\xaa!\xe2\xff\xf1\xf5X\x8a\x82\\Ji6XB,\xe6\x81\xe4\x8d\xdb\xde<\xfa\xeb\x02\x97w2\xce\x0f\xfa\x92\x8f\xfdg\xcaA\x18\xd2f\xb33o\n\x03\x06f;\xd8c\xda\xef$A\xb8.\x9e\xf6\x08\x16+?\x1dS\xbf$4\xf2i\xe8\xb4\xc6\xcd\x0c\xac\xfd\x06\xabB\xcc'
DEBUG:localtuya.pytuya:decrypted result='{"dps":{"3":1,"14":2,"15":1,"101":1,"102":true,"103":true,"104":"éð\\u000fïó\\b\x80ïó\\t\x81rFOð\\u0003\\u0003\xa0ñ\x80\\u0004\x84ó\\b\x88\\u0001L GpG\\u0019","105":0}}'
AVAILABLE DPS ARE [{'3': 1, '14': 2, '15': 1, '101': 1, '102': True, '103': True, '104': 'éð\x0fïó\x08\x80ïó\t\x81rFOð\x03\x03\xa0ñ\x80\x04\x84ó\x08\x88\x01L GpG\x19', '105': 0}]
INFO:localtuya:COMPLETE response from device <id> [<ip>].
INFO:localtuya:Detecting list of available DPS of device <id> [<ip>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<id>","devId":"<id>"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b'\x8b\xd0\x9e2\x91\xfed\x02\xf6\xca\x99\xcf>yM@\x0c{\xc3\xa2\xb0LH)\x03\x08\xe6\x1c\x1bO%z\x17\xd8-\x17~\xf1\x97\xeb\x90NU\xf3\xca\xb8\xc5\x07W\x1b\x86Y\x9a\x9d\xbc\xd4\xbf\x02\xceg\xee\x16W\xaa!\xe2\xff\xf1\xf5X\x8a\x82\\Ji6XB,\xe6\x81\xe4\x8d\xdb\xde<\xfa\xeb\x02\x97w2\xce\x0f\xfa\x92\x8f\xfdg\xcaA\x18\xd2f\xb33o\n\x03\x06f;\xd8c\xda\xef$A\xb8.\x9e\xf6\x08\x16+?\x1dS\xbf$4\xf2i\xe8\xb4\xc6\xcd\x0c\xac\xfd\x06\xabB\xcc'
DEBUG:localtuya.pytuya:decrypted result='{"dps":{"3":1,"14":2,"15":1,"101":1,"102":true,"103":true,"104":"éð\\u000fïó\\b\x80ïó\\t\x81rFOð\\u0003\\u0003\xa0ñ\x80\\u0004\x84ó\\b\x88\\u0001L GpG\\u0019","105":0}}'
AVAILABLE DPS ARE [{'3': 1, '14': 2, '15': 1, '101': 1, '102': True, '103': True, '104': 'éð\x0fïó\x08\x80ïó\t\x81rFOð\x03\x03\xa0ñ\x80\x04\x84ó\x08\x88\x01L GpG\x19', '105': 0}]
INFO:localtuya:COMPLETE response from device <id> [<ip>].
INFO:localtuya:TIMEOUT: No response from device <id> [<ip>] after 2 attempts.

(Note the \0xe9 in the data from DPS 104.)

For reference, the older pet feeder works fine with the unpatched code:

$ ./test.py <id> <ip> <key> 3.3
INFO:localtuya:localtuya version 1.0.0
INFO:localtuya:Python 3.9.9 (main, Nov 20 2021, 21:30:06) 
[GCC 11.1.0] on linux
INFO:localtuya:Using pytuya version '8.1.0'
INFO:localtuya:Detecting list of available DPS of device <id> [<ip>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<id>","devId":"<id>"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b"O\x0e*\x98\xe3\xc7z\xff\x18\xf9(\xb2{\xc1c\xd5E\x10\xc9\x81\x15\xa9\x93\xf1 \xcf\xee\x01\xc5\xb4\x9c\xf05\x8c\xe8dD\xf0\xe7\x90y\xbf\x1f\n\xb1\xd2\xbd\x8d\x05\x16\x0f9\x04\xe4l6\xce\x81\xb8\xe8x\xadl*1\xe5A\x90-@\x95\x1cB\xb3\x14X\xaa\xe5O1\xa0\x92\x17\xabS\xc2\n4\x19\n#'\\$\xa2n\x91D\xf0QR\xf3zmj\xd9Y\xca\xf1\xb0\x96:"
DEBUG:localtuya.pytuya:decrypted result='{"devId":"<id>","dps":{"3":1,"14":1,"15":1,"101":1,"102":true,"103":true,"104":" "}}'
AVAILABLE DPS ARE [{'3': 1, '14': 1, '15': 1, '101': 1, '102': True, '103': True, '104': ' '}]
INFO:localtuya:COMPLETE response from device <id> [<ip>].
INFO:localtuya:Detecting list of available DPS of device <id> [<ip>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<id>","devId":"<id>"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b"O\x0e*\x98\xe3\xc7z\xff\x18\xf9(\xb2{\xc1c\xd5E\x10\xc9\x81\x15\xa9\x93\xf1 \xcf\xee\x01\xc5\xb4\x9c\xf05\x8c\xe8dD\xf0\xe7\x90y\xbf\x1f\n\xb1\xd2\xbd\x8d\x05\x16\x0f9\x04\xe4l6\xce\x81\xb8\xe8x\xadl*1\xe5A\x90-@\x95\x1cB\xb3\x14X\xaa\xe5O1\xa0\x92\x17\xabS\xc2\n4\x19\n#'\\$\xa2n\x91D\xf0QR\xf3zmj\xd9Y\xca\xf1\xb0\x96:"
DEBUG:localtuya.pytuya:decrypted result='{"devId":"<id>","dps":{"3":1,"14":1,"15":1,"101":1,"102":true,"103":true,"104":" "}}'
AVAILABLE DPS ARE [{'3': 1, '14': 1, '15': 1, '101': 1, '102': True, '103': True, '104': ' '}]
INFO:localtuya:COMPLETE response from device <id> [<ip>].
INFO:localtuya:Detecting list of available DPS of device <id> [<ip>], protocol 3.3.
DEBUG:localtuya.pytuya:Sending command status (device type: type_0a)
DEBUG:localtuya.pytuya:paylod=b'{"gwId":"<id>","devId":"<id>"}'
DEBUG:localtuya.pytuya:DATA RECEIVED!
DEBUG:localtuya.pytuya:decode payload=b"O\x0e*\x98\xe3\xc7z\xff\x18\xf9(\xb2{\xc1c\xd5E\x10\xc9\x81\x15\xa9\x93\xf1 \xcf\xee\x01\xc5\xb4\x9c\xf05\x8c\xe8dD\xf0\xe7\x90y\xbf\x1f\n\xb1\xd2\xbd\x8d\x05\x16\x0f9\x04\xe4l6\xce\x81\xb8\xe8x\xadl*1\xe5A\x90-@\x95\x1cB\xb3\x14X\xaa\xe5O1\xa0\x92\x17\xabS\xc2\n4\x19\n#'\\$\xa2n\x91D\xf0QR\xf3zmj\xd9Y\xca\xf1\xb0\x96:"
DEBUG:localtuya.pytuya:decrypted result='{"devId":"<id>","dps":{"3":1,"14":1,"15":1,"101":1,"102":true,"103":true,"104":" "}}'
AVAILABLE DPS ARE [{'3': 1, '14': 1, '15': 1, '101': 1, '102': True, '103': True, '104': ' '}]
INFO:localtuya:COMPLETE response from device <id> [<ip>].
INFO:localtuya:TIMEOUT: No response from device <id> [<ip>] after 2 attempts.

(Note that the output from DPS 104 is different.)

Stooovie commented 2 years ago

Same. Cannot get DPs of ANY of my five Tuya devices. I can control them with LocalTuya from Home Assistant just fine. I'm getting either an error 104 or the "failed to get status: 'utf-8' codec can't decode byte 0xe9 in position 0: invalid continuation byte" one. MacOS and Ubuntu.

hugoideler commented 2 years ago

@Stooovie: as a workaround you could try to patch the code to use .decode('latin-1') and see if that helps (see my comment, I also ran into 0xe9). I didn't look into a proper fix.