I am running a python file at command prompt and it is working fine. Execution of the file as
python myfile.py -d device01 -s
on and this is working. when I import
the same class from another file, it's not working.
Source code:
#!/usr/bin/env python3
import pytuya
import sys
import getopt
import json
def smartlite(argv):
device = ''
state = ''
try:
opts, args = getopt.getopt(argv,"hd:s:",["help", "device=", "state="])
except getopt.GetoptError:
print (sys.argv[0], '-d <device> -s <state>')
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print (sys.argv[0], '-d <device> -s <state>')
sys.exit()
elif opt in ("-d", "--device"):
device = arg
elif opt in ("-s", "--state"):
state = arg
filename = "p.json"
if filename:
with open(filename, 'r') as f:
datafile = json.load(f)
print ('Device is', device)
print ('State is', state)
print (datafile["devices"][device]["uuid"])
print (datafile["devices"][device]["key"])
print (datafile["devices"][device]["ip"])
d = pytuya.OutletDevice(datafile["devices"][device]["uuid"], datafile["devices"][device]["ip"], datafile["devices"][device]["key"])
data = d.status() # NOTE this does NOT require a valid key
print('Dictionary %r' % data)
print('state (bool, true is ON) %r' % data['dps']['1']) # Show status of first controlled switch on device
# Toggle switch state
switch_state = data['dps']['1']
data = d.set_status(not switch_state) # This requires a valid key
if data:
print('set_status() result %r' % data)
sys.exit(0)
if __name__ == "__main__":
smartlite(sys.argv[1:])
The above code is working when I run at command prompt as
\.> python myfile.py -d device01 -s on
when I am calling the above code from another file I am getting an error.
Importing the library of smartlite.
from sourcefolder.pytuya import smartlite # this is good.
try:
smartlite.get("-d Lite01 -s on")
#os.system('python pytuya/mypytuya.py -d Lite01 -s on')
except sr.UnknownValueError:
print('error')
except sr.RequestError as e:
print('faile'.format(e))
error when executing.
('Device is', '')
('State is', '')
Traceback (most recent call last):
File "voicetotext.py", line 22, in <module>
smartlite("-d Lite01 -s on")
File "/home/respeaker/voice-engine/examples/pytuya/mypytuya.py", line 34, in smartlite
print (datafile["devices"][device]["uuid"])
KeyError: ''