Skip to content

Commit

Permalink
Support multiple inverters (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Ruys committed Jul 5, 2020
2 parents 8f53be4 + 2f5c801 commit 7e26c6f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
5 changes: 0 additions & 5 deletions gw2pvo/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,6 @@ def run():
raise ValueError('Invalid log level: %s' % loglevel)
logging.basicConfig(format='%(levelname)-8s %(message)s', level=numeric_level)

# Check if we're running the supported Python version
if sys.version_info[0] != 3:
logging.error("Please use Python 3 to run this script")
sys.exit()

# Check if we want to copy old data
if args.date:
try:
Expand Down
62 changes: 43 additions & 19 deletions gw2pvo/gw_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,63 @@ def __init__(self, system_id, account, password):
self.token = '{"version":"v3.1","client":"ios","language":"en"}'
self.global_url = 'https://semsportal.com/api/'
self.base_url = self.global_url
self.status = { -1 : 'Offline', 0 : 'Waiting', 1 : 'Normal' }

def statusText(self, status):
labels = { -1 : 'Offline', 0 : 'Waiting', 1 : 'Normal', 2: 'Fault' }
return labels[status] if status in labels else 'Unknown'

def calcPvVoltage(self, data):
pv_voltages = [
data['vpv' + str(i)]
for i in range(1, 5)
if 'vpv' + str(i) in data
if data['vpv' + str(i)]
if data['vpv' + str(i)] < 6553
]
return round(sum(pv_voltages), 1)

def getCurrentReadings(self):
''' Download the most recent readings from the GoodWe API. '''

payload = {
'powerStationId' : self.system_id
}

# goodwe_server
data = self.call("v2/PowerStation/GetMonitorDetailByPowerstationId", payload)

inverterData = data['inverter'][0]

pv_voltages = [
inverterData['d']['vpv' + str(i)]
for i in range(1, 5)
if 'vpv' + str(i) in inverterData['d']
if inverterData['d']['vpv' + str(i)]
if inverterData['d']['vpv' + str(i)] < 6553
]

result = {
'status' : self.status[inverterData['status']],
'pgrid_w' : inverterData['out_pac'],
'eday_kwh' : inverterData['eday'],
'etotal_kwh' : inverterData['etotal'],
'grid_voltage' : self.parseValue(inverterData['output_voltage'], 'V'),
'pv_voltage' : round(sum(pv_voltages), 1),
'status' : 'Unknown',
'pgrid_w' : 0,
'eday_kwh' : 0,
'etotal_kwh' : 0,
'grid_voltage' : 0,
'pv_voltage' : 0,
'latitude' : data['info'].get('latitude'),
'longitude' : data['info'].get('longitude')
}

count = 0
for inverterData in data['inverter']:
status = self.statusText(inverterData['status'])
if status == 'Normal':
result['status'] = status
result['pgrid_w'] += inverterData['out_pac']
result['grid_voltage'] += self.parseValue(inverterData['output_voltage'], 'V')
result['pv_voltage'] += self.calcPvVoltage(inverterData['d'])
count += 1
result['eday_kwh'] += inverterData['eday']
result['etotal_kwh'] += inverterData['etotal']
if count > 0:
# These values should not be the sum, but the average
result['grid_voltage'] /= count
result['pv_voltage'] /= count
elif len(data['inverter']) > 0:
# We have no online inverters, then just pick the first
inverterData = data['inverter'][0]
result['status'] = self.statusText(inverterData['status'])
result['pgrid_w'] = inverterData['out_pac']
result['grid_voltage'] = self.parseValue(inverterData['output_voltage'], 'V')
result['pv_voltage'] = self.calcPvVoltage(inverterData['d'])

message = "{status}, {pgrid_w} W now, {eday_kwh} kWh today, {etotal_kwh} kWh all time, {grid_voltage} V grid, {pv_voltage} V PV".format(**result)
if result['status'] == 'Normal' or result['status'] == 'Offline':
logging.info(message)
Expand Down

0 comments on commit 7e26c6f

Please sign in to comment.