Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetches live Enphase data at the moment it is called #66

Merged
merged 5 commits into from
Mar 4, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions quartz_solar_forecast/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

ssl._create_default_https_context = ssl._create_unverified_context

# User needs to add their Enphase API details
ENPHASE_API_KEY = 'user_enphase_api_key'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we make this envionrment variables?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we make this envionrment variables?

Sure, will make the required change and add a .env.example file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we make this envionrment variables?

Should i add a .env file in /Open-Source-Quartz-Solar-Forecast/quartz_solar_forecast
or simply in /Open-Source-Quartz-Solar-Forecast

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we make this envionrment variables?

I have added the changes in my PR for environment variables, please review it. Thank you

ENPHASE_USER_ID = 'user_enphase_user_id'

def get_nwp(site: PVSite, ts: datetime, nwp_source: str = "icon") -> xr.Dataset:
"""
Expand Down Expand Up @@ -111,20 +114,45 @@ def format_nwp_data(df: pd.DataFrame, nwp_source:str, site: PVSite):
)
return data_xr

def get_enphase_data(enphase_user_id: str, enphase_api_key: str) -> float:
peterdudfield marked this conversation as resolved.
Show resolved Hide resolved
"""
Get live PV generation data from Enphase API

def make_pv_data(site: PVSite, ts: pd.Timestamp) -> xr.Dataset:
:param enphase_user_id: User ID for Enphase API
:param enphase_api_key: API Key for Enphase API
:return: Live PV generation in Watt-hours, assumes to be a floating-point number
"""
Make fake PV data for the site
url = f'https://api.enphaseenergy.com/api/v2/systems/{enphase_user_id}/summary'
headers = {'Authorization': f'Bearer {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

def make_pv_data(site: PVSite, ts: pd.Timestamp, use_enphase_data: bool = False) -> xr.Dataset:
peterdudfield marked this conversation as resolved.
Show resolved Hide resolved
"""
Make PV data by combining Enphase live data and fake PV data

Later we could add PV history here

:param site: the PV site
:param ts: the timestamp of the site
:return: The fake PV dataset in xarray form
:param use_enphase_data: Flag indicating whether to use live Enphase data or not
:return: The combined PV dataset in xarray form
"""

# make fake pv data, this is where we could add history of a pv system
generation_wh = [[np.nan]]
if use_enphase_data:
# Fetch live Enphase data and store it in live_generation_wh
live_generation_wh = get_enphase_data(ENPHASE_USER_ID, ENPHASE_API_KEY)
else:
live_generation_wh = np.nan # Default value if not using live Enphase data

# Combine live Enphase data with fake PV data, this is where we could add history of a pv system
generation_wh = [[live_generation_wh]]
lon = [site.longitude]
lat = [site.latitude]
timestamp = [ts]
Expand Down
Loading