|
| 1 | +# Solar Forecast API Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This API provides solar power forecast data based on the given site information and handles authorization with Enphase solar inverters. It has been developed using FastAPI and includes the following key endpoints: |
| 6 | + |
| 7 | +1. `/forecast/`: Generate solar power forecasts. |
| 8 | +2. `/solar_inverters/enphase/auth_url`: Retrieve the Enphase authorization URL. |
| 9 | +3. `/solar_inverters/enphase/token_and_id`: Obtain an Enphase access token and system ID. |
| 10 | + |
| 11 | +## Endpoints |
| 12 | + |
| 13 | +### 1. Generate Solar Power Forecast |
| 14 | + |
| 15 | +- **Endpoint:** `/forecast/` |
| 16 | +- **Method:** `POST` |
| 17 | +- **Description:** This endpoint generates a solar power forecast for a specified site and timestamp. It optionally includes real-time data from inverters if available. |
| 18 | + |
| 19 | +#### Request Body: |
| 20 | + |
| 21 | +- **ForecastRequest:** |
| 22 | + - `site` (PVSite, required): The site details for which the forecast is to be generated. |
| 23 | + - `timestamp` (string, optional): The timestamp for the forecast in ISO 8601 format. If not provided, the current time will be used. |
| 24 | + |
| 25 | +- **PVSite:** |
| 26 | + - `latitude` (float, required): The latitude of the site. Must be between -90 and 90. |
| 27 | + - `longitude` (float, required): The longitude of the site. Must be between -180 and 180. |
| 28 | + - `capacity_kwp` (float, required): The capacity of the site in kilowatts peak (kWp). Must be a positive value. |
| 29 | + - `tilt` (float, optional, default=35): The tilt angle of the solar panels in degrees. Must be between 0 and 90. |
| 30 | + - `orientation` (float, optional, default=180): The orientation angle of the solar panels in degrees, measured from north. Must be between 0 and 360. |
| 31 | + - `inverter_type` (string, optional): The type of inverter used. Accepted values: `"enphase"`, `"solis"`, `"givenergy"`, `"solarman"`, or `None`. |
| 32 | + |
| 33 | +#### Response: |
| 34 | + |
| 35 | +- **200 OK** |
| 36 | + - **JSON Structure:** |
| 37 | + ```json |
| 38 | + { |
| 39 | + "timestamp": "2023-08-14 10:00:00", |
| 40 | + "predictions": { |
| 41 | + "power_kw": [values], |
| 42 | + "power_kw_no_live_pv": [values] |
| 43 | + } |
| 44 | + } |
| 45 | + ``` |
| 46 | + - `timestamp` (string): The formatted timestamp of the forecast. |
| 47 | + - `predictions` (dictionary): The forecasted power data. If inverter data is available, it will also include `power_kw_no_live_pv` without inverter data. |
| 48 | + |
| 49 | +### 2. Retrieve Enphase Authorization URL |
| 50 | + |
| 51 | +- **Endpoint:** `/solar_inverters/enphase/auth_url` |
| 52 | +- **Method:** `GET` |
| 53 | +- **Description:** This endpoint returns the authorization URL required to initiate the OAuth flow for Enphase inverters. |
| 54 | + |
| 55 | +#### Response: |
| 56 | + |
| 57 | +- **200 OK** |
| 58 | + - **JSON Structure:** |
| 59 | + ```json |
| 60 | + { |
| 61 | + "auth_url": "https://api.enphaseenergy.com/oauth/authorize?client_id=..." |
| 62 | + } |
| 63 | + ``` |
| 64 | + - `auth_url` (string): The URL to redirect the user to for Enphase authorization. |
| 65 | + |
| 66 | +### 3. Obtain Enphase Access Token and System ID |
| 67 | + |
| 68 | +- **Endpoint:** `/solar_inverters/enphase/token_and_id` |
| 69 | +- **Method:** `POST` |
| 70 | +- **Description:** This endpoint exchanges an authorization code for an access token and retrieves the system ID of the Enphase solar inverter. |
| 71 | + |
| 72 | +#### Request Body: |
| 73 | + |
| 74 | +- **TokenRequest:** |
| 75 | + - `redirect_url` (string, required): The URL to which the user was redirected after Enphase authorization, containing the authorization code. |
| 76 | + |
| 77 | +#### Response: |
| 78 | + |
| 79 | +- **200 OK** |
| 80 | + - **JSON Structure:** |
| 81 | + ```json |
| 82 | + { |
| 83 | + "access_token": "abc123...", |
| 84 | + "enphase_system_id": "123456789" |
| 85 | + } |
| 86 | + ``` |
| 87 | + - `access_token` (string): The access token for Enphase API. |
| 88 | + - `enphase_system_id` (string): The system ID of the Enphase solar inverter. |
| 89 | + |
| 90 | +- **400 Bad Request** |
| 91 | + - **Error Message:** |
| 92 | + ```json |
| 93 | + { |
| 94 | + "detail": "Invalid redirect URL" |
| 95 | + } |
| 96 | + ``` |
| 97 | + - **Description:** The request was not properly formatted or did not contain the necessary authorization code. |
| 98 | + |
| 99 | +## Error Handling |
| 100 | + |
| 101 | +All endpoints will return appropriate HTTP status codes. Common responses include: |
| 102 | + |
| 103 | +- **200 OK:** The request was successful. |
| 104 | +- **400 Bad Request:** The request was malformed or contained invalid data. |
| 105 | +- **500 Internal Server Error:** An unexpected error occurred on the server. |
| 106 | + |
| 107 | +## Example Usage |
| 108 | + |
| 109 | +### Generate Solar Power Forecast |
| 110 | + |
| 111 | +**Request:** |
| 112 | + |
| 113 | +```bash |
| 114 | +curl -X POST "http://localhost:8000/forecast/" -H "Content-Type: application/json" -d '{ |
| 115 | + "site": { |
| 116 | + "latitude": 37.7749, |
| 117 | + "longitude": -122.4194, |
| 118 | + "capacity_kwp": 5.0, |
| 119 | + "tilt": 30, |
| 120 | + "orientation": 180, |
| 121 | + "inverter_type": "enphase" |
| 122 | + }, |
| 123 | + "timestamp": "2023-08-14T10:00:00Z" |
| 124 | +}' |
| 125 | +``` |
| 126 | + |
| 127 | +**Response:** |
| 128 | + |
| 129 | +```json |
| 130 | +{ |
| 131 | + "timestamp": "2023-08-14 10:00:00", |
| 132 | + "predictions": { |
| 133 | + "power_kw": [values], |
| 134 | + "power_kw_no_live_pv": [values] |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +### Retrieve Enphase Authorization URL |
| 140 | + |
| 141 | +**Request:** |
| 142 | + |
| 143 | +```bash |
| 144 | +curl -X GET "http://localhost:8000/solar_inverters/enphase/auth_url" |
| 145 | +``` |
| 146 | + |
| 147 | +**Response:** |
| 148 | + |
| 149 | +```json |
| 150 | +{ |
| 151 | + "auth_url": "https://api.enphaseenergy.com/oauth/authorize?client_id=..." |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +### Obtain Enphase Access Token and System ID |
| 156 | + |
| 157 | +**Request:** |
| 158 | + |
| 159 | +```bash |
| 160 | +curl -X POST "http://localhost:8000/solar_inverters/enphase/token_and_id" -H "Content-Type: application/json" -d '{ |
| 161 | + "redirect_url": "https://yourapp.com/callback?code=abc123" |
| 162 | +}' |
| 163 | +``` |
| 164 | + |
| 165 | +**Response:** |
| 166 | + |
| 167 | +```json |
| 168 | +{ |
| 169 | + "access_token": "abc123...", |
| 170 | + "enphase_system_id": "123456789" |
| 171 | +} |
| 172 | +``` |
0 commit comments