jasonacox / tinytuya

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

Why my Device ID in tinytuya scan and tuya-cli different? #83

Open drbhpark opened 2 years ago

drbhpark commented 2 years ago

Hi! Thank you for your efforts.

I am a bit confused to follow your instruction to handle tuya devices 👍

C:\Windows\system32>tuya-cli wizard ? The API key from tuya.com: qp99stxxxxyk8uf6wca ? The API secret from tuya.com 83146ffe6f0a4xxxxxb5b4295786972 ? Provide a 'virtual ID' of a device currently registered in the app: eb376exxxxxdca582ao8my [ { name: 'Temperature Humidity Sensor', id: 'eb376e8xxxxxa582ao8my', key: 'a7f77fxxxxx963c0' }, { name: 'Temperature Humidity Sensor 2', id: 'eb3eexxxxx891f500gvr', key: 'daffaxxxxxb81b9e' } ]

(base) C:\Windows\system32>python -m tinytuya scan -nocolor

TinyTuya (Tuya device scanner) [1.2.10]

[Loaded devices.json - 2 devices]

Scanning on UDP ports 6666 and 6667 for devices (15 retries)...

Unknown v3.1 Device Product ID = jutTAkekSaIvLkun [Valid payload]: Address = 192.168.219.111, Device ID = 0420029xxxxx22c9789d, Local Key = , Version = 3.1 Status: {'1': False, '2': 0} Unknown v3.3 Device Product ID = key5nck4tavy43jp [Valid payload]: Address = 192.168.219.109, Device ID = 75162802e8xxxxxc1774, Local Key = , Version = 3.3 No Stats for 192.168.219.109: DEVICE KEY required to poll for status

Scan Complete! Found 2 devices.

Here's question. Which device ID should I use to run your first module,

import tinytuya

d = tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE') d.set_version(3.3) data = d.status() print('set_status() result %r' % data)

Thank you !

jasonacox commented 2 years ago

You can use any of the Device IDs on your network:

I noticed you use tuya-cli instead of tinytuya wizard. If you run this you can have tinytuya pull the Tuya local keys and store them and other device info in a devices.json file.

You will need the local key (tinytuya wizard will show these) to be able to use the code you posted. TinyTuya's wizard will also run a series of status() pulls for all of your devices:

python -m tinytuya wizard -nocolor

Fair warning: I see that your devices are temperature sensors. Some of these are designed to send their data only to the Tuya Cloud instead of responding to local queries. Still, give them a try in the d.status() code you posted:

import tinytuya

# First One
d1 = tinytuya.OutletDevice('0420029xxxxx22c9789d', '192.168.219.111', 'LOCAL_KEY_HERE')
d1.set_version(3.1)
data = d1.status()
print('d1 result %r' % data)

# Second One
d2 = tinytuya.OutletDevice('75162802e8xxxxxc1774', '192.168.219.109', 'LOCAL_KEY_HERE')
d2.set_version(3.3)
data = d2.status()
print('d2 result %r' % data)
drbhpark commented 2 years ago

Dear Jason!

Thank you for help. I tried tinytuya wizard. First part was okay but still had time to get polling local devices So I mixed the information gathered from tinytuya wizard and scan to see this result -

import tinytuya

First One

d1 = tinytuya.OutletDevice('04200298dc4f22c9789d', '192.168.219.111', 'a7f77ff86aa963c0') d1.set_version(3.1) data = d1.status() print('d1 result %r' % data)

Second One

d2 = tinytuya.OutletDevice('75162802e868e76c1774', '192.168.219.109', 'daffa1aa3db81b9e') d2.set_version(3.3) data = d2.status() print('d2 result %r' % data)

d1 result {'devId': '04200298dc4f22c9789d', 'dps': {'1': False, '2': 0}} d2 result {'Error': 'Invalid JSON Response from Device', 'Err': '900', 'Payload': ''}


d1 seems to work fine.

I still have problems on Polling local information in tinytuya wizard.

Scanning local network for Tuya devices... 2 local devices discovered

Polling local devices... [Temperature Humidity Sensor] - 0 - Error: No IP found [Temperature Humidity Sensor 2] - 0 - Error: No IP found

Saving device snapshot data to snapshot.json

Done.

I gotta be patient!!!!

jasonacox commented 2 years ago

Upgrade TinyTuya to v1.2.11 and run the wizard again with an additional parameter:

# Upgrade
python -m pip install --upgrade tinytuya

# Run Wizard
python -m tinytuya wizard -nocolor 50 

The 50 tells it to wait longer listening for the devices to broadcast their IP address. You can increase or decrease that number.

d1 result {'devId': '04200298dc4f22c9789d', 'dps': {'1': False, '2': 0}}

Unfortunately, based on the payload for D1 above, it seems that you may not be able to get the sensor data (temp or humidity) from the local network. It looks like it is only sending back DPS 1 and 2 and they look more like switch data points. Look at the tuya-raw.json file generated by the wizard to see what DPS value should be available.