jasonacox / tinytuya

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

Error from Tuya server: permission deny #369

Open Electrik-rich546456 opened 1 year ago

Electrik-rich546456 commented 1 year ago

python -m tinytuya wizard says TinyTuya Setup Wizard [1.6.4]

Existing settings:
 <hidden>

Error from Tuya server: permission deny I have logged into my iot.tuya.com account and it says IoT Core trial expired... Does that mean i can't use tynytuya anymore @jasonacox

jasonacox commented 1 year ago

Hi @Electrik-rich546456 , it means your Tuya IoT account has expired. TinyTuya is an open source project and is not part of the company, Tuya. You can continue to use TinyTuya with the local keys you already have but to get new device information, but to use wizard you will need to create a new IoT account with Tuya. I recommend following the instructions here: https://github.com/jasonacox/tinytuya/files/8145832/Tuya.IoT.API.Setup.pdf and make sure you not sign up for a individual developer or commercial trial account (see illustration):

image

TinyTuya Setup Wizard [1.6.4]

This is an older version. I would also recommend upgrading pip install --upgrade tinytuya.

Electrik-rich546456 commented 1 year ago

Thanks i created a new project in the iot tuya site and linked it to my smart lifa app as you said and now the wizard finds the devices however a lot of them are reporting [Bedside Light] - 0 - Error: No IP found how do i scan the local network again maybe with some more aggressive options ?

Electrik-rich546456 commented 1 year ago

I just updated to TinyTuya Setup Wizard [1.12.8] It seems to be working ok again

Electrik-rich546456 commented 1 year ago

Hi @jasonacox Hows it going ? I have a strange issue going on. If i attempt to send and commands to my lights it works just fine. But i get this error

 {'Error': 'Network Error: Unable to Connect', 'Err': '901', 'Payload': None}
Traceback (most recent call last):
  File "/home/pi/bin/new tuya poject/./menue.py", line 42, in <module>
    main()
  File "/home/pi/bin/new tuya poject/./menue.py", line 23, in main
    house_sw.lights("Living Room Light") 
  File "/home/pi/bin/new tuya poject/house_sw.py", line 21, in lights
    if(status['dps']['20'] == True):
KeyError: 'dps'

I know i spelt menu wrong lol. I suspect it has to do with the smart life app on my phone. How do prevent the dps key error from closing the script.

I have the script monitoring 2 gpio ports on my rpi 2 and they are connected to a wireless kinetic switch. So i can run the turn on command from anywhere in my flat.

Electrik-rich546456 commented 1 year ago

code im using

#!/usr/bin/env python3
#from threading import Thread
import tinytuya
import json
#from threading import Thread

with open('snapshot.json') as json_file:
     data = json.load(json_file)

def lights(name, *num):
    for item in data["devices"]:
        if item["name"] == name:
            break
#    print("\nSwitching: %s" % item["name"])
    d = tinytuya.BulbDevice(item["id"], item["ip"], item["key"])
    d.set_version(float(item["ver"]))
    d.set_socketPersistent(True)
#    print(d)
    status = d.status()
    print(status)
    if(status['dps']['20'] == True):
        ##print("its on Turning off")
        d.turn_off()
    elif(status['dps']['20'] == False):
        ##print("its off Turing on")
        d.turn_on()
        if name == ("The R ornament"):
            payload=d.generate_payload(tinytuya.CONTROL, {'20': True, '21': 'music', '24': '010302500211', '25': '16323202005a0384006400000000323202005a038403e8000000004646020032025803e800000000505002001e02ee03e800000000323202000003e803e800000000', '26': 0, '34': False, '42': '010100016464000003e8007803e800f003e8003c03e800b403e8012c03e8'})
            d._send_receive(payload)
    print("------------------")
    status = d.status()
    print(status)
jasonacox commented 1 year ago

I suspect it has to do with the smart life app on my phone.

Do you mean that you may have the SmartLife app open when this script runs, so it blocks it? If so, you can adjust your code to gracefully fail, something like this:

#!/usr/bin/env python3
#from threading import Thread
import tinytuya
import json
#from threading import Thread

with open('snapshot.json') as json_file:
     data = json.load(json_file)

def lights(name, *num):
    for item in data["devices"]:
        if item["name"] == name:
            break
#    print("\nSwitching: %s" % item["name"])
    d = tinytuya.BulbDevice(item["id"], item["ip"], item["key"])
    d.set_version(float(item["ver"]))
    d.set_socketPersistent(True)
#    print(d)
    status = d.status()
    print(status)
    if 'dps" in status:
       if(status['dps']['20'] == True):
           ##print("its on Turning off")
           d.turn_off()
       elif(status['dps']['20'] == False):
           ##print("its off Turing on")
           d.turn_on()
           if name == ("The R ornament"):
               payload=d.generate_payload(tinytuya.CONTROL, {'20': True, '21': 'music', '24': '010302500211', '25': '16323202005a0384006400000000323202005a038403e8000000004646020032025803e800000000505002001e02ee03e800000000323202000003e803e800000000', '26': 0, '34': False, '42': '010100016464000003e8007803e800f003e8003c03e800b403e8012c03e8'})
               d._send_receive(payload)
       print("------------------")
       status = d.status()
       print(status)
    else:
       print("device appears to be locked - ignoring")
uzlonewolf commented 1 year ago

That can still blow up if the bulb accepts the command but does not return anything (as status() can return None) or if '20' is not in the response. It also shouldn't be calling private functions (d._send_receive()) like that. My suggestion:

#!/usr/bin/env python3
#from threading import Thread
import tinytuya
import json
#from threading import Thread

with open('snapshot.json') as json_file:
    data = json.load(json_file)

def lights(name, *num):
    item = None
    for dev in data["devices"]:
        if dev["name"] == name:
            item = dev
            break
    if not item:
        print('Device not found!')
        return False
#    print("\nSwitching: %s" % item["name"])
    d = tinytuya.BulbDevice(item["id"], item["ip"], item["key"], version=item["ver"])
    d.set_socketPersistent(True)
#    print(d)
    status = d.status()
    print(status)
    if status and "dps" in status:
        if '20' in status['dps']:
            if(status['dps']['20'] == True):
                ##print("its on Turning off")
                d.turn_off()
            elif(status['dps']['20'] == False):
                ##print("its off Turing on")
                d.turn_on()
                if name == ("The R ornament"):
                    d.set_multiple_values({'20': True, '21': 'music', '24': '010302500211', '25': '16323202005a0384006400000000323202005a038403e8000000004646020032025803e800000000505002001e02ee03e800000000323202000003e803e800000000', '26': 0, '34': False, '42': '010100016464000003e8007803e800f003e8003c03e800b403e8012c03e8'})
            print("------------------")
            status = d.status()
            print(status)
    else:
        print("device appears to be locked - ignoring")