Update decode-status.py to python 3.x

This commit is contained in:
Theo Arends 2020-07-04 17:39:56 +02:00
parent 046b43b967
commit b8afa11743

View File

@ -20,7 +20,7 @@
Requirements: Requirements:
- Python - Python
- pip json pycurl - pip json requests
Instructions: Instructions:
Execute command with option -d to retrieve status report from device or Execute command with option -d to retrieve status report from device or
@ -42,11 +42,10 @@ Example:
import io import io
import os.path import os.path
import json import json
import pycurl import requests
import urllib2 import urllib
from sys import exit from sys import exit
from optparse import OptionParser from optparse import OptionParser
from StringIO import StringIO
a_on_off = ["OFF","ON "] a_on_off = ["OFF","ON "]
@ -146,11 +145,14 @@ a_setoption = [[
"Distinct MQTT topics per device for Zigbee", "Distinct MQTT topics per device for Zigbee",
"Disable non-json MQTT response", "Disable non-json MQTT response",
"Enable light fading at start/power on", "Enable light fading at start/power on",
"Set PWM Mode from regular PWM to ColorTemp control","", "Set PWM Mode from regular PWM to ColorTemp control",
"Keep uncompressed rules in memory to avoid CPU load of uncompressing at each tick", "Keep uncompressed rules in memory to avoid CPU load of uncompressing at each tick",
"Implement simpler MAX6675 protocol instead of MAX31855", "Implement simpler MAX6675 protocol instead of MAX31855",
"","", "Enable Wifi",
"","","","", "Enable Ethernet (ESP32)",
"Set Baud rate for TuyaMCU serial communication (0 = 9600 or 1 = 115200)",
"Rotary encoder uses rules instead of light control",
"","","",
"","","","", "","","","",
"","","","", "","","","",
"","","","" "","","",""
@ -225,25 +227,19 @@ parser.add_option("-f", "--file", metavar="FILE",
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if (options.device): if (options.device):
buffer = StringIO()
loginstr = "" loginstr = ""
if options.password is not None: if options.password is not None:
loginstr = "user={}&password={}&".format(urllib2.quote(options.username), urllib2.quote(options.password)) loginstr = "user={}&password={}&".format(urllib.parse.quote(options.username), urllib.parse.quote(options.password))
url = str("http://{}/cm?{}cmnd=status%200".format(options.device, loginstr)) url = str("http://{}/cm?{}cmnd=status%200".format(options.device, loginstr))
c = pycurl.Curl() res = requests.get(url)
c.setopt(c.URL, url) obj = json.loads(res.content)
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
body = buffer.getvalue()
obj = json.loads(body)
else: else:
jsonfile = options.jsonfile jsonfile = options.jsonfile
with open(jsonfile, "r") as fp: with open(jsonfile, "r") as fp:
obj = json.load(fp) obj = json.load(fp)
def StartDecode(): def StartDecode():
print ("\n*** decode-status.py v20200627 by Theo Arends and Jacek Ziolkowski ***") print ("\n*** decode-status.py v20200704 by Theo Arends and Jacek Ziolkowski ***")
# print("Decoding\n{}".format(obj)) # print("Decoding\n{}".format(obj))