-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolar.py
46 lines (36 loc) · 1.48 KB
/
solar.py
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
import os
import requests
from flask import Flask
import datetime
app = Flask(__name__)
# Dialogflow invokes with POST
@app.route('/', methods=['GET', 'POST'])
def index():
return get_current_generation()
# API docs
# https://www.solaredge.com/sites/default/files/se_monitoring_api.pdf
def get_current_generation(request=''):
# Environment variables set in the Cloud Run config.
siteId = os.environ.get('SITE_ID')
apiKey = os.environ.get('SOLAR_API_KEY')
apiKeyParam = 'api_key='+apiKey
baseUrl = 'https://monitoringapi.solaredge.com/site/' + siteId
overviewUrl = baseUrl + '/overview?' + apiKeyParam
resp = requests.get(overviewUrl).json()
overviewData = resp['overview']
currentKw = round(overviewData['currentPower']['power'] / 1000, 1)
lastDayKwh = round(overviewData['lastDayData']['energy'] / 1000)
lifeTimeKwh = round(overviewData['lifeTimeData']['energy'] / 1000)
# Heuristic to decide whether today's production is done or not, so we can
# phrase accordingly.
prefix = 'So far today, '
if datetime.datetime.now().hour >= 16 and currentKw == 0:
prefix = 'Today was '
reply = prefix + str(lastDayKwh) + ' kilowatt-hours.'
# Format the reply in the way Google Assistant needs it.
replyObj = {'fulfillmentText': reply}
return str(replyObj)
if __name__ == '__main__':
# copied from https://cloud.google.com/run/docs/quickstarts/build-and-deploy:
app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))
#print(get_current_generation())