From 4cc244bcc2fbc01680a122a1597437987f268e76 Mon Sep 17 00:00:00 2001 From: John Paul Lorenti Date: Sat, 27 Jan 2018 18:10:49 -0500 Subject: [PATCH 1/5] Make devicename optional, only print devices otherwise, add parentheses for print statements, comment out change_ac_state --- sensibo_client.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/sensibo_client.py b/sensibo_client.py index 149213b..b6b0a6f 100644 --- a/sensibo_client.py +++ b/sensibo_client.py @@ -39,17 +39,18 @@ def pod_change_ac_state(self, podUid, currentAcState, propertyToChange, newValue import argparse parser = argparse.ArgumentParser(description='Sensibo client example parser') parser.add_argument('apikey', type = str) - parser.add_argument('deviceName', type = str) + parser.add_argument('--deviceName', type = str) args = parser.parse_args() client = SensiboClientAPI(args.apikey) devices = client.devices() - print "-" * 10, "devices", "-" * 10 - print devices + print ("-" * 10, "devices", "-" * 10) + print (devices) - uid = devices[args.deviceName] - ac_state = client.pod_ac_state(uid) - print "-" * 10, "AC State of %s" % args.deviceName, "_" * 10 - print ac_state + if(args.deviceName): + uid = devices[args.deviceName] + ac_state = client.pod_ac_state(uid) + print ("-" * 10, "AC State of %s" % args.deviceName, "_" * 10) + print (ac_state) - client.pod_change_ac_state(uid, ac_state, "on", not ac_state['on']) +# client.pod_change_ac_state(uid, ac_state, "on", not ac_state['on']) From ba9a9fd56fdcc31c5c6cec1542ca41f0ddb64656 Mon Sep 17 00:00:00 2001 From: John Paul Lorenti Date: Sat, 27 Jan 2018 19:21:13 -0500 Subject: [PATCH 2/5] showState and showMeasurement command line options, catch Request HTTP errors --- sensibo_client.py | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/sensibo_client.py b/sensibo_client.py index b6b0a6f..f1903c1 100644 --- a/sensibo_client.py +++ b/sensibo_client.py @@ -38,19 +38,39 @@ def pod_change_ac_state(self, podUid, currentAcState, propertyToChange, newValue if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description='Sensibo client example parser') - parser.add_argument('apikey', type = str) - parser.add_argument('--deviceName', type = str) + parser.add_argument('apikey', type = str, help='Obtain from https://home.sensibo.com/me/api') + parser.add_argument('--deviceName', type = str, help='Device name') + parser.add_argument('--showState', action='store_true',help='Display the AC state') + parser.add_argument('--showMeasurement', action='store_true',help='Display the sensor measurements') args = parser.parse_args() + #print(args) client = SensiboClientAPI(args.apikey) - devices = client.devices() - print ("-" * 10, "devices", "-" * 10) - print (devices) - + try: + devices = client.devices() + print ("-" * 10, "devices", "-" * 10) + print (devices) + except requests.exceptions.RequestException as exc: + print ("Request failed with message %s" % exc) + exit(1) + if(args.deviceName): - uid = devices[args.deviceName] - ac_state = client.pod_ac_state(uid) - print ("-" * 10, "AC State of %s" % args.deviceName, "_" * 10) - print (ac_state) + try: + uid = devices[args.deviceName] + #Default to showing AC state since a device was specified without a clear reason + if(not args.showState and not args.showMeasurement): + args.showState=True + + if(args.showState and uid): + ac_state = client.pod_ac_state(uid) + print ("-" * 10, "AC State of %s" % args.deviceName, "-" * 10) + print (ac_state) + if(args.showMeasurement and uid): + pod_measurement = client.pod_measurement(uid) + print ("-" * 10, "Measurement of %s" % args.deviceName, "-" * 10) + print (pod_measurement) -# client.pod_change_ac_state(uid, ac_state, "on", not ac_state['on']) + #client.pod_change_ac_state(uid, ac_state, "on", not ac_state['on']) + except requests.exceptions.RequestException as exc: + print ("Request failed with message %s" % exc) + exit(1) From 3f3a070ac26fe26f595644ddec3cd6b657a01a9d Mon Sep 17 00:00:00 2001 From: batinthestacks Date: Sat, 27 Jan 2018 20:52:10 -0500 Subject: [PATCH 3/5] New function: tempFromMeasurements. New switches: allDevices,showTempMeasurement,unitF,unitC --- sensibo_client.py | 66 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/sensibo_client.py b/sensibo_client.py index f1903c1..1f7e9ff 100644 --- a/sensibo_client.py +++ b/sensibo_client.py @@ -34,43 +34,73 @@ def pod_ac_state(self, podUid): def pod_change_ac_state(self, podUid, currentAcState, propertyToChange, newValue): self._patch("/pods/%s/acStates/%s" % (podUid, propertyToChange), json.dumps({'currentAcState': currentAcState, 'newValue': newValue})) + +def tempFromMeasurements(measurement): + if (args.unitC): + return (str(measurement["temperature"]) + 'C' ) + elif (args.unitF): + return (str(round(measurement["temperature"]*(9/5)+32,1)) + 'F') if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description='Sensibo client example parser') parser.add_argument('apikey', type = str, help='Obtain from https://home.sensibo.com/me/api') parser.add_argument('--deviceName', type = str, help='Device name') + parser.add_argument('--allDevices', action='store_true', help='Query all devices') parser.add_argument('--showState', action='store_true',help='Display the AC state') parser.add_argument('--showMeasurement', action='store_true',help='Display the sensor measurements') + parser.add_argument('--showTempMeasurement', action='store_true',help='Display the sensor temperature') + parser.add_argument('--unitF', action='store_true',help='Use Fahrenheit') + parser.add_argument('--unitC', action='store_true',help='Use Celsius') args = parser.parse_args() #print(args) + if (not args.unitF and not args.unitC): + args.unitC=True client = SensiboClientAPI(args.apikey) try: devices = client.devices() - print ("-" * 10, "devices", "-" * 10) - print (devices) + if(not args.deviceName and not args.allDevices): + print ("-" * 10, "devices", "-" * 10) + print (devices) except requests.exceptions.RequestException as exc: print ("Request failed with message %s" % exc) - exit(1) + devices = {'living room A/C': 'ntZFS9SF', 'bedroom A/C': 'wwyyJdeY', "Megan's A/C": 'f3uEc9GG'} + #exit(1) - if(args.deviceName): + if(args.deviceName and not args.allDevices): + uidList = [devices[args.deviceName]] + deviceNameByUID = {devices[args.deviceName]:args.deviceName} + elif(args.allDevices): + uidList = devices.values() + deviceNameByUID = {v:k for k,v in devices.items()} + else: + uidList = False + + #A specific device or all devices were requested + if (uidList): + #Default to showing AC state since no particular information request was given + if(not args.showState and not args.showMeasurement and not args.showTempMeasurement): + args.showState=True + try: - uid = devices[args.deviceName] - #Default to showing AC state since a device was specified without a clear reason - if(not args.showState and not args.showMeasurement): - args.showState=True - - if(args.showState and uid): - ac_state = client.pod_ac_state(uid) - print ("-" * 10, "AC State of %s" % args.deviceName, "-" * 10) - print (ac_state) - if(args.showMeasurement and uid): - pod_measurement = client.pod_measurement(uid) - print ("-" * 10, "Measurement of %s" % args.deviceName, "-" * 10) - print (pod_measurement) + for uid in uidList: + #print ("UID {}".format(uid)) + #continue + if(args.showState): + ac_state = client.pod_ac_state(uid) + print ("-" * 10, "AC State of %s" % deviceNameByUID[uid], "-" * 10) + print (ac_state) + if(args.showMeasurement or args.showTempMeasurement): + pod_measurement = client.pod_measurement(uid) + if(args.showMeasurement): + print ("-" * 10, "Measurement of %s" % deviceNameByUID[uid], "-" * 10) + print (pod_measurement) + if(args.showTempMeasurement): + print ("-" * 10, "Temperature in %s" % deviceNameByUID[uid], "-" * 10) + print (tempFromMeasurements(pod_measurement[0])) - #client.pod_change_ac_state(uid, ac_state, "on", not ac_state['on']) + #client.pod_change_ac_state(uid, ac_state, "on", not ac_state['on']) except requests.exceptions.RequestException as exc: print ("Request failed with message %s" % exc) exit(1) From fe2b6f9f865d8b1165be4217efd4e6a38dd0d3d7 Mon Sep 17 00:00:00 2001 From: batinthestacks Date: Sat, 27 Jan 2018 22:13:55 -0500 Subject: [PATCH 4/5] terse message support. dual unit temp output. Add togglePower switch --- sensibo_client.py | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/sensibo_client.py b/sensibo_client.py index 1f7e9ff..794bfa2 100644 --- a/sensibo_client.py +++ b/sensibo_client.py @@ -36,10 +36,24 @@ def pod_change_ac_state(self, podUid, currentAcState, propertyToChange, newValue json.dumps({'currentAcState': currentAcState, 'newValue': newValue})) def tempFromMeasurements(measurement): + unitId='' if (args.unitC): - return (str(measurement["temperature"]) + 'C' ) + if(not args.terse): + unitId='C' + return (str(measurement["temperature"]) + unitId ) elif (args.unitF): - return (str(round(measurement["temperature"]*(9/5)+32,1)) + 'F') + if(not args.terse): + unitId='F' + return (str(round(measurement["temperature"]*(9/5)+32,1)) + unitId) + elif (args.unitDual): + if(not args.terse): + unitId='F' + unitId2='C' + unitIdSep=' / ' + else: + unitId2='' + unitIdSep=' / ' + return (str(round(measurement["temperature"]*(9/5)+32,1)) + unitId + unitIdSep +str(measurement["temperature"]) + unitId2) if __name__ == "__main__": import argparse @@ -48,13 +62,16 @@ def tempFromMeasurements(measurement): parser.add_argument('--deviceName', type = str, help='Device name') parser.add_argument('--allDevices', action='store_true', help='Query all devices') parser.add_argument('--showState', action='store_true',help='Display the AC state') - parser.add_argument('--showMeasurement', action='store_true',help='Display the sensor measurements') + parser.add_argument('--togglePower', action='store_true',help='Toggle the AC power') + parser.add_argument('--showMeasurements', action='store_true',help='Display the sensor measurements') parser.add_argument('--showTempMeasurement', action='store_true',help='Display the sensor temperature') parser.add_argument('--unitF', action='store_true',help='Use Fahrenheit') parser.add_argument('--unitC', action='store_true',help='Use Celsius') + parser.add_argument('--unitDual', action='store_true',help='Use F/C') + parser.add_argument('--terse', action='store_true',help='Keep the response short') args = parser.parse_args() #print(args) - if (not args.unitF and not args.unitC): + if (not args.unitF and not args.unitC and not args.unitDual): args.unitC=True client = SensiboClientAPI(args.apikey) @@ -80,27 +97,31 @@ def tempFromMeasurements(measurement): #A specific device or all devices were requested if (uidList): #Default to showing AC state since no particular information request was given - if(not args.showState and not args.showMeasurement and not args.showTempMeasurement): + if(not args.showState and not args.showMeasurements and not args.showTempMeasurement): args.showState=True try: for uid in uidList: #print ("UID {}".format(uid)) - #continue - if(args.showState): + #continue + if(args.terse and args.allDevices): + print(deviceNameByUID[uid]) + if(args.showState or args.togglePower): ac_state = client.pod_ac_state(uid) - print ("-" * 10, "AC State of %s" % deviceNameByUID[uid], "-" * 10) + if(args.showState): + not args.terse and print ("-" * 10, "AC State of %s" % deviceNameByUID[uid], "-" * 10) print (ac_state) - if(args.showMeasurement or args.showTempMeasurement): + if(args.togglePower): + client.pod_change_ac_state(uid, ac_state, "on", not ac_state['on']) + if(args.showMeasurements or args.showTempMeasurement): pod_measurement = client.pod_measurement(uid) - if(args.showMeasurement): - print ("-" * 10, "Measurement of %s" % deviceNameByUID[uid], "-" * 10) + if(args.showMeasurements): + not args.terse and print ("-" * 10, "Measurement of %s" % deviceNameByUID[uid], "-" * 10) print (pod_measurement) if(args.showTempMeasurement): - print ("-" * 10, "Temperature in %s" % deviceNameByUID[uid], "-" * 10) + not args.terse and print ("-" * 10, "Temperature in %s" % deviceNameByUID[uid], "-" * 10) print (tempFromMeasurements(pod_measurement[0])) - #client.pod_change_ac_state(uid, ac_state, "on", not ac_state['on']) except requests.exceptions.RequestException as exc: print ("Request failed with message %s" % exc) exit(1) From b65d3af92e4b92c53040bbf4a1d67f7edfe2d721 Mon Sep 17 00:00:00 2001 From: batinthestacks Date: Sat, 27 Jan 2018 22:18:51 -0500 Subject: [PATCH 5/5] remove hard coded device Id fallbacks --- sensibo_client.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sensibo_client.py b/sensibo_client.py index 794bfa2..96012c0 100644 --- a/sensibo_client.py +++ b/sensibo_client.py @@ -82,8 +82,7 @@ def tempFromMeasurements(measurement): print (devices) except requests.exceptions.RequestException as exc: print ("Request failed with message %s" % exc) - devices = {'living room A/C': 'ntZFS9SF', 'bedroom A/C': 'wwyyJdeY', "Megan's A/C": 'f3uEc9GG'} - #exit(1) + exit(1) if(args.deviceName and not args.allDevices): uidList = [devices[args.deviceName]]