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

library/python cant connect with my device for unknown reason #406

Closed markoloko1223 closed 10 months ago

markoloko1223 commented 10 months ago

so i have the SHLS111W smart switch model (wifi) and whenever i do switch,turn_on() it does not turn on my light, i didnt know why and im kind of a beginner in python so i asked chat gpt for help and it said to enter this into my code:

try:
    # Attempt to control the device
    response = switch.turn_on()
    print(response)  # Print the response received from the device
except Exception as e:
    print(f"Error: {str(e)}")

and i got the error {'Error': 'Unexpected Payload from Device', 'Err': '904', 'Payload': ''} i completed the setup like it said in the documentation and nothing. i made sure that everything is correct. heres my code:

import tinytuya
switch = tinytuya.OutletDevice("nuh uh i aint giving away private info")
try:
    # Attempt to control the device
    response = switch.turn_on()
    print(response)  # Print the response received from the device
except Exception as e:
    print(f"Error: {str(e)}")
uzlonewolf commented 10 months ago

It's fine to snip the device ID and local key, but we really need to see the rest of that tinytuya.OutletDevice() line. Are you setting version=... in it? The correct version needs to be set or it won't work.

markoloko1223 commented 10 months ago

no i dont have version=... in it and how can i obtain it? btw heres the corrected code:

import tinytuya
switch = tinytuya.OutletDevice("bfaab3b4dbe7999840fauu","no",">nEx=u+fu!Y}Cj#O")
data = switch.status() 
print('set_status() result %r' % data)
try:
    # Attempt to control the device
    response = switch.turn_on()
    print(response)  # Print the response received from the device
except Exception as e:
    print(f"Error: {str(e)}")
jasonacox commented 10 months ago

Hi @markoloko1223 - you should see the version when you run the scanner:

python3 -m tinytuya scan

It will look something like this:

Unknown v3.3 Device   Product ID = MShdslm9Uw7Q59nN  [Valid Broadcast]:
    Address = 10.0.x.x   Device ID = xxxxxxxxxxxxx (len:20)  Local Key =  xxxxxxxxx 
    Version = 3.3  Type = default, MAC = xx:xx:xx:xx:xx:xx

An example of how to use the tinytuya library is here:

import tinytuya

# Connect to Device
d = tinytuya.OutletDevice(
    dev_id='DEVICE_ID_HERE',
    address='IP_ADDRESS_HERE',      # Or set to 'Auto' to auto-discover IP address
    local_key='LOCAL_KEY_HERE', 
      version=3.3)                  # This is where you set the version

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

# Turn On
d.turn_on()

# Turn Off
d.turn_off()
markoloko1223 commented 10 months ago

still the same error

markoloko1223 commented 10 months ago

even with version included

markoloko1223 commented 10 months ago

nevermind it works now thank you so much