Skip to content

Commit

Permalink
[fronius] Catch IAE thrown by PercentType constructor or FroniusBatte…
Browse files Browse the repository at this point in the history
…ryControl on invalid percent value

Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 committed Jan 10, 2025
1 parent 0e0e9d8 commit d823d22
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.openhab.core.thing.binding.ThingHandler;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Implementation of the {@link ThingActions} interface used for controlling battery charging and discharging for
Expand All @@ -41,6 +43,8 @@
@ThingActionsScope(name = "fronius")
@NonNullByDefault
public class FroniusSymoInverterActions implements ThingActions {
private final Logger logger = LoggerFactory.getLogger(FroniusSymoInverterActions.class);

private @Nullable FroniusSymoInverterHandler handler;

public static boolean resetBatteryControl(ThingActions actions) {
Expand Down Expand Up @@ -191,7 +195,12 @@ public boolean addForcedBatteryChargingSchedule(ZonedDateTime from, ZonedDateTim
@ActionInput(name = "percent", label = "@text/actions.soc.label", description = "@text/actions.soc.description", required = true) int percent) {
FroniusSymoInverterHandler handler = this.handler;
if (handler != null) {
return handler.setBackupReservedBatteryCapacity(new PercentType(percent));
try {
return handler.setBackupReservedBatteryCapacity(new PercentType(percent));
} catch (IllegalArgumentException e) {
logger.warn("Failed to set backup reserved battery capacity: {}", e.getMessage());
return false;
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private void setTimeOfUse(TimeOfUseRecords records) throws FroniusCommunicationE
new ByteArrayInputStream(json.getBytes()), "application/json", API_TIMEOUT);
PostConfigResponse response = gson.fromJson(responseString, PostConfigResponse.class);
if (!response.writeSuccess().contains("timeofuse")) {
LOGGER.debug("{}", responseString);
throw new FroniusCommunicationException("Failed to write configuration to inverter");
}
LOGGER.trace("Time of Use settings set successfully");
Expand Down Expand Up @@ -209,10 +210,11 @@ ScheduleType.CHARGE_MIN, new TimeTableRecord(from.format(TIME_FORMATTER), until.
*
* @param percent the reserved battery capacity for backup power
* @throws FroniusCommunicationException when an error occurs during communication with the inverter
* @throws IllegalArgumentException if the percent is not in [10,95]
*/
public void setBackupReservedCapacity(PercentType percent) throws FroniusCommunicationException {
if (percent.intValue() < 10) {
throw new IllegalArgumentException("percent must be in the range [10, 100]");
if (percent.intValue() < 10 || percent.intValue() > 95) {
throw new IllegalArgumentException("invalid percent value: " + percent + " (must be in [10,95])");
}

// Login and get the auth header for the next request
Expand All @@ -227,6 +229,7 @@ public void setBackupReservedCapacity(PercentType percent) throws FroniusCommuni
new ByteArrayInputStream(json.getBytes()), "application/json", API_TIMEOUT);
PostConfigResponse response = gson.fromJson(responseString, PostConfigResponse.class);
if (!response.writeSuccess().contains(BACKUP_RESERVED_CAPACITY_PARAMETER)) {
LOGGER.debug("{}", responseString);
throw new FroniusCommunicationException("Failed to write configuration to inverter");
}
LOGGER.trace("Backup Reserved Capacity setting set successfully");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2010-2025 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
Expand Down

0 comments on commit d823d22

Please sign in to comment.