def fetch(SYS_ID, KEY, USER_ID): """ Expects login details and system id for the Enlighten API. Returns the last known watts reading along with its timestamp. Also warns of abnormal system health. """ # secondary imports here to reduce startup time when this collector is unused. import sys import urllib.request import json from lib import support ERRORLOG.info("whir whir...") # config validation if not ( SYS_ID and KEY and USER_ID and support.is_int(SYS_ID) and support.is_hex(KEY) and support.is_hex(USER_ID) and len(SYS_ID) < 10 and len(KEY) == 32 and len(USER_ID) == 18): sys.exit("Unexpected values in Enlighten config. Aborting.") # setup URL = "https://api.enphaseenergy.com/api/v2/systems/" + SYS_ID + "/summary?key=" + KEY + "&user_id=" + USER_ID request = urllib.request.Request(URL) # fetch try: response = urllib.request.urlopen(request) except urllib.error.URLError as err: sys.exit("Error talking to Enighten: %s" % (err)) # parse result = response.read().decode('utf-8') result = json.loads(result) # warn if we notice poor system health if result['status'] != "normal": ERRORLOG.warn("WARNING: Enlighten reports status: %s " % (result['status'])) # setup timestamp for later content_timestamp = result['last_report_at'] # result print(json.dumps(result, sort_keys=True, indent=4)) watts = str(result['current_power']) return watts, content_timestamp
def fetch(SYS_ID, KEY, USER_ID): """ Expects login details and system id for the Enlighten API. Returns the last known watts reading along with its timestamp. Also warns of abnormal system health. """ # secondary imports here to reduce startup time when this collector is unused. import sys import urllib.request import json from lib import support ERRORLOG.info("whir whir...") # config validation if not (SYS_ID and KEY and USER_ID and support.is_int(SYS_ID) and support.is_hex(KEY) and support.is_hex(USER_ID) and len(SYS_ID) < 10 and len(KEY) == 32 and len(USER_ID) == 18): sys.exit("Unexpected values in Enlighten config. Aborting.") # setup URL = "https://api.enphaseenergy.com/api/v2/systems/" + SYS_ID + "/summary?key=" + KEY + "&user_id=" + USER_ID request = urllib.request.Request(URL) # fetch try: response = urllib.request.urlopen(request) except urllib.error.URLError as err: sys.exit("Error talking to Enighten: %s" % (err)) # parse result = response.read().decode('utf-8') result = json.loads(result) # warn if we notice poor system health if result['status'] != "normal": ERRORLOG.warn("WARNING: Enlighten reports status: %s " % (result['status'])) # setup timestamp for later content_timestamp = result['last_report_at'] # result print(json.dumps(result, sort_keys=True, indent=4)) watts = str(result['current_power']) return watts, content_timestamp
def fetch(KEY, SYS_ID): """ Expects login key and system id for the PVOutput API. Returns the last known watts reading along with its timestamp. Also warns if the API limit is close. """ # secondary imports here to reduce startup time when this collector is unused. import sys import time import urllib.request import json from lib import support ERRORLOG.info("bleep bloop bleep...") # config validation if not (KEY and SYS_ID and support.is_hex(KEY) and support.is_int(SYS_ID) and len(KEY) == 40 and len(SYS_ID) < 10): sys.exit("Error: unexpected values in PVOutput config.") # setup URL = "http://pvoutput.org/service/r2/getstatus.jsp" request = urllib.request.Request(URL) request.add_header('X-Rate-Limit', '1') request.add_header('X-Pvoutput-Apikey', KEY) request.add_header('X-Pvoutput-SystemId', SYS_ID) # fetch try: response = urllib.request.urlopen(request) except urllib.error.URLError as err: sys.exit("Error talking to PvOutput: %s" % (err)) # parse/compile body content = response.read().decode('utf-8') content = content.split(",") pv_keys = [ 'date', 'time', 'watt_hour_total', 'watts', 'watt_hour_used', 'watts_using', 'efficiency', 'temperature', 'voltage' ] content = dict(zip(pv_keys, content)) # compile/parse headers headers = dict(response.info().items()) limit_left = int(headers['X-Rate-Limit-Remaining']) limit_reset = float(headers['X-Rate-Limit-Reset']) limit_reset = time.strftime('%H:%M', time.localtime(limit_reset)) # warn if we notice imminent API throttle if limit_left < 10: ERRORLOG.warn( "WARNING: You are %s requests from the PVOutput API limit. Resets at %s." % (limit_left, limit_reset)) # setup timestamp for later content_timestamp = content['date'] + content['time'] content_timestamp = time.mktime( time.strptime(content_timestamp, "%Y%m%d%H:%M")) # result print(json.dumps(content, sort_keys=True, indent=4)) watts = content['watts'] return watts, content_timestamp
def fetch(KEY, SYS_ID): """ Expects login key and system id for the PVOutput API. Returns the last known watts reading along with its timestamp. Also warns if the API limit is close. """ # secondary imports here to reduce startup time when this collector is unused. import sys import time import urllib.request import json from lib import support ERRORLOG.info("bleep bloop bleep...") # config validation if not ( KEY and SYS_ID and support.is_hex(KEY) and support.is_int(SYS_ID) and len(KEY) == 40 and len(SYS_ID) < 10): sys.exit("Error: unexpected values in PVOutput config.") # setup URL = "http://pvoutput.org/service/r2/getstatus.jsp" request = urllib.request.Request(URL) request.add_header('X-Rate-Limit', '1') request.add_header('X-Pvoutput-Apikey', KEY) request.add_header('X-Pvoutput-SystemId', SYS_ID) # fetch try: response = urllib.request.urlopen(request) except urllib.error.URLError as err: sys.exit("Error talking to PvOutput: %s" % (err)) # parse/compile body content = response.read().decode('utf-8') content = content.split(",") pv_keys = ['date', 'time', 'watt_hour_total', 'watts', 'watt_hour_used', 'watts_using', 'efficiency', 'temperature', 'voltage'] content = dict(zip(pv_keys, content)) # compile/parse headers headers = dict(response.info().items()) limit_left = int(headers['X-Rate-Limit-Remaining']) limit_reset = float(headers['X-Rate-Limit-Reset']) limit_reset = time.strftime('%H:%M', time.localtime(limit_reset)) # warn if we notice imminent API throttle if limit_left < 10: ERRORLOG.warn("WARNING: You are %s requests from the PVOutput API limit. Resets at %s." % (limit_left, limit_reset)) # setup timestamp for later content_timestamp = content['date'] + content['time'] content_timestamp = time.mktime(time.strptime(content_timestamp, "%Y%m%d%H:%M")) # result print(json.dumps(content, sort_keys=True, indent=4)) watts = content['watts'] return watts, content_timestamp