This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsunnyportal2pvoutput
executable file
·284 lines (242 loc) · 9.54 KB
/
sunnyportal2pvoutput
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
#!/usr/bin/env python3
# Copyright (c) 2016 Erik Johansson <erik@ejohansson.se>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
from datetime import date, datetime, timedelta
from getpass import getpass
import argparse
import configparser
import http.client as http
import logging
import sys
import sunnyportal.client, sunnyportal.responses
import time
import urllib.parse
class PvOutput(object):
def __init__(self, apikey, systemid, dry_run):
self.dry_run = dry_run
self.headers = {
"X-Pvoutput-Apikey": apikey,
"X-Pvoutput-SystemId": systemid,
"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain",
}
def send_request(self, url, params):
logging.debug("POST to %s: %s", url, params)
if self.dry_run:
return (http.OK, "OK", "")
conn = http.HTTPConnection("pvoutput.org")
encoded = urllib.parse.urlencode(params)
conn.request("POST", url, encoded, self.headers)
response = conn.getresponse()
status = response.status
reason = response.reason
body = response.read().decode("utf-8")
logging.debug("HTTP response: %d (%s): %s", status, reason, body)
conn.close()
return (status, reason, body)
def add_status(self, datetime, generated_energy, power=None):
params = {
"d": datetime.strftime("%Y%m%d"),
"t": datetime.strftime("%H:%M"),
"v1": generated_energy,
}
if power is not None:
params["v2"] = power
(status, reason, body) = self.send_request("/service/r2/addstatus.jsp", params)
if status != http.OK:
logging.error("Failed to add status: %s", body)
raise Exception("could not add status")
def add_batch_status(self, statuses):
data = []
for status in statuses:
data.append(
",".join(
[
status[0].strftime("%Y%m%d"),
status[0].strftime("%H:%M"),
str(status[2]),
str(status[1]),
]
)
)
offset = 0
while offset < len(data):
entries = data[offset : offset + 30]
(status, reason, body) = self.send_request(
"/service/r2/addbatchstatus.jsp", {"data": ";".join(entries)}
)
if status == http.OK:
offset += len(entries)
if offset < len(data):
time.sleep(10)
elif status == http.BAD_REQUEST and "Load in progress" in body:
time.sleep(20)
else:
logging.error("Failed to add status batch: %s", body)
raise Exception("could not add status batch")
def add_batch_output(self, data):
offset = 0
batch_size = 100
while offset < len(data):
entries = data[offset : offset + batch_size]
(status, reason, body) = self.send_request(
"/service/r2/addoutput.jsp", {"data": ";".join(entries)}
)
if status == http.OK:
offset += len(entries)
if offset < len(data):
time.sleep(10)
elif status == http.BAD_REQUEST and "Load in progress" in body:
time.sleep(20)
elif batch_size > 1 and len(entries) > 1:
# Retry with a single entry in case it fails due to missing donation mode
batch_size = 1
else:
logging.error("Failed to add output batch: %s", body)
raise Exception("could not add output batch")
def get_data_for_day(plant, date, get_consumption):
day = plant.day_overview(date)
if day.difference is None:
return None
data = ["" for _ in range(14)]
data[0] = day.date.strftime("%Y%m%d")
data[1] = day.difference
# Find peak hour and power
peak = sunnyportal.responses.Power(None, 0, None, None)
for measure in day.power_measurements:
if measure.power > peak.power:
peak = measure
if peak.timestamp is not None:
data[3] = peak.power
data[4] = peak.timestamp.strftime("%H:%M")
if get_consumption:
balance = plant.day_energy_balance(date).day
data[2] = balance.generation.feed_in
data[13] = balance.consumption.external + balance.consumption.internal
# All import as "Import Peak"
data[9] = balance.consumption.external
return ",".join(str(d) for d in data).rstrip(",")
def get_data_for_period(plant, start_date=None, end_date=None, get_consumption=False):
if start_date is None:
start_date = plant.all_data("year").start_timestamp.date()
if end_date is None:
end_date = date.today()
data = []
while start_date <= end_date:
res = get_data_for_day(plant, start_date, get_consumption)
if res:
data.append(res)
start_date = start_date + timedelta(days=1)
return data
def check_positive(value):
ivalue = int(value)
if ivalue <= 0:
raise argparse.ArgumentTypeError("%s is an invalid positive int value" % value)
return ivalue
def main():
logging.basicConfig(
format="%(asctime)s %(levelname)s: %(message)s", level=logging.DEBUG
)
parser = argparse.ArgumentParser(description="Connect Sunny Portal to PVoutput.org")
parser.add_argument("config", help="Configuration file to use")
parser.add_argument("-s", "--status", help="Report status(es)", action="store_true")
parser.add_argument("-o", "--output", help="Report output(s)", action="store_true")
parser.add_argument(
"-c", "--consumption", help="Report consumption", action="store_true"
)
parser.add_argument(
"-p",
"--days-past",
help="number of DAYS in the past to go back -- default: 0 (today only)",
type=check_positive,
default=0,
)
parser.add_argument("-q", "--quiet", help="Silence output", action="store_true")
parser.add_argument(
"-n", "--dry-run", help="Don't send any data", action="store_true"
)
args = parser.parse_args()
if args.quiet:
logging.disable(logging.DEBUG)
config = configparser.ConfigParser()
config["sunnyportal"] = {}
config["pvoutput"] = {}
config.read(args.config)
modified = False
if not config["sunnyportal"].get("email"):
config["sunnyportal"]["email"] = input("Sunny Portal e-mail: ")
modified = True
if not config["sunnyportal"].get("password"):
config["sunnyportal"]["password"] = getpass("Sunny Portal password: ")
modified = True
if not config["pvoutput"].get("apikey"):
config["pvoutput"]["apikey"] = input("PVOutput API key: ")
modified = True
if not config["pvoutput"].get("systemid"):
config["pvoutput"]["systemid"] = input("PVOutput System Id: ")
modified = True
client = sunnyportal.client.Client(
config["sunnyportal"]["email"], config["sunnyportal"]["password"]
)
plants = client.get_plants()
if not plants:
logging.error("No plant found")
sys.exit(1)
plant_oid = config["sunnyportal"].get("plant")
if not plant_oid:
index = 1
if len(plants) > 1:
print("Found %d plants" % len(plants))
for i, plant in enumerate(plants):
print("%d: %s" % (i + 1, plant.name))
index = int(input("Enter number for the plant to use: "))
plant_oid = plants[index - 1].oid
config["sunnyportal"]["plant"] = plant_oid
modified = True
if modified:
with open(args.config, "w") as configfile:
config.write(configfile)
for plant in plants:
if plant.oid == plant_oid:
break
else:
logging.error("Configured plant not found")
sys.exit(1)
pvoutput = PvOutput(
config["pvoutput"]["apikey"], config["pvoutput"]["systemid"], args.dry_run
)
if args.status:
for i in reversed(range(0, args.days_past + 1)):
day = plant.day_overview(date.today() - timedelta(days=i))
if day.power_measurements:
data = []
energy = day.difference
for measure in reversed(day.power_measurements):
data.append((measure.timestamp, measure.power, energy))
energy -= int(measure.power / 4)
pvoutput.add_batch_status(list(reversed(data)))
elif day.difference is not None:
pvoutput.add_status(day.date, day.difference)
if args.output:
start_date = date.today() - timedelta(days=args.days_past)
data = get_data_for_period(
plant, start_date=start_date, get_consumption=args.consumption
)
pvoutput.add_batch_output(data)
client.logout()
if __name__ == "__main__":
main()