[fronius] Expose BatteryControl::addSchedule as Thing action (#19352)

* [fronius] Expose BatteryControl::addSchedule as Thing action

Signed-off-by: Florian Hotze <dev@florianhotze.com>
This commit is contained in:
Florian Hotze
2025-09-18 20:11:15 +02:00
committed by GitHub
parent 48485705e6
commit 08d5c8e385
4 changed files with 62 additions and 6 deletions
@@ -193,9 +193,20 @@ Once the actions instance has been retrieved, you can invoke the following metho
- `forceBatteryDischarging(QuantityType<Power> power)`: Force the battery to discharge with the specified power (removes all battery control schedules first and applies all the time).
- `addForcedBatteryDischargingSchedule(LocalTime from, LocalTime until, QuantityType<Power> power)`: Add a schedule to force the battery to discharge with the specified power in the specified time range.
- `addForcedBatteryDischargingSchedule(ZonedDateTime from, ZonedDateTime until, QuantityType<Power> power)`: Add a schedule to force the battery to discharge with the specified power in the specified time range.
- `addSchedule(LocalTime from, LocalTime until, ScheduleType scheduleType, QuantityType<Power> power)`: Add a custom schedule with the specified type and power in the specified time range.
- `addSchedule(ZonedDateTime from, ZonedDateTime until, ScheduleType scheduleType, QuantityType<Power> power)`: Add a custom schedule with the specified type and power in the specified time range.
- `setBackupReservedBatteryCapacity(int percent)`: Set the reserved battery capacity for backup power.
- `setBackupReservedBatteryCapacity(PercentType percent)`: Set the reserved battery capacity for backup power.
The `ScheduleType` enum has the following members:
- `CHARGE_MIN`
- `CHARGE_MAX`
- `DISCHARGE_MIN`
- `DISCHARGE_MAX`
Its full class name is `org.openhab.binding.fronius.internal.api.dto.inverter.batterycontrol.ScheduleType`.
All methods return a boolean value indicating whether the action was successful.
### Examples
@@ -22,6 +22,7 @@ import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.fronius.internal.api.FroniusBatteryControl;
import org.openhab.binding.fronius.internal.api.FroniusCommunicationException;
import org.openhab.binding.fronius.internal.api.FroniusUnauthorizedException;
import org.openhab.binding.fronius.internal.api.dto.inverter.batterycontrol.ScheduleType;
import org.openhab.binding.fronius.internal.handler.FroniusSymoInverterHandler;
import org.openhab.core.automation.annotation.ActionInput;
import org.openhab.core.automation.annotation.ActionOutput;
@@ -58,6 +59,21 @@ public class FroniusSymoInverterActions implements ThingActions {
}
}
public static boolean addSchedule(ThingActions actions, LocalTime from, LocalTime until, ScheduleType scheduleType,
QuantityType<Power> power) {
if (actions instanceof FroniusSymoInverterActions froniusSymoInverterActions) {
return froniusSymoInverterActions.addSchedule(from, until, scheduleType, power);
} else {
throw new IllegalArgumentException(
"The 'actions' argument is not an instance of FroniusSymoInverterActions");
}
}
public static boolean addSchedule(ThingActions actions, ZonedDateTime from, ZonedDateTime until,
ScheduleType scheduleType, QuantityType<Power> power) {
return addSchedule(actions, from.toLocalTime(), until.toLocalTime(), scheduleType, power);
}
public static boolean holdBatteryCharge(ThingActions actions) {
if (actions instanceof FroniusSymoInverterActions froniusSymoInverterActions) {
return froniusSymoInverterActions.holdBatteryCharge();
@@ -201,6 +217,31 @@ public class FroniusSymoInverterActions implements ThingActions {
return false;
}
public boolean addSchedule(LocalTime from, LocalTime until, ScheduleType scheduleType, QuantityType<Power> power) {
FroniusBatteryControl batteryControl = getBatteryControl();
if (batteryControl != null) {
return executeBatteryControlAction(() -> {
batteryControl.addSchedule(from, until, scheduleType, power);
return true;
});
}
return false;
}
public boolean addSchedule(ZonedDateTime from, ZonedDateTime until, ScheduleType scheduleType,
QuantityType<Power> power) {
return addSchedule(from.toLocalTime(), until.toLocalTime(), scheduleType, power);
}
@RuleAction(label = "@text/actions.add-schedule.label", description = "@text/actions.add-schedule.description")
public @ActionOutput(type = "boolean", label = "Success") boolean addSchedule(
@ActionInput(name = "from", label = "@text/actions.from.label", description = "@text/actions.from.description", required = true) LocalTime from,
@ActionInput(name = "until", label = "@text/actions.until.label", description = "@text/actions.until.description", required = true) LocalTime until,
@ActionInput(name = "scheduleType", label = "@text/actions.schedule-type.label", description = "@text/actions.schedule-type.description", required = true) String scheduleType,
@ActionInput(name = "power", label = "@text/actions.power.label", description = "@text/actions.power.description", type = "QuantityType<Power>", required = true) QuantityType<Power> power) {
return addSchedule(from, until, ScheduleType.valueOf(scheduleType), power);
}
@RuleAction(label = "@text/actions.hold-battery-charge.label", description = "@text/actions.hold-battery-charge.description")
public @ActionOutput(type = "boolean", label = "Success") boolean holdBatteryCharge() {
FroniusBatteryControl batteryControl = getBatteryControl();
@@ -233,7 +274,7 @@ public class FroniusSymoInverterActions implements ThingActions {
@RuleAction(label = "@text/actions.force-battery-charging.label", description = "@text/actions.force-battery-charging.description")
public @ActionOutput(type = "boolean", label = "Success") boolean forceBatteryCharging(
@ActionInput(name = "power", label = "@text/actions.power.label", description = "@text/actions.power.label", type = "QuantityType<Power>", required = true) QuantityType<Power> power) {
@ActionInput(name = "power", label = "@text/actions.power.label", description = "@text/actions.power.description", type = "QuantityType<Power>", required = true) QuantityType<Power> power) {
FroniusBatteryControl batteryControl = getBatteryControl();
if (batteryControl != null) {
return executeBatteryControlAction(() -> {
@@ -248,7 +289,7 @@ public class FroniusSymoInverterActions implements ThingActions {
public @ActionOutput(type = "boolean", label = "Success") boolean addForcedBatteryChargingSchedule(
@ActionInput(name = "from", label = "@text/actions.from.label", description = "@text/actions.from.description", required = true) LocalTime from,
@ActionInput(name = "until", label = "@text/actions.until.label", description = "@text/actions.until.description", required = true) LocalTime until,
@ActionInput(name = "power", label = "@text/actions.power.label", description = "@text/actions.power.label", type = "QuantityType<Power>", required = true) QuantityType<Power> power) {
@ActionInput(name = "power", label = "@text/actions.power.label", description = "@text/actions.power.description", type = "QuantityType<Power>", required = true) QuantityType<Power> power) {
FroniusBatteryControl batteryControl = getBatteryControl();
if (batteryControl != null) {
return executeBatteryControlAction(() -> {
@@ -296,7 +337,7 @@ public class FroniusSymoInverterActions implements ThingActions {
@RuleAction(label = "@text/actions.force-battery-discharging.label", description = "@text/actions.force-battery-discharging.description")
public @ActionOutput(type = "boolean", label = "Success") boolean forceBatteryDischarging(
@ActionInput(name = "power", label = "@text/actions.power.label", description = "@text/actions.power.label", type = "QuantityType<Power>", required = true) QuantityType<Power> power) {
@ActionInput(name = "power", label = "@text/actions.power.label", description = "@text/actions.power.description", type = "QuantityType<Power>", required = true) QuantityType<Power> power) {
FroniusBatteryControl batteryControl = getBatteryControl();
if (batteryControl != null) {
return executeBatteryControlAction(() -> {
@@ -311,7 +352,7 @@ public class FroniusSymoInverterActions implements ThingActions {
public @ActionOutput(type = "boolean", label = "Success") boolean addForcedBatteryDischargingSchedule(
@ActionInput(name = "from", label = "@text/actions.from.label", description = "@text/actions.from.description", required = true) LocalTime from,
@ActionInput(name = "until", label = "@text/actions.until.label", description = "@text/actions.until.description", required = true) LocalTime until,
@ActionInput(name = "power", label = "@text/actions.power.label", description = "@text/actions.power.label", type = "QuantityType<Power>", required = true) QuantityType<Power> power) {
@ActionInput(name = "power", label = "@text/actions.power.label", description = "@text/actions.power.description", type = "QuantityType<Power>", required = true) QuantityType<Power> power) {
FroniusBatteryControl batteryControl = getBatteryControl();
if (batteryControl != null) {
return executeBatteryControlAction(() -> {
@@ -170,7 +170,7 @@ public class FroniusBatteryControl {
* @throws FroniusCommunicationException when an error occurs during communication with the inverter
* @throws FroniusUnauthorizedException when the login fails due to invalid credentials
*/
private void addSchedule(LocalTime from, LocalTime until, ScheduleType scheduleType, QuantityType<Power> power)
public void addSchedule(LocalTime from, LocalTime until, ScheduleType scheduleType, QuantityType<Power> power)
throws FroniusCommunicationException, FroniusUnauthorizedException {
TimeOfUseRecords currentTimeOfUse = getTimeOfUse();
TimeOfUseRecord[] timeOfUse = new TimeOfUseRecord[currentTimeOfUse.records().length + 1];
@@ -124,6 +124,8 @@ channel-type.fronius.year_energy.description = Energy generated in current year
actions.reset-battery-control.label = Reset Battery Control
actions.reset-battery-control.description = Remove all battery control schedules from the inverter
actions.add-schedule.label = Add Advanced Schedule
actions.add-schedule.description = Add an advanced battery control schedule to the inverter
actions.hold-battery-charge.label = Hold Battery Charge
actions.hold-battery-charge.description = Prevent the battery from discharging
actions.add-hold-battery-charge-schedule.label = Add Hold Battery Charge Schedule
@@ -140,14 +142,16 @@ actions.force-battery-discharging.label = Force Battery Discharging
actions.force-battery-discharging.description = Force the battery to discharge with the specified power
actions.add-forced-battery-discharging-schedule.label = Add Forced Battery Discharging Schedule
actions.add-forced-battery-discharging-schedule.description = Add a schedule to force the battery to discharge with the specified power in the specified time range
actions.backup-reserved-battery-capacity.label = Backup Power Reserved Battery Capacity
actions.backup-reserved-battery-capacity.description = Set the reserved battery capacity for backup power supply
actions.from.label = Begin Timestamp
actions.from.description = The beginning of the time range
actions.until.label = End Timestamp
actions.until.description = The (inclusive) end of the time range
actions.power.label = Power
actions.power.description = The power to charge the battery with
actions.schedule-type.label = Schedule Type
actions.schedule-type.description = The type of the schedule: CHARGE_MIN, CHARGE_MAX, DISCHARGE_MIN or DISCHARGE_MAX
actions.soc.label = State of Charge
actions.soc.description = Battery State of Charge (in percent)