Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import json
- import requests
- import urllib3
- urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
- iqGatewayIp = ''
- enlightenUsername=''
- enlightenPassword=''
- # Step 1 - Get the info from https://{iqGatewayIp}/info.xml, which includes the firmware/software
- # version and the serial number.
- #
- print("1. Get information from IQ Gateway at http://" + iqGatewayIp + "/info.xml...")
- systemInfo = requests.get('http://' + iqGatewayIp + '/info.xml', verify=False)
- if "<sn>" in systemInfo.text:
- envoyserial = systemInfo.text.split("<sn>")[1].split("</sn>")[0]
- print(" Got IQ Gateway serialnumber '" + envoyserial + "' from http://" + iqGatewayIp + "/info.xml")
- if "<software>" in systemInfo.text:
- envoysoftware = systemInfo.text.split("<software>")[1].split("</software>")[0]
- print(" Got IQ Gateway software version '" + envoysoftware + "' from http://" + iqGatewayIp + "/info.xml")
- # Only continue when the softwareVersion starts with D7.
- #
- if envoysoftware.startswith('D7.'):
- # Step 2 - Use the username and password from your Enlighten account to get the (first)
- # sessionId by executing a POST on https://enlighten.enphaseenergy.com/login/login.json.
- #
- print("\n2. Logging in onto https://enlighten.enphaseenergy.com/login/login.json...");
- data = {
- 'user[email]': enlightenUsername, 'user[password]': enlightenPassword
- }
- response = requests.post('https://enlighten.enphaseenergy.com/login/login.json?', data=data)
- response_data = json.loads(response.text)
- enlighten_sessionId = response_data['session_id']
- print(" Got Enlighten sessionId '" + enlighten_sessionId + "' from https://enlighten.enphaseenergy.com/login/login.json")
- # Step 3 - Use the serial number, username and the sessionId to get the authToken by
- # executing a POST on https://entrez.enphaseenergy.com/tokens
- #
- print("\n3. Getting authToken from https://entrez.enphaseenergy.com/tokens...")
- data = {
- 'serial_num': envoyserial,
- 'username': enlightenUsername,
- 'session_id': enlighten_sessionId
- }
- response = requests.post('https://entrez.enphaseenergy.com/tokens', json=data)
- entrezAuthToken = response.text
- print(" Got authToken '" + entrezAuthToken + "' from https://entrez.enphaseenergy.com/tokens")
- # Step 4 - Use the authToken to get the cookies for the IQ Gateway by executing a GET on
- # https://{iqGatewayIp}/auth/check_jwt.
- #
- print("\n4. Verify authToken on https://" + iqGatewayIp + "/auth/check_jwt...");
- headers = {
- "Authorization": "Bearer " + entrezAuthToken
- }
- response = requests.get('https://' + iqGatewayIp + '/auth/check_jwt', headers=headers, verify=False)
- # Check response, a valid response will look like: <!DOCTYPE html><h2>Valid token.</h2>. If
- # valid, extract the sessionId.
- #
- if "Valid token" in response.text:
- # Extract the sessionId from the cookies.
- #
- iqGateway_sessionId = response.cookies['sessionId']
- print(" Got IQ Gateway sessionId '" + iqGateway_sessionId + "' from https://" + iqGatewayIp + "/auth/check_jwt")
- #
- cookies = dict(sessionid=iqGateway_sessionId)
- # Step 5 - Use the sessionId from the cookie for the other URL's (like
- # https://{iqGatewayIp}/production.json and
- # https://iqGatewayIp}/api/v1/production/inverters/) on the IQ Gateway.
- #
- print("\n5a. Get production info from https://" + iqGatewayIp + "/production.json...")
- iqGateway_production = requests.get('https://' + iqGatewayIp + '/production.json', cookies=cookies, verify=False)
- print(" Got response from https://" + iqGatewayIp + "/production.json: " + iqGateway_production.text);
- print("\n5b. Get inverters info from https://" + iqGatewayIp + "/api/v1/production/inverters/...")
- iqGateway_inverters = requests.get('https://' + iqGatewayIp + '/api/v1/production/inverters/', cookies=cookies, verify=False)
- print(" Got response from https://" + iqGatewayIp + "/api/v1/production/inverters/: " + iqGateway_inverters.text);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement