Skip to content

Commit e009276

Browse files
authored
[sunsynk] Updates to support Solar strings, fixes for battery status and misc improvments. (#19506)
* Fix: Mismatch between Constants.java and thing.xml. battery-grid >> battery-dc Signed-off-by: LeeC77 <lee.charlton00@gmail.com>
1 parent 493aeb1 commit e009276

24 files changed

Lines changed: 782 additions & 189 deletions

bundles/org.openhab.binding.sunsynk/README.md

Lines changed: 77 additions & 36 deletions
Large diffs are not rendered by default.
11.5 KB
Loading

bundles/org.openhab.binding.sunsynk/src/main/feature/feature.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<features name="org.openhab.binding.sunsynk-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
33
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>
4-
54
<feature name="openhab-binding-sunsynk" description="SunSynk Binding" version="${project.version}">
65
<feature>openhab-runtime-base</feature>
76
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.sunsynk/${project.version}</bundle>

bundles/org.openhab.binding.sunsynk/src/main/java/org/openhab/binding/sunsynk/internal/SunSynkBindingConstants.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,30 @@ public class SunSynkBindingConstants {
7070
public static final String CHANNEL_INVERTER_GRID_POWER = "inverter-grid-power";
7171
public static final String CHANNEL_INVERTER_GRID_VOLTAGE = "inverter-grid-voltage";
7272
public static final String CHANNEL_INVERTER_GRID_CURRENT = "inverter-grid-current";
73+
public static final String CHANNEL_INVERTER_GRID_FREQUENCY = "inverter-grid-frequency";
74+
public static final String CHANNEL_INVERTER_RATED_AC_OUTPUT = "inverter-rated-ac-output";
7375

7476
public static final String CHANNEL_INVERTER_AC_TEMPERATURE = "inverter-ac-temperature";
7577
public static final String CHANNEL_INVERTER_DC_TEMPERATURE = "inverter-dc-temperature";
7678

77-
public static final String CHANNEL_BATTERY_VOLTAGE = "battery-grid-voltage";
78-
public static final String CHANNEL_BATTERY_CURRENT = "battery-grid-current";
79-
public static final String CHANNEL_BATTERY_POWER = "battery-grid-power";
79+
public static final String CHANNEL_BATTERY_VOLTAGE = "battery-dc-voltage";
80+
public static final String CHANNEL_BATTERY_CURRENT = "battery-dc-current";
81+
public static final String CHANNEL_BATTERY_POWER = "battery-dc-power";
8082
public static final String CHANNEL_BATTERY_SOC = "battery-soc";
8183
public static final String CHANNEL_BATTERY_TEMPERATURE = "battery-temperature";
8284

8385
public static final String CHANNEL_INVERTER_SOLAR_ENERGY_TODAY = "inverter-solar-energy-today";
86+
public static final String CHANNEL_INVERTER_SOLAR_ENERGY_MONTH = "inverter-solar-energy-month";
87+
public static final String CHANNEL_INVERTER_SOLAR_ENERGY_YEAR = "inverter-solar-energy-year";
8488
public static final String CHANNEL_INVERTER_SOLAR_ENERGY_TOTAL = "inverter-solar-energy-total";
85-
public static final String CHANNEL_INVERTER_SOLAR_POWER_NOW = "inverter-solar-power-now";
89+
public static final String CHANNEL_INVERTER_SOLAR_EFFICIENCY = "inverter-solar-efficiency";
90+
public static final String CHANNEL_INVERTER_SOLAR_POWER = "inverter-solar-ac-power";
91+
public static final String CHANNEL_INVERTER_SOLAR_STRING_VOLTAGE_1 = "inverter-solar-string-voltage-1";
92+
public static final String CHANNEL_INVERTER_SOLAR_STRING_VOLTAGE_2 = "inverter-solar-string-voltage-2";
93+
public static final String CHANNEL_INVERTER_SOLAR_STRING_CURRENT_1 = "inverter-solar-string-current-1";
94+
public static final String CHANNEL_INVERTER_SOLAR_STRING_CURRENT_2 = "inverter-solar-string-current-2";
95+
public static final String CHANNEL_INVERTER_SOLAR_STRING_POWER_1 = "inverter-solar-string-power-1";
96+
public static final String CHANNEL_INVERTER_SOLAR_STRING_POWER_2 = "inverter-solar-string-power-2";
8697

8798
public static final String CHANNEL_INVERTER_CONTROL_TIMER = "inverter-control-timer";
8899
public static final String CHANNEL_INVERTER_CONTROL_ENERGY_PATTERN = "inverter-control-energy-pattern";
@@ -92,4 +103,6 @@ public class SunSynkBindingConstants {
92103
public static final String CONFIG_GATE_SERIAL = "gsn";
93104
public static final String CONFIG_SERIAL = "serialnumber";
94105
public static final String CONFIG_NAME = "alias";
106+
public static final String CONFIG_PLANT_ID = "plantId";
107+
public static final String CONFIG_PLANT_NAME = "plantName";
95108
}

bundles/org.openhab.binding.sunsynk/src/main/java/org/openhab/binding/sunsynk/internal/api/AccountController.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class AccountController {
5555
private static final int TIMEOUT_IN_MS = 4000;
5656
private final Logger logger = LoggerFactory.getLogger(AccountController.class);
5757
private static final String BEARER_TYPE = "Bearer ";
58+
private static final long EXPIRYSECONDS = 100L; // 100 seconds before expiry
5859
private Client sunAccount = new Client();
5960

6061
public AccountController() {
@@ -84,10 +85,13 @@ public void authenticate(String username, String password)
8485
public void refreshAccount(String username) throws SunSynkAuthenticateException, SunSynkTokenException {
8586
Long expiresIn = this.sunAccount.getExpiresIn();
8687
Long issuedAt = this.sunAccount.getIssuedAt();
87-
if ((issuedAt + expiresIn) - Instant.now().getEpochSecond() > 30) { // > 30 seconds
88+
if ((issuedAt + expiresIn) - Instant.now().getEpochSecond() > EXPIRYSECONDS) {
8889
logger.debug("Account configuration token not expired.");
8990
return;
9091
}
92+
if (this.sunAccount.getRefreshTokenString().isEmpty()) {
93+
throw new SunSynkTokenException("No refresh token available, re-authentication required.");
94+
}
9195
logger.debug("Account configuration token expired : {}", this.sunAccount.getData().toString());
9296
String payload = makeRefreshBody(username, this.sunAccount.getRefreshTokenString());
9397
sendHttp(payload);
@@ -110,21 +114,21 @@ private void sendHttp(String payload) throws SunSynkAuthenticateException, SunSy
110114
Client client = gson.fromJson(response, Client.class);
111115
if (client == null) {
112116
throw new SunSynkAuthenticateException(
113-
"Sun Synk Account could not be authenticated: Try re-enabling account");
117+
"Sun Synk account could not be authenticated: Try re-enabling account");
114118
}
115119
this.sunAccount = client;
116120
} catch (IOException | JsonSyntaxException e) {
117-
throw new SunSynkAuthenticateException("Sun Synk Account could not be authenticated", e);
121+
throw new SunSynkAuthenticateException("Sun Synk account could not be authenticated", e);
118122
}
119123
if (this.sunAccount.getCode() == 102) {
120-
logger.debug("Sun Synk Account could not be authenticated: {}.", this.sunAccount.getMsg());
124+
logger.debug("Sun Synk account could not be authenticated: {}.", this.sunAccount.getMsg());
121125
throw new SunSynkAuthenticateException(
122-
"Sun Synk Accountfailed to authenticate: Check your password or email.");
126+
"Sun Synk account failed to authenticate: Check your password or email.");
123127
}
124128
if (this.sunAccount.getStatus() == 404) {
125-
logger.debug("Sun Synk Account could not be authenticated: 404 {} {}.", this.sunAccount.getError(),
129+
logger.debug("Sun Synk account could not be authenticated: 404 {} {}.", this.sunAccount.getError(),
126130
this.sunAccount.getPath());
127-
throw new SunSynkAuthenticateException("Sun Synk Accountfailed to authenticate: 404 Not Found.");
131+
throw new SunSynkAuthenticateException("Sun Synk account failed to authenticate: 404 Not Found.");
128132
}
129133
getToken();
130134
}
@@ -148,13 +152,13 @@ public ArrayList<Inverter> getDetails() throws SunSynkInverterDiscoveryException
148152
Gson gson = new Gson();
149153
Properties headers = new Properties();
150154
String response = "";
151-
152155
String httpsURL = makeLoginURL(
153156
"api/v1/inverters?page=1&limit=10&total=0&status=-1&sn=&plantId=&type=-2&softVer=&hmiVer=&agentCompanyId=-1&gsn=");
154157
headers.setProperty(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
155158
headers.setProperty(HttpHeaders.AUTHORIZATION, BEARER_TYPE + APIdata.staticAccessToken);
156159
response = HttpUtil.executeUrl(HttpMethod.GET.asString(), httpsURL, headers, null,
157160
MediaType.APPLICATION_JSON, TIMEOUT_IN_MS);
161+
logger.trace("Account Details Response: {}", response);
158162
@Nullable
159163
Details maybeDeats = gson.fromJson(response, Details.class);
160164
if (maybeDeats == null) {
@@ -163,10 +167,10 @@ public ArrayList<Inverter> getDetails() throws SunSynkInverterDiscoveryException
163167
output = maybeDeats;
164168
} catch (IOException | JsonSyntaxException e) {
165169
if (logger.isDebugEnabled()) {
166-
String message = Objects.requireNonNullElse(e.getMessage(), "unkown error message");
170+
String message = Objects.requireNonNullElse(e.getMessage(), "unknown error message");
167171
Throwable cause = e.getCause();
168-
String causeMessage = cause != null ? Objects.requireNonNullElse(cause.getMessage(), "unkown cause")
169-
: "unkown cause";
172+
String causeMessage = cause != null ? Objects.requireNonNullElse(cause.getMessage(), "unknown cause")
173+
: "unknown cause";
170174
logger.debug("Error attempting to find inverters registered to account: Msg = {}. Cause = {}.", message,
171175
causeMessage);
172176
}

0 commit comments

Comments
 (0)