-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* updated api to v4 * added missing brackets * changed varnames * ocf_style coding * access token generator * removed the redundant import * example env format * authorization url format * added venv to gitignore --------- Co-authored-by: root <root@aryansdellg15>
- Loading branch information
1 parent
ada7bf0
commit 9facc2d
Showing
4 changed files
with
45 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ scripts/datapipes/UK_PV_metadata.csv | |
scripts/datapipes/nwp_data | ||
quartz_solar_forecast/data | ||
.env | ||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,47 @@ | ||
import requests | ||
import os | ||
|
||
def get_enphase_data(enphase_user_id: str, enphase_api_key: str) -> float: | ||
from dotenv import load_dotenv | ||
|
||
ENPHASE_CLIENT_ID = os.getenv('ENPHASE_CLIENT_ID') | ||
ENPHASE_CLIENT_SECRET = os.getenv('ENPHASE_CLIENT_SECRET') | ||
ENPHASE_API_KEY = os.getenv('ENPHASE_API_KEY') | ||
|
||
def get_enphase_access_token(): | ||
""" | ||
Get live PV generation data from Enphase API | ||
Obtain an access token for the Enphase API using the Client Credentials Grant flow. | ||
""" | ||
url = "https://api.enphaseenergy.com/oauth/token" | ||
headers = { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Authorization": f"Basic {(ENPHASE_CLIENT_ID + ':' + ENPHASE_CLIENT_SECRET).encode().decode('utf-8')}", | ||
} | ||
data = { | ||
"grant_type": "client_credentials", | ||
"scope": "read" | ||
} | ||
|
||
response = requests.post(url, headers=headers, data=data) | ||
response.raise_for_status() | ||
return response.json()["access_token"] | ||
|
||
:param enphase_user_id: User ID for Enphase API | ||
:param enphase_api_key: API Key for Enphase API | ||
def get_enphase_data(enphase_system_id: str) -> float: | ||
""" | ||
Get live PV generation data from Enphase API v4 | ||
:param enphase_system_id: System ID for Enphase API | ||
:return: Live PV generation in Watt-hours, assumes to be a floating-point number | ||
""" | ||
url = f'https://api.enphaseenergy.com/api/v2/systems/{enphase_user_id}/summary' | ||
headers = {'Authorization': f'Bearer {enphase_api_key}'} | ||
url = f'https://api.enphaseenergy.com/api/v4/{enphase_system_id}/summary' | ||
headers = { | ||
'Authorization': f'Bearer {get_enphase_access_token()}', | ||
'key': ENPHASE_API_KEY | ||
} | ||
|
||
response = requests.get(url, headers=headers) | ||
data = response.json() | ||
|
||
# Extracting live generation data assuming it's in Watt-hours | ||
live_generation_wh = data['current_power']['power'] | ||
|
||
return live_generation_wh |