From c54d16d63372dd97808a5c95630f6c899d48019e Mon Sep 17 00:00:00 2001 From: Tim van Werkhoven Date: Sun, 25 Nov 2018 15:55:55 +0100 Subject: [PATCH 1/3] Init version to push to influxdb --- tplink_smartplug.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tplink_smartplug.py b/tplink_smartplug.py index 4012a55..7c4af76 100755 --- a/tplink_smartplug.py +++ b/tplink_smartplug.py @@ -75,6 +75,12 @@ def decrypt(string): group = parser.add_mutually_exclusive_group(required=True) group.add_argument("-c", "--command", metavar="", help="Preset command to send. Choices are: "+", ".join(commands), choices=commands) group.add_argument("-j", "--json", metavar="", help="Full JSON string of command to send") +parser.add_argument('--influxdb', type=str, metavar=("URI", "database"), default=None, + nargs=2, help='If command is "energy", push to influxdb. URI should point to influxdb, e.g. [http/https]://:. Database: e.g. smarthome.') +parser.add_argument('--influxdb_energy', type=str, metavar="query", default=None, + help='query to store energy as Joule, e.g. energy,type=elec,device=hs110-1 this will be appended with (as int)') +parser.add_argument('--influxdb_power', type=str, metavar="query", default=None, + help='query to store power as Watt, e.g. power,type=elec,device=hs110-1 this will be appended with (as float)') args = parser.parse_args() @@ -101,3 +107,33 @@ def decrypt(string): except socket.error: quit("Cound not connect to host " + ip + ":" + str(port)) +if (args.command == "energy") and (args.influxdb != None): + import requests + import json + + # Get total_wh from json response + energy_response = decrypt(data[4:]) + energy_wh = json.loads(energy_response)['emeter']['get_realtime']['total_wh'] + energy_joule = int(energy_wh)*3600 + + power_mW = json.loads(energy_response)['emeter']['get_realtime']['power_mw'] + power_W = float(power_mW)/1000.0 + + # Build URI and query + # Something like req_url = "http://localhost:8086/write?db=smarthometest&precision=s" + req_url = args.influxdb[0]+"/write?db="+args.influxdb[1]+"&precision=s" + # Something like post_data = "water,type=usage,device=sensus value=1" + post_data = "" + if (args.influxdb_energy != None): + post_data = args.influxdb_energy+" value="+str(energy_joule) + if (args.influxdb_power != None): + post_data += "\n"+args.influxdb_power+" value="+str(power_W) + + # Post data to influxdb + try: + httpresponse = requests.post(req_url, data=post_data, verify=False, timeout=5) + if (httpresponse.status_code != 204): + print "Push to influxdb failed: " + str(httpresponse.status_code) + " - " + str(httpresponse.text) + except requests.exceptions.Timeout as e: + print "Update failed due to timeout. Is influxdb running?" + From 7ca67e2f554a3c8ab994da0a2b503ec613395d90 Mon Sep 17 00:00:00 2001 From: Tim van Werkhoven Date: Sun, 25 Nov 2018 17:19:41 +0100 Subject: [PATCH 2/3] Documentation minor --- tplink_smartplug.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tplink_smartplug.py b/tplink_smartplug.py index 7c4af76..64d7871 100755 --- a/tplink_smartplug.py +++ b/tplink_smartplug.py @@ -122,14 +122,15 @@ def decrypt(string): # Build URI and query # Something like req_url = "http://localhost:8086/write?db=smarthometest&precision=s" req_url = args.influxdb[0]+"/write?db="+args.influxdb[1]+"&precision=s" - # Something like post_data = "water,type=usage,device=sensus value=1" + + # Something like post_data = "water,type=usage,device=devicename value=1" post_data = "" if (args.influxdb_energy != None): post_data = args.influxdb_energy+" value="+str(energy_joule) if (args.influxdb_power != None): post_data += "\n"+args.influxdb_power+" value="+str(power_W) - # Post data to influxdb + # Post data to influxdb, check for obvious errors try: httpresponse = requests.post(req_url, data=post_data, verify=False, timeout=5) if (httpresponse.status_code != 204): From a094843c64bbfe62780f0e74412c090ad15a9f6d Mon Sep 17 00:00:00 2001 From: Tim van Werkhoven Date: Sun, 25 Nov 2018 17:22:26 +0100 Subject: [PATCH 3/3] Minor code comments --- tplink_smartplug.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tplink_smartplug.py b/tplink_smartplug.py index 64d7871..6fbf1e7 100755 --- a/tplink_smartplug.py +++ b/tplink_smartplug.py @@ -111,19 +111,17 @@ def decrypt(string): import requests import json - # Get total_wh from json response + # Get total_wh and power_mw from json response energy_response = decrypt(data[4:]) energy_wh = json.loads(energy_response)['emeter']['get_realtime']['total_wh'] energy_joule = int(energy_wh)*3600 - power_mW = json.loads(energy_response)['emeter']['get_realtime']['power_mw'] power_W = float(power_mW)/1000.0 # Build URI and query # Something like req_url = "http://localhost:8086/write?db=smarthometest&precision=s" - req_url = args.influxdb[0]+"/write?db="+args.influxdb[1]+"&precision=s" - # Something like post_data = "water,type=usage,device=devicename value=1" + req_url = args.influxdb[0]+"/write?db="+args.influxdb[1]+"&precision=s" post_data = "" if (args.influxdb_energy != None): post_data = args.influxdb_energy+" value="+str(energy_joule)