-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolarEdge.py
More file actions
349 lines (276 loc) · 13.4 KB
/
SolarEdge.py
File metadata and controls
349 lines (276 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import time as pytime
from dataclasses import dataclass
from zoneinfo import ZoneInfo
from typing import List, Dict
import datetime
from datetime import timedelta, datetime, timezone, time
import requests
import keyring
import api_keys
import SolarPlatform
import GeoCode
SOLAREDGE_BASE_URL = 'https://monitoringapi.solaredge.com/v2'
SOLAREDGE_SITE_URL = 'https://monitoring.solaredge.com/solaredge-web/p/site/'
@dataclass(frozen=True)
class SolarEdgeKeys:
account_key : str
api_key : str
def fetch_solaredge_keys():
try:
account_key = keyring.get_password("solaredge", "account_key")
api_key = keyring.get_password("solaredge", "api_key")
if any(key is None for key in [account_key, api_key]):
raise ValueError("Missing SolarEdge key(s) in keyring.")
return SolarEdgeKeys(account_key, api_key)
except Exception as e:
print(f"Error fetching SolarEdge keys: {e}")
return None
#SOLAREDGE_KEYS = fetch_solaredge_keys()
SOLAREDGE_SLEEP = 0.2
SOLAREDGE_KEYS = SolarEdgeKeys(api_keys.SOLAREDGE_V2_ACCOUNT_KEY, api_keys.SOLAREDGE_V2_API_KEY)
SOLAREDGE_HEADERS = {
"X-API-Key": SOLAREDGE_KEYS.api_key,
"Accept": "application/json",
"X-Account-Key": SOLAREDGE_KEYS.account_key,
}
class SolarEdgePlatform(SolarPlatform.SolarPlatform):
@classmethod
def get_vendorcode(cls):
return "SE"
@classmethod
@SolarPlatform.disk_cache(SolarPlatform.CACHE_EXPIRE_WEEK)
def get_sites_list(cls):
url = f'{SOLAREDGE_BASE_URL}/sites'
params = {"page": 1, "sites-in-page": 500}
all_sites = []
while True:
cls.log("Fetching all sites from SolarEdge API...")
response = requests.get(url, headers=SOLAREDGE_HEADERS, params=params)
response.raise_for_status()
sites = response.json()
for site in sites:
all_sites.append(site)
if len(sites) < params["sites-in-page"]:
break
params["page"] += 1
return all_sites
@classmethod
def get_coordinates(cls, site):
"""Get coordinates for a site: full address, then street name fallback."""
# Extract location components
location = site['location']
address = location['address']
zip_code = location['zip']
# Case 1: Full address (with street number)
full_address = f"{address}, {zip_code}"
lat, lon = GeoCode.geocode_address(full_address)
if lat and lon:
lat = float(lat)
lon = float(lon)
return lat, lon
# Case 2: Street name only
street_parts = address.split(maxsplit=1)
if len(street_parts) > 1:
street_name = street_parts[1]
street_only = f"{street_name}, {zip_code}"
lat, lon = GeoCode.geocode_address(street_only)
if lat and lon:
lat = float(lat)
lon = float(lon)
return lat, lon
# If both fail, go based on zip
return SolarPlatform.get_coordinates(zip_code)
@classmethod
def get_sites_map(cls) -> Dict[str, SolarPlatform.SiteInfo]:
sites = cls.get_sites_list()
sites_dict = {}
for site in sites:
raw_site_id = site.get('siteId')
inverters = cls.get_inverters(raw_site_id)
#Skip sites with no inverters
if inverters == []:
continue
site_url = SOLAREDGE_SITE_URL + str(raw_site_id)
site_id = cls.add_vendorcodeprefix(raw_site_id)
zipcode = site['location']['zip']
name = site.get('name')
latitude, longitude = cls.get_coordinates(site)
site_info = SolarPlatform.SiteInfo(site_id, name, site_url, zipcode, latitude, longitude)
sites_dict[site_id] = site_info
return sites_dict
@classmethod
@SolarPlatform.disk_cache(SolarPlatform.cache_expire_month())
def get_devices(cls, raw_site_id):
url = f'{SOLAREDGE_BASE_URL}/sites/{raw_site_id}/devices'
params = {"types": ["BATTERY", "INVERTER"]}
cls.log(f"Fetching Inverter / battery inventory data from SolarEdge API for site {raw_site_id}.")
pytime.sleep(SOLAREDGE_SLEEP)
response = requests.get(url, headers=SOLAREDGE_HEADERS, params=params)
response.raise_for_status()
devices = response.json()
return devices
@classmethod
def delete_device_cache(cls, site_id):
"""Delete the cached device data (batteries and inverters) for a specific SolarEdge site."""
raw_site_id = cls.strip_vendorcodeprefix(site_id)
cache_key = f"get_devices_(<class 'SolarEdge.SolarEdgePlatform'>, '{raw_site_id}')_{{}}"
if cache_key in SolarPlatform.cache:
del SolarPlatform.cache[cache_key]
#Sort by created_time so that the order is stable.
@classmethod
def get_inverters(cls, raw_site_id):
devices = cls.get_devices(raw_site_id)
inverters = [device for device in devices if device.get('type') == 'INVERTER' and device.get('active') == True]
sorted_data = sorted(inverters, key=lambda x: x['createdAt'])
return sorted_data
@classmethod
def get_batteries(cls, raw_site_id):
devices = cls.get_devices(raw_site_id)
batteries = [device for device in devices if device.get('type') == 'BATTERY' and device.get('active') == True]
return batteries
@classmethod
@SolarPlatform.disk_cache(SolarPlatform.CACHE_EXPIRE_HOUR * 4)
def get_battery_state_of_energy(cls, raw_site_id, serial_number):
end_time = datetime.utcnow() #Fixme
start_time = end_time - timedelta(minutes=15)
url = f'{SOLAREDGE_BASE_URL}/sites/{raw_site_id}/storage/{serial_number}/state-of-energy'
params = {'from': start_time.isoformat() + 'Z', 'to': end_time.isoformat() + 'Z',
'resolution': 'QUARTER_HOUR', 'unit': 'PERCENTAGE'}
pytime.sleep(SOLAREDGE_SLEEP)
cls.log(f"Fetching battery State of Energy from SolarEdge API for site {raw_site_id} and battery {serial_number}.")
response = requests.get(url, headers=SOLAREDGE_HEADERS, params=params)
response.raise_for_status()
soe_data = response.json().get('values', [])
latest_value = next((entry['value'] for entry in reversed(
soe_data) if entry['value'] is not None), None)
return latest_value
@classmethod
def get_batteries_soe(cls, site_id):
raw_site_id = cls.strip_vendorcodeprefix(site_id)
batteries = cls.get_batteries(raw_site_id)
battery_states = []
for battery in batteries:
serial_number = battery.get('serialNumber')
soe = cls.get_battery_state_of_energy(raw_site_id, serial_number)
soe_pct = soe * 100 if soe is not None else 0
battery_states.append({'serialNumber': serial_number, 'model': battery.get(
'model'), 'stateOfEnergy': soe_pct})
return battery_states
@classmethod
@SolarPlatform.disk_cache(SolarPlatform.CACHE_EXPIRE_WEEK)
def _get_inverter_production(cls, raw_site_id, reference_time, inverter_id):
formatted_begin_time = reference_time.isoformat(timespec='seconds').replace('+00:00', 'Z')
end_time = reference_time + timedelta(minutes=15)
formatted_end_time = end_time.isoformat(timespec='seconds').replace('+00:00', 'Z')
url = SOLAREDGE_BASE_URL + f'/sites/{raw_site_id}/inverters/{inverter_id}/power'
params = {'from': formatted_begin_time , 'to': formatted_end_time,
'resolution': 'QUARTER_HOUR', 'unit': 'KW'}
cls.log(f"Fetching production from SolarEdge API for site: {raw_site_id} inverter: {inverter_id} at {formatted_begin_time}.")
pytime.sleep(SOLAREDGE_SLEEP)
response = requests.get(url, headers=SOLAREDGE_HEADERS, params=params)
response.raise_for_status()
json = response.json().get('values', [])
return json
@classmethod
def get_inverter_production(cls, raw_site_id, reference_time, inverter_id):
powers = cls._get_inverter_production(raw_site_id, reference_time, inverter_id)
power = powers[0].get('value', 0.0)
if power is None:
power = 0.0
power = round(power, 2)
return power
# Extract the last 2 digits before the dash and all digits after the dash from SolarEdge serial numbers
@classmethod
def extract_last_two_and_after_dash(cls, serial):
parts = serial.split("-")
if len(parts) >= 2: # Ensure there's a dash and parts after it
return parts[0][-2:] + "-" + parts[1]
return parts[0][-4:] # If no dash, just return last 4 digits
@classmethod
def get_production(cls, site_id, reference_time) -> Dict[str, float]:
raw_site_id = cls.strip_vendorcodeprefix(site_id)
inverters = cls.get_inverters(raw_site_id)
productions = {}
for inverter in inverters:
serial_number = inverter.get('serialNumber')
power = cls.get_inverter_production(raw_site_id, reference_time, serial_number)
sub_ser = cls.extract_last_two_and_after_dash(serial_number)
productions[sub_ser] = power
return productions
@classmethod
@SolarPlatform.disk_cache(SolarPlatform.CACHE_EXPIRE_WEEK)
def get_site_energy(cls, site_id, start_date, end_date):
raw_site_id = cls.strip_vendorcodeprefix(site_id)
# Get local timezone from cache, defaulting to SolarPlatform.DEFAULT_TIMEZONE
tz = ZoneInfo(SolarPlatform.cache.get('TimeZone', SolarPlatform.DEFAULT_TIMEZONE))
# Convert dates to 6 AM local start and 23:59:59 local end, then to UTC
start_local = datetime.combine(start_date, time(6, 0, 0), tzinfo=tz)
end_local = datetime.combine(end_date, time(23, 59, 59), tzinfo=tz)
start_utc = start_local.astimezone(timezone.utc)
end_utc = end_local.astimezone(timezone.utc)
# Format as ISO 8601 with seconds precision and Z
formatted_start = start_utc.isoformat(timespec='seconds').replace('+00:00', 'Z')
formatted_end = end_utc.isoformat(timespec='seconds').replace('+00:00', 'Z')
# Construct API URL and parameters
url = SOLAREDGE_BASE_URL + f'/sites/{raw_site_id}/energy'
params = {
'from': formatted_start,
'to': formatted_end,
'resolution': 'DAY'
}
# Log the exact URL for debugging
full_url = requests.Request('GET', url, headers=SOLAREDGE_HEADERS, params=params).prepare().url
cls.log(f"Fetching energy from SolarEdge API for site: {raw_site_id} with URL: {full_url}")
pytime.sleep(1) #Longer sleep for this expensive request, but not all day because we have a lot to gather ;-)
try:
response = requests.get(url, headers=SOLAREDGE_HEADERS, params=params)
response.raise_for_status()
json_data = response.json()
values = json_data.get('values', [])
if not values:
cls.log(f"Empty data returned for site {raw_site_id} from {formatted_start} to {formatted_end}")
return values # Only return if successful and valid
except Exception as e:
cls.log(f"Production request failed for site {raw_site_id}: {e}")
raise
@classmethod
def convert_alert_to_standard(cls, alert):
if alert == "SITE_COMMUNICATION_FAULT":
return SolarPlatform.AlertType.NO_COMMUNICATION
if alert == "INVERTER_BELOW_THRESHOLD_LIMIT":
return SolarPlatform.AlertType.PRODUCTION_ERROR
if alert == "PANEL_COMMUNICATION_FAULT":
return SolarPlatform.AlertType.PANEL_ERROR
else:
return SolarPlatform.AlertType.CONFIG_ERROR
@classmethod
@SolarPlatform.disk_cache(SolarPlatform.CACHE_EXPIRE_HOUR * 2)
def get_alerts(cls) -> List[SolarPlatform.SolarAlert]:
url = f'{SOLAREDGE_BASE_URL}/alerts'
all_alerts = []
try:
response = requests.get(url, headers=SOLAREDGE_HEADERS)
response.raise_for_status()
alerts = response.json()
for alert in alerts:
# Filter out unwanted alert types
if alert.get('type') == 'SNOW_ON_SITE':
continue
site_id = cls.add_vendorcodeprefix(alert.get('siteId'))
alert_type = cls.convert_alert_to_standard(alert.get('type'))
alert_details = ''
if alert_type == SolarPlatform.AlertType.CONFIG_ERROR:
alert_details = alert.get('type')
first_triggered_str = alert.get('firstTrigger')
# If the timestamp ends with a 'Z', replace it with '+00:00' for proper parsing
if first_triggered_str and first_triggered_str.endswith("Z"):
first_triggered = datetime.fromisoformat(
first_triggered_str.replace("Z", "+00:00"))
else:
first_triggered = first_triggered_str
solarAlert = SolarPlatform.SolarAlert(site_id, alert_type, alert.get('impact'), alert_details, first_triggered)
all_alerts.append(solarAlert)
return all_alerts
except requests.exceptions.RequestException as e:
print(f"Failed to retrieve SolarEdge alerts: {e}")
return []