diff --git a/enphase.py b/enphase.py index dfa5f08..b6521a4 100644 --- a/enphase.py +++ b/enphase.py @@ -171,18 +171,42 @@ while True: session.headers.update({"Authorization":"Bearer "+enphasetoken}) details = enphaseurl + "/production.json?details=1" inverteruri= enphaseurl + "/api/v1/production/inverters" - mainresponse = session.get(details,verify=False).json() - inverterresponse = session.get(inverteruri,verify=False).json() - #print(json.dumps(mainresponse,indent=1)) - for inverterdata in inverterresponse: + mainresponse = session.get(details,verify=False) + if mainresponse.status_code == 401: + print("Authentication Error, please renew enphase token at https://entrez.enphaseenergy.com/entrez_tokens") + quit() + print("Status code:", mainresponse.status_code) + print("Headers:", mainresponse.headers) + print("Raw text:", mainresponse.text[:500]) + try: + mainresponseresult = mainresponse.json() + print("Parsed JSON:\n", json.dumps(mainresponseresult, indent=2)) + except Exception as e: + print("Failed to parse JSON:", e) + quit() + + inverterresponse = session.get(inverteruri,verify=False) + if inverterresponse.status_code == 401: + print("Authentication Error, please renew enphase token at https://entrez.enphaseenergy.com/entrez_tokens") + quit() + print("Status code:", inverterresponse.status_code) + print("Headers:", inverterresponse.headers) + print("Raw text:", inverterresponse.text[:500]) + try: + inverterresponseresult = inverterresponse.json() + print("Parsed JSON:\n", json.dumps(inverterresponseresult, indent=2)) + except Exception as e: + print("Failed to parse JSON:", e) + quit() + for inverterdata in inverterresponseresult: myinverter = inverter(inverterdata['serialNumber'],inverterdata['lastReportDate'],inverterdata['devType'],inverterdata['lastReportWatts'],inverterdata['maxReportWatts']) inverterlist.append(myinverter) #print(json.dumps(response.json(),indent=1)) #for myinverter in inverterlist: #pass # myinverter.print() - print(json.dumps(mainresponse,indent=1)) - maindata = ElectricalData(mainresponse) + #print(json.dumps(mainresponse,indent=1)) + maindata = ElectricalData(mainresponseresult) influxsession = requests.Session() influxsession.verify = False influxsession.auth = (infdbuser, infdbpass)