In followup of the Trannergy platform migration, as previously noticed by different users, I contacted the customer service as mentioned in the notification mail you should have received. Customer service indeed is confirming the implementation of a different API. While using the script as shared at the start of this post, it indeed will result in errors, and you would not get this fixed using the method initially used.
With a minimum amount of words, the customer services created an appID and appSercret which should make it possible to talk to the new API. Additionally they also shared a manual with limited but useful information on how to talk to the API.
With my 20 years rusty knowledge of programming, I started as a complete new-be in Python to see if I could get this working.....and got it fixed.
Steps to get this implemented:
1) Contact customer support (
[email protected]) and ask them to create an AppID and AppSecret connected to your loginname for your current account. And yes, this customer service is also for Trannergy.
2) Easiest way forward is to make a backup copy of your current, in this case, trannergy.py file
3) Copy & paste below code in the existing .py file
4) Adjust the #Trannergy account detail section to your need
5) Adjust the #Domoticz settings. You can copy them from your previous configuration
6) Test the script first
7) When successful, remove the # in front of the before last line
Happy coding
Note: Code is using Python 3, and you need the libs as mentioned at the top of the script.
Code: Select all
#!/usr/bin/env python3
import http.client, urllib.request, json, hashlib
from datetime import datetime
#Trannergy account details
apiSite = "api.solarmanpv.com"
appID = "" #Change to your own appID, to be provided from Trannergy/Solarman
appSecret = "" #Change to your own appSecret, to be provided from Trannergy/Solarman
email = "" #Change to your own account name/mail address
password = b"" #Change password to your own, maintain the b in front of the password as password need to be binary
conn = http.client.HTTPSConnection(apiSite)
password = hashlib.sha256(password).hexdigest()
#domoticz settings
domoticz_host = "" #ip adress of the domoticz host
domoticz_port = "8080"
domoticz_url = "json.htm"
domoticz_ActualPower = "" #idx of new device
#Request access token, manaul chapter/routine 2.1
payload = json.dumps({
"appSecret": appSecret,
"email": email,
"password": password
})
headers = {
'Content-Type': 'application/json'
}
conn.request("POST", "/account/v1.0/token?appId="+appID+"&language=en", payload, headers)
res = conn.getresponse()
data = json.loads(res.read())
accesstoken = data['access_token']
print("Accesstoken :" +accesstoken)
#Request current power + lastupdate time + stationID, manual chapter/routine 4.4
payload = json.dumps({
"page": 1,
"size": 20
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'bearer '+accesstoken
}
conn.request("POST", "/station/v1.0/list?language=en", payload, headers)
res = conn.getresponse()
data = json.loads(res.read())
stationID = str(data['stationList'][0]['id'])
currentpower = str(data['stationList'][0]['generationPower'])
lastupdatetime = int(data['stationList'][0]['lastUpdateTime'])
lastupdatetime = str(datetime.fromtimestamp(lastupdatetime))
print("Current Power :"+currentpower+"W")
print("Last update time :"+lastupdatetime)
print("StationID :"+stationID)
#Request deviceID + deviceSN, manual chapter/routine 4.2
payload = json.dumps({
"deviceType": "INVERTER",
"page": 1,
"size": 10,
"stationId": stationID
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'bearer '+accesstoken
}
conn.request("POST", "/station/v1.0/device?language=en", payload, headers)
res = conn.getresponse()
data = json.loads(res.read())
deviceID = str(data['deviceListItems'][0]['deviceId'])
deviceSN = str(data['deviceListItems'][0]['deviceSn'])
print('Device serial : '+deviceSN)
print('Device ID : '+deviceID)
#Total generated power, manual chapter/routine 3.3
payload = json.dumps({
"deviceSn": deviceSN,
"deviceId": deviceID
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'bearer '+accesstoken
}
conn.request("POST", "/device/v1.0/currentData?language=en", payload, headers)
res = conn.getresponse()
data = json.loads(res.read())
multiply='1000.0'
totalgenpower = float(data['dataList'][16]['value']) * float(multiply)
totalgenpower = str(totalgenpower)
print("Total generated power: " +totalgenpower+"W")
#uploading values to domoticz
url = ("http://" + domoticz_host + ":" + domoticz_port + "/" + domoticz_url+ "?type=command¶m=udevice&idx=" + domoticz_ActualPower+ "&nvalue=0&svalue=" + currentpower+ ";" + totalgenpower)
#urllib.request.urlopen(url)
print(url)