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 183 forks source link

device.status() gives sometimes a weird reply #565

Open grosjo opened 1 week ago

grosjo commented 1 week ago

calling d.status() returns sometimes some weird answer (protocol 3.4)

image

my code:

    d=tinytuya.Device(dev[1],dev[2],dev[3])        // dev[] include the ID/key/ip of the device                     
    d.set_socketRetryLimit(2)                                   
    d.set_socketTimeout(2)                                       
    d.set_sendWait(1)                                                                                             
    d.set_dpsUsed({"1": None})                                                                                    
    d.set_version(3.4)                                                                                            
    data = d.status()                                                                                             
jasonacox commented 1 week ago

That's correct, especially if you are just monitoring for status changes. The device will not always send back the full DPS payload so you need to expect that you may not get every DPS every time. You need to add error handling to your python script to handle that. The code snip you sent doesn't seem to be the full code (monitor.py?).

grosjo commented 6 days ago

This is the complete code

ct = datetime.now()                                                                                               
print("Current time = ",ct.strftime("%H %M"))                                                                     

ch = int(ct.strftime("%H"))                                                                                       
cm = ct.strftime("%M")                                                                                            
chh = int(ct.strftime("%H")) + float(ct.strftime("%M"))/60.0                                                      

print("Current hour = ",chh) 

for dev in (devices):                                                                                 

  print("\nTesting ",dev[0]," Day time = ",dev[4]," Night time = ",dev[5])                            

  tobe = False                                                                                        
  if((chh > dev[5]) or (chh < dev[4])):                                                               
    tobe = True                                                                                       
  try:                                                                                                            
    d=tinytuya.Device(dev[1],dev[2],dev[3])                                                                       
    d.set_socketRetryLimit(2)                                      # set retry count limit [default 5]            
    d.set_socketTimeout(2)                                         # set connection timeout in seconds [default 5]
    d.set_sendWait(1)                                                                                             
    d.set_dpsUsed({"1": None})                                                                                    
    d.set_version(3.4)                                                                                            

    data = d.status()                                                                                             
    print(data)                                                                                                   

    asis = data['dps'][dev[6]]
    print("To be = ",tobe," As is =",asis)

    if(asis != tobe):
      print("Switching ",dev[0])                                                                                  
      d.set_value(dev[6],tobe)                                                                                    
  except:                                                                                                         
    print("Error connecting ",dev[0])                                                                             

print("Done")  
jasonacox commented 5 days ago

Possibly try something like this:

from datetime import datetime
import tinytuya

ct = datetime.now()                                                                                               
print("Current time = ", ct.strftime("%H %M"))                                                                     

ch = int(ct.strftime("%H"))                                                                                       
cm = ct.strftime("%M")                                                                                            
chh = int(ct.strftime("%H")) + float(ct.strftime("%M")) / 60.0                                                      

print("Current hour = ", chh) 

for dev in devices:                                                                                 

    print("\nTesting ", dev[0], " Day time = ", dev[4], " Night time = ", dev[5])                            

    tobe = False                                                                                        
    if (chh > dev[5]) or (chh < dev[4]):                                                               
        tobe = True                                                                                       
    try:                                                                                                            
        d = tinytuya.Device(dev[1], dev[2], dev[3])                                                                       
        d.set_socketRetryLimit(2)  # set retry count limit [default 5]            
        d.set_socketTimeout(2)     # set connection timeout in seconds [default 5]
        d.set_sendWait(1)                                                                                             
        d.set_dpsUsed({"1": None})                                                                                    
        d.set_version(3.4)                                                                                            

        data = d.status()                                                                                             
        print(data)                                                                                                   

        # Check if 'dps' exists and contains the required key
        if 'dps' in data and dev[6] in data['dps']:
            asis = data['dps'][dev[6]]
            print("To be = ", tobe, " As is =", asis)

            if asis != tobe:
                print("Switching ", dev[0])                                                                                  
                d.set_value(dev[6], tobe)
        else:
            print(f"DPS data missing or key {dev[6]} not found for device {dev[0]}")
    except Exception as e:                                                                                                         
        print(f"Error connecting to {dev[0]}: {e}")                                                                             

print("Done")