Advertisement
Guest User

Example: Read from Enphase IQGateway - Firmware D7.x, using Enphase Enlighten Account.

a guest
Oct 29th, 2022
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | Source Code | 0 0
  1. #!/usr/bin/python3
  2. import json
  3. import requests
  4. import urllib3
  5. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  6.  
  7. iqGatewayIp = ''
  8. enlightenUsername=''
  9. enlightenPassword=''
  10.  
  11. # Step 1 - Get the info from https://{iqGatewayIp}/info.xml, which includes the firmware/software
  12. # version and the serial number.
  13. #
  14. print("1. Get information from IQ Gateway at http://" + iqGatewayIp + "/info.xml...")
  15.  
  16. systemInfo = requests.get('http://' + iqGatewayIp + '/info.xml', verify=False)
  17. if "<sn>" in systemInfo.text:
  18.     envoyserial = systemInfo.text.split("<sn>")[1].split("</sn>")[0]
  19.     print("  Got IQ Gateway serialnumber '" + envoyserial + "' from http://" + iqGatewayIp + "/info.xml")
  20.  
  21. if "<software>" in systemInfo.text:
  22.     envoysoftware = systemInfo.text.split("<software>")[1].split("</software>")[0]
  23.     print("  Got IQ Gateway software version '" + envoysoftware + "' from http://" + iqGatewayIp + "/info.xml")
  24.  
  25. # Only continue when the softwareVersion starts with D7.
  26. #
  27. if envoysoftware.startswith('D7.'):
  28.  
  29.     # Step 2 - Use the username and password from your Enlighten account to get the (first)
  30.     # sessionId by executing a POST on https://enlighten.enphaseenergy.com/login/login.json.
  31.     #
  32.     print("\n2. Logging in onto https://enlighten.enphaseenergy.com/login/login.json...");
  33.  
  34.     data = {
  35.         'user[email]': enlightenUsername, 'user[password]': enlightenPassword
  36.     }
  37.     response = requests.post('https://enlighten.enphaseenergy.com/login/login.json?', data=data)
  38.     response_data = json.loads(response.text)
  39.     enlighten_sessionId = response_data['session_id']
  40.     print("  Got Enlighten sessionId '" + enlighten_sessionId + "' from https://enlighten.enphaseenergy.com/login/login.json")
  41.  
  42.     # Step 3 - Use the serial number, username and the sessionId to get the authToken by
  43.     # executing a POST on https://entrez.enphaseenergy.com/tokens
  44.     #
  45.     print("\n3. Getting authToken from https://entrez.enphaseenergy.com/tokens...")
  46.  
  47.     data = {
  48.         'serial_num': envoyserial,
  49.         'username': enlightenUsername,
  50.         'session_id': enlighten_sessionId
  51.     }
  52.     response = requests.post('https://entrez.enphaseenergy.com/tokens', json=data)
  53.     entrezAuthToken = response.text
  54.     print("  Got authToken '" + entrezAuthToken + "' from https://entrez.enphaseenergy.com/tokens")
  55.  
  56.     # Step 4 - Use the authToken to get the cookies for the IQ Gateway by executing a GET on
  57.     # https://{iqGatewayIp}/auth/check_jwt.
  58.     #
  59.     print("\n4. Verify authToken on https://" + iqGatewayIp + "/auth/check_jwt...");
  60.  
  61.     headers = {
  62.         "Authorization": "Bearer " + entrezAuthToken
  63.     }
  64.     response = requests.get('https://' + iqGatewayIp + '/auth/check_jwt', headers=headers, verify=False)
  65.  
  66.     # Check response, a valid response will look like: <!DOCTYPE html><h2>Valid token.</h2>. If
  67.     # valid, extract the sessionId.
  68.     #
  69.     if "Valid token" in response.text:
  70.  
  71.         # Extract the sessionId from the cookies.
  72.         #
  73.         iqGateway_sessionId = response.cookies['sessionId']
  74.         print("  Got IQ Gateway sessionId '" + iqGateway_sessionId + "' from https://" + iqGatewayIp + "/auth/check_jwt")
  75.  
  76.         #
  77.         cookies = dict(sessionid=iqGateway_sessionId)
  78.  
  79.         # Step 5 - Use the sessionId from the cookie for the other URL's (like
  80.         # https://{iqGatewayIp}/production.json and
  81.         # https://iqGatewayIp}/api/v1/production/inverters/) on the IQ Gateway.
  82.         #
  83.         print("\n5a. Get production info from https://" + iqGatewayIp + "/production.json...")
  84.         iqGateway_production = requests.get('https://' + iqGatewayIp + '/production.json', cookies=cookies, verify=False)
  85.         print("  Got response from https://" + iqGatewayIp + "/production.json: " + iqGateway_production.text);
  86.  
  87.         print("\n5b. Get inverters info from https://" + iqGatewayIp + "/api/v1/production/inverters/...")
  88.         iqGateway_inverters = requests.get('https://' + iqGatewayIp + '/api/v1/production/inverters/', cookies=cookies, verify=False)
  89.         print("  Got response from https://" + iqGatewayIp + "/api/v1/production/inverters/: " + iqGateway_inverters.text);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement