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.01k stars 177 forks source link

Tried installing on python 3 #122

Closed JAbelP closed 2 years ago

JAbelP commented 2 years ago

in _find_latest_available_vc_ver return self.find_available_vc_vers()[-1] IndexError: list index out of range

jasonacox commented 2 years ago

Hi @Jpinales97 - python3 is the default for TinyTuya and is tested with each build on these versions: https://github.com/jasonacox/tinytuya/actions/runs/2050567608

What version are you using? Also, please paste what you were running to get that error (that function you mention is not part of TinyTuya). It could be that you have something corrupt in your python install.

JAbelP commented 2 years ago

I just needed to update pip!

but when I tried running the file (py3 -m dinnerRoomLight.py (the code will be below)) I got this error Traceback (most recent call last): File "C:\Python3\lib\runpy.py", line 184, in _run_module_as_main mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "C:\Python3\lib\runpy.py", line 110, in _get_module_details import(pkg_name) File "C:\Users\Helquin\Documents\Code\JavaScript\React\homewebsite\backend\dinnerRoomLight.py", line 1, in import tinytuya File "C:\Python3\lib\site-packages\tinytuya__init__.py", line 116, in import pyaes # https://github.com/ricmoo/pyaes ModuleNotFoundError: No module named 'pyaes'

Traceback (most recent call last): File "C:\Python3\lib\runpy.py", line 184, in _run_module_as_main mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "C:\Python3\lib\runpy.py", line 110, in _get_module_details import(pkg_name) File "C:\Users\Helquin\Documents\Code\JavaScript\React\homewebsite\backend\dinnerRoomLight.py", line 1, in from tuya_iot import TuyaOpenAPI File "C:\Python3\lib\site-packages\tuya_iot__init__.py", line 2, in from .device import TuyaDevice, TuyaDeviceListener, TuyaDeviceManager File "C:\Python3\lib\site-packages\tuya_iot\device.py", line 11, in from .openmq import TuyaOpenMQ File "C:\Python3\lib\site-packages\tuya_iot\openmq.py", line 13, in from Crypto.Cipher import AES ModuleNotFoundError: No module named 'Crypto'

but running pip freeze you can see that both are installed certifi==2021.10.8 charset-normalizer==2.0.12 click==8.0.3 colorama==0.4.4 crypto==1.4.1 distlib==0.3.4 filelock==3.4.2 Flask==2.0.2 flux-led==0.28.22 idna==3.3 itsdangerous==2.0.1 Jinja2==3.0.3 MarkupSafe==2.0.1 Naked==0.1.31 numpy==1.22.3 paho-mqtt==1.6.1 pandas==1.4.1 platformdirs==2.5.0 pyaes==1.6.1 pycryptodome==3.14.1 python-dateutil==2.8.2 pytz==2022.1 PyYAML==6.0 requests==2.27.1 shellescape==3.8.1 six==1.16.0 tinytuya==1.3.1 tuya-iot-py-sdk==0.6.6 urllib3==1.26.9 virtualenv==20.13.1 webcolors==1.11.1 websocket-client==1.3.2 Werkzeug==2.0.3

jasonacox commented 2 years ago

Something is not right with your python installation. Try this:

# Try to upgrade tinytuya
py3 -m pip install --upgrade tinytuya

# Try to upgrade pycryptodome - used by tinytuya for encryption
py3 -m pip install --upgrade pycryptodome

# Have tinytuya scan your network
py3 -m tinytuya scan -nocolor

# Try your code again
py3 dinnerRoomLight.py

Windows doesn't always play well with pyaes and Crypto so you could try to uninstall those.

JAbelP commented 2 years ago

I finally got that part running, but when I try to scan > poll devices

Scanning local network for Tuya devices (retry 29 times)... 6 local devices discovered

Polling local devices... [Dining room east] - 0 - Error: No IP found [Dining room north] - 0 - Error: No IP found [Dining room west] - 0 - Error: No IP found [Dining room south] - 0 - Error: No IP found

(two of the ones not listed are my roomate's so it makes sense that two are not on the list)

I also went into my router and found that the IP address being shown in the app does not match the one found in the router (router shows 192.167.1.229 while the tuya-raw.json shows 99.127.43.38)

import tinytuya d = tinytuya.BulbDevice('xxxx','192.168.1.229','xxxx') b =d.status() print(b)

(backend) C:\Users\Helquin\Documents\Code\JavaScript\React\homewebsite\backend>py3 dinnerRoomLight.py {'Error': 'Unexpected Payload from Device', 'Err': '904', 'Payload': ''}

jasonacox commented 2 years ago

Hi @Jpinales97 - One thing to note, the scan actually just listens for the Tuya UDP broadcast packets from the devices. This is how Tuya does local device discovery (and is used in the Smart Life app). Some things can preven this:

The scan is useful in helping discover the IP addresses of the devices and matching those with their Device IDs. You can do that manually if your route provides IP address for the devices, but it does require that you know the right Device ID and LocalKey to use for each one.

{'Error': 'Unexpected Payload from Device', 'Err': '904', 'Payload': ''}

This can be caused by invalid version, invalid ID and LocalKey. I notice you are not setting a version in your code. This will cause it to default to protocol 3.1 which is highly unlikely for SmartBulbs. I would try this:

import tinytuya
# tinytuya.set_debug(color=False) # Optional debug mode I would try

d = tinytuya.BulbDevice('xxxx','192.168.1.229','xxxx')
d.set_version(3.3)
b =d.status()
print(b)
JAbelP commented 2 years ago

Nothing should be blocking the ports but I did have the App open; I'll go ahead and try the code with the version specified by the app

JAbelP commented 2 years ago

also should I be using the 192.168.1.228 IP or should I use the 99.127.43.38 IP Address?

jasonacox commented 2 years ago

The IP address in tuya-raw.json is the public IP address that Tuya Cloud sees when your devices connect to it. You will want to use the local private network addresses for local control (e.g. 192.168.1.228). Try to close the app and run this:

python -m tinytuya scan -nocolor
JAbelP commented 2 years ago

awesome it all worked out, just for reference, how can you tell what version an item is?

jasonacox commented 2 years ago

That's great news! The scan will print the version of the device along with the Address and Device ID (it will also include the Name and Local Key if you have a devices.json file in the same directory). Here is an example 3.3 device:

Chandelier  Product ID = qcgkaqmaivuwfwz4  [Valid payload]:
    Address = 10.0.1.6,  Device ID = xxxxxx,  Local Key = yyyyyy,  Version = 3.3
    Status: {u'1': True, u'3': 166, u'101': False}