jasonacox / tinytuya

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

Communication errors with device that has local password containing single quote ' #532

Open RedFlh opened 3 months ago

RedFlh commented 3 months ago

HI, first of all congratulations and thank you for your great work with this project I have already used your code to communicate with several Tuya devices without problems. But now I can't communicate with a device that has the single quota ' character contained in the local key I have try to set the local key string closde by doble quote instead of single quote o escaping using \ but i can't comunicate with it. Could it be that this character prevents your library from working properly? I attach some explanatory images. This is test code (I have change real data to xxxxx for pivacy):

import tinytuya
import sys

# Connect to Device
d = tinytuya.OutletDevice(
    dev_id='xxxxxxxxxxxxxxxxx',
    address='192.168.0.33',      # 192.168.0.33 Or set to 'Auto' to auto-discover IP address
    local_key='xxxxxxx'xxxx',
    version=3.4)

# Get Status
data = d.status() 
print('status() result %r' % data)"

And the result is: "status() result {'Error': 'Check device key or version', 'Err': '914', 'Payload': None}" Thanks for your time!!!! Best regards T_001 T_002

uzlonewolf commented 3 months ago

Hi @RedFlh . I have multiple devices with single quotes in the local key and they all work fine, so it's not an issue with the library. I'd run the wizard again to make sure you have the correct local key.

RedFlh commented 2 months ago

Thanks for your quick response I ran the wizard and the key matches! I have rechecked several times, device id, key, protol version but nothing to do, I don't understand why! Thanks anyway

RedFlh commented 2 months ago

Hi, I only now noticed that there is another particular character in the local key this char ` Thanks again for your time and best regards

jasonacox commented 2 months ago

Hi @RedFlh - when you ran the wizard, was it able to poll the device? Same for scan (e.g. python3 -m tinytuya scan)?

If you unpair the device in the SmartLife app and re-pair it, Tuya will assign a new key. Now, if the key you are using is causing a bug to show up in tinytuya, that wouldn't address the bug in the code but may get it working for you. Also, you could post the old key so we can see if there are other characters that could be causing the issue.

RedFlh commented 2 months ago

Hi Jasonacox, Yes! the problem was caused by some character in the key! I deleted the device from the app and re-paired it, as you suggested and now it works with a different key!!! The old key that caused problems was: ".+*V?qp'h`D-nQ>+" Thanks for your availability!! Since you are so helpful, I'll take this opportunity to ask you for advice: I'm trying to convert your library to C# because I don't know the Python language. I encountered a big problem encrypting and decrypting data using AES GCM. Implementing the code in C# with real data captured using your library I can't decode the data correctly and I get an error: System.Security.Cryptography.CryptographicException: 'The computed authentication tag did not match the input authentication tag.' Any suggestions? Thanks again for your time and best regards!

uzlonewolf commented 2 months ago

I've seen all those symbols before (see #341 ) and none of them have ever caused problems. I'm going to blame this one on the device itself. It's kinda late to ask this, but did you reboot the device at any point? I've seen them just up and "forget" what their local key is supposed to be until they get power cycled, at which point they work fine for a while before forgetting again.

RedFlh commented 2 months ago

Hi Jasonacox, no, I have never reboot the device and it has always worked normally from the smartLife app. Regarding what I would like to obtain, (the instantaneous power in W), I am encountering other problems: In the information I see in the devices.json file, there are some ids(ex:6,11,12,13,14 ) that I don't see when querying the device Thid id Cutted part of devices.json: devices_cut.json And this is the response by querying the device status by Tinituya: D:\Python>python -m Get_SYSmt_Mtr_sts {'dps': {'1': 496, '11': False, '16': True, '101': 'Closed', '102': 'Trip', '103': 'Alarm', '104': 'Trip', '105': 'Closed', '107': 'Trip', '109': 'online', '110': 'Manual_ON', '111': True, '112': True, '114': 50, '115': 280, '116': 165, '119': 2000, '120': 10, '125': 496, '131': 357, '137': 600, '138': '', '140': 5, '141': False, '142': 'memory'}} Why are some ids missing? Thanks again for your time and best regards!

jasonacox commented 2 months ago

always worked normally from the smartLife app

That's correct. These devices are designed to be cloud-first and the local access is an after thought. SmartLife will pull from cloud if it can't get a local update. The local socket on these devices can get into a bad state that will allow the device to work with the cloud but not for local access. As @uzlonewolf mentioned, I suspect if you rebooted the device it would have fixed it. Repairing essentially reset it. I don't know why I didn't think of that.

I don't see when querying the device

We don't know why the devices sometimes return all the data values. You can sometimes see them running the monitoring loop https://github.com/jasonacox/tinytuya/blob/master/examples/monitor.py

Implementing the code in C# with real data captured using your library I can't decode the data correctly

I assume you are using this same version 3.4 device? Are you using the right nonce (initialization vector)?

uzlonewolf commented 2 months ago

using AES GCM

Wait, AES-GCM is a v3.5 thing, v3.4 devices still use AES-CBC but with a negotiated session key.

RedFlh commented 2 months ago

Hi Jasonacox, the question regarding AES-GCM was for another device using the version 3.5 protocol. I have a lot of devices, some with protocol 3.3, others 3.4, others 3.5 Thanks again for your time and best regards!

uzlonewolf commented 2 months ago

@jasonacox I guess I'm just an alt account of yours now :rofl: :rofl: :rofl:

@RedFlh If it helps any, there's a "fake v3.5 device" script in the tools/ folder I wrote specifically to help debug crypto implementations. https://github.com/jasonacox/tinytuya/blob/master/tools/fake-v35-device.py

jasonacox commented 2 months ago

@jasonacox I guess I'm just an alt account of yours now 🤣 🤣 🤣

No!!! Haha! 😛 @RedFlh just so you know, @uzlonewolf is the mastermind behind all the 3.4 and 3.4 code and 10x smarter than I am. 🙇

Send us the link to your C# project if you release it.

RedFlh commented 2 months ago

Hi Jasonacox, Thanks again for the information and your availability and best regards! Now I'm going on holiday and will be offline!