2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bmwconnecteddrive.internal.action;
15 import static org.openhab.binding.bmwconnecteddrive.internal.utils.ChargeProfileWrapper.ProfileKey.*;
17 import java.time.DayOfWeek;
18 import java.time.LocalTime;
19 import java.util.Optional;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.bmwconnecteddrive.internal.handler.VehicleHandler;
25 import org.openhab.binding.bmwconnecteddrive.internal.utils.ChargeProfileWrapper;
26 import org.openhab.binding.bmwconnecteddrive.internal.utils.ChargeProfileWrapper.ProfileKey;
27 import org.openhab.core.automation.annotation.ActionInput;
28 import org.openhab.core.automation.annotation.ActionOutput;
29 import org.openhab.core.automation.annotation.RuleAction;
30 import org.openhab.core.thing.binding.ThingActions;
31 import org.openhab.core.thing.binding.ThingActionsScope;
32 import org.openhab.core.thing.binding.ThingHandler;
35 * The {@link BMWConnectedDriveActions} provides actions for VehicleHandler
37 * @author Norbert Truchsess - Initial contribution
39 @ThingActionsScope(name = "bmwconnecteddrive")
41 public class BMWConnectedDriveActions implements ThingActions {
43 private Optional<VehicleHandler> handler = Optional.empty();
45 private Optional<ChargeProfileWrapper> profile = Optional.empty();
47 @RuleAction(label = "getTimer1Departure", description = "returns the departure time of timer1")
48 public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getTimer1Departure() {
49 return getTime(TIMER1);
52 @RuleAction(label = "setTimer1Departure", description = "sets the timer1 departure time")
53 public void setTimer1Departure(@ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
54 setTime(TIMER1, time);
57 @RuleAction(label = "getTimer1Enabled", description = "returns the enabled state of timer1")
58 public @ActionOutput(name = "enabled", type = "java.util.Optional<java.lang.Boolean>") Optional<Boolean> getTimer1Enabled() {
59 return getEnabled(TIMER1);
62 @RuleAction(label = "setTimer1Enabled", description = "sets the enabled state of timer1")
63 public void setTimer1Enabled(@ActionInput(name = "enabled", type = "java.lang.Boolean") @Nullable Boolean enabled) {
64 setEnabled(TIMER1, enabled);
67 @RuleAction(label = "getTimer2Departure", description = "returns the departure time of timer2")
68 public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getTimer2Departure() {
69 return getTime(TIMER2);
72 @RuleAction(label = "setTimer2Departure", description = "sets the timer2 departure time")
73 public void setTimer2Departure(@ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
74 setTime(TIMER2, time);
77 @RuleAction(label = "getTimer2Enabled", description = "returns the enabled state of timer2")
78 public @ActionOutput(name = "enabled", type = "java.util.Optional<java.lang.Boolean>") Optional<Boolean> getTimer2Enabled() {
79 return getEnabled(TIMER2);
82 @RuleAction(label = "setTimer2Enabled", description = "sets the enabled state of timer2")
83 public void setTimer2Enabled(@ActionInput(name = "enabled", type = "java.lang.Boolean") @Nullable Boolean enabled) {
84 setEnabled(TIMER2, enabled);
87 @RuleAction(label = "getTimer3Departure", description = "returns the departure time of timer3")
88 public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getTimer3Departure() {
89 return getTime(TIMER3);
92 @RuleAction(label = "setTimer3Departure", description = "sets the timer3 departure time")
93 public void setTimer3Departure(@ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
94 setTime(TIMER3, time);
97 @RuleAction(label = "getTimer3Enabled", description = "returns the enabled state of timer3")
98 public @ActionOutput(name = "enabled", type = "java.util.Optional<java.lang.Boolean>") Optional<Boolean> getTimer3Enabled() {
99 return getEnabled(TIMER3);
102 @RuleAction(label = "setTimer3Enabled", description = "sets the enabled state of timer3")
103 public void setTimer3Enabled(@ActionInput(name = "enabled", type = "java.lang.Boolean") @Nullable Boolean enabled) {
104 setEnabled(TIMER3, enabled);
107 @RuleAction(label = "getOverrideTimerDeparture", description = "returns the departure time of overrideTimer")
108 public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getOverrideTimerDeparture() {
109 return getTime(OVERRIDE);
112 @RuleAction(label = "setOverrideTimerDeparture", description = "sets the overrideTimer departure time")
113 public void setOverrideTimerDeparture(
114 @ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
115 setTime(OVERRIDE, time);
118 @RuleAction(label = "getOverrideTimerEnabled", description = "returns the enabled state of overrideTimer")
119 public @ActionOutput(name = "enabled", type = "java.util.Optional<java.lang.Boolean>") Optional<Boolean> getOverrideTimerEnabled() {
120 return getEnabled(OVERRIDE);
123 @RuleAction(label = "setOverrideTimerEnabled", description = "sets the enabled state of overrideTimer")
124 public void setOverrideTimerEnabled(
125 @ActionInput(name = "enabled", type = "java.lang.Boolean") @Nullable Boolean enabled) {
126 setEnabled(OVERRIDE, enabled);
129 @RuleAction(label = "getPreferredWindowStart", description = "returns the preferred charging-window start time")
130 public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getPreferredWindowStart() {
131 return getTime(WINDOWSTART);
134 @RuleAction(label = "setPreferredWindowStart", description = "sets the preferred charging-window start time")
135 public void setPreferredWindowStart(
136 @ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
137 setTime(WINDOWSTART, time);
140 @RuleAction(label = "getPreferredWindowEnd", description = "returns the preferred charging-window end time")
141 public @ActionOutput(name = "time", type = "java.util.Optional<java.time.LocalTime>") Optional<LocalTime> getPreferredWindowEnd() {
142 return getTime(WINDOWEND);
145 @RuleAction(label = "setPreferredWindowEnd", description = "sets the preferred charging-window end time")
146 public void setPreferredWindowEnd(
147 @ActionInput(name = "time", type = "java.time.LocalTime") @Nullable LocalTime time) {
148 setTime(WINDOWEND, time);
151 @RuleAction(label = "getClimatizationEnabled", description = "returns the enabled state of climatization")
152 public @ActionOutput(name = "enabled", type = "java.util.Optional<java.lang.Boolean>") Optional<Boolean> getClimatizationEnabled() {
153 return getEnabled(CLIMATE);
156 @RuleAction(label = "setClimatizationEnabled", description = "sets the enabled state of climatization")
157 public void setClimatizationEnabled(
158 @ActionInput(name = "enabled", type = "java.lang.Boolean") @Nullable Boolean enabled) {
159 setEnabled(CLIMATE, enabled);
162 @RuleAction(label = "getChargingMode", description = "gets the charging-mode")
163 public @ActionOutput(name = "mode", type = "java.util.Optional<java.lang.String>") Optional<String> getChargingMode() {
164 return getProfile().map(profile -> profile.getMode());
167 @RuleAction(label = "setChargingMode", description = "sets the charging-mode")
168 public void setChargingMode(@ActionInput(name = "mode", type = "java.lang.String") @Nullable String mode) {
169 getProfile().ifPresent(profile -> profile.setMode(mode));
172 @RuleAction(label = "getTimer1Days", description = "returns the days of week timer1 is enabled for")
173 public @ActionOutput(name = "days", type = "java.util.Optional<java.util.Set<java.time.DayOfWeek>>") Optional<Set<DayOfWeek>> getTimer1Days() {
174 return getDays(TIMER1);
177 @RuleAction(label = "setTimer1Days", description = "sets the days of week timer1 is enabled for")
178 public void setTimer1Days(
179 @ActionInput(name = "days", type = "java.util.Set<java.time.DayOfWeek>") @Nullable Set<DayOfWeek> days) {
180 setDays(TIMER1, days);
183 @RuleAction(label = "getTimer2Days", description = "returns the days of week timer2 is enabled for")
184 public @ActionOutput(name = "days", type = "java.util.Optional<java.util.Set<java.time.DayOfWeek>>") Optional<Set<DayOfWeek>> getTimer2Days() {
185 return getDays(TIMER2);
188 @RuleAction(label = "setTimer2Days", description = "sets the days of week timer2 is enabled for")
189 public void setTimer2Days(
190 @ActionInput(name = "days", type = "java.util.Set<java.time.DayOfWeek>") @Nullable Set<DayOfWeek> days) {
191 setDays(TIMER2, days);
194 @RuleAction(label = "getTimer3Days", description = "returns the days of week timer3 is enabled for")
195 public @ActionOutput(name = "days", type = "java.util.Optional<java.util.Set<java.time.DayOfWeek>>") Optional<Set<DayOfWeek>> getTimer3Days() {
196 return getDays(TIMER3);
199 @RuleAction(label = "setTimer3Days", description = "sets the days of week timer3 is enabled for")
200 public void setTimer3Days(
201 @ActionInput(name = "days", type = "java.util.Set<java.time.DayOfWeek>") @Nullable Set<DayOfWeek> days) {
202 setDays(TIMER3, days);
205 @RuleAction(label = "getOverrideTimerDays", description = "returns the days of week the overrideTimer is enabled for")
206 public @ActionOutput(name = "days", type = "java.util.Optional<java.util.Set<java.time.DayOfWeek>>") Optional<Set<DayOfWeek>> getOverrideTimerDays() {
207 return getDays(OVERRIDE);
210 @RuleAction(label = "setOverrideTimerDays", description = "sets the days of week the overrideTimer is enabled for")
211 public void setOverrideTimerDays(
212 @ActionInput(name = "days", type = "java.util.Set<java.time.DayOfWeek>") @Nullable Set<DayOfWeek> days) {
213 setDays(OVERRIDE, days);
216 @RuleAction(label = "sendChargeProfile", description = "sends the charging profile to the vehicle")
217 public void sendChargeProfile() {
218 handler.ifPresent(handle -> handle.sendChargeProfile(getProfile()));
221 @RuleAction(label = "cancel", description = "cancel current edit of charging profile")
222 public void cancelEditChargeProfile() {
223 profile = Optional.empty();
226 public static Optional<LocalTime> getTimer1Departure(ThingActions actions) {
227 return ((BMWConnectedDriveActions) actions).getTimer1Departure();
230 public static void setTimer1Departure(ThingActions actions, @Nullable LocalTime time) {
231 ((BMWConnectedDriveActions) actions).setTimer1Departure(time);
234 public static Optional<Boolean> getTimer1Enabled(ThingActions actions) {
235 return ((BMWConnectedDriveActions) actions).getTimer1Enabled();
238 public static void setTimer1Enabled(ThingActions actions, @Nullable Boolean enabled) {
239 ((BMWConnectedDriveActions) actions).setTimer1Enabled(enabled);
242 public static Optional<LocalTime> getTimer2Departure(ThingActions actions) {
243 return ((BMWConnectedDriveActions) actions).getTimer2Departure();
246 public static void setTimer2Departure(ThingActions actions, @Nullable LocalTime time) {
247 ((BMWConnectedDriveActions) actions).setTimer2Departure(time);
250 public static Optional<Boolean> getTimer2Enabled(ThingActions actions) {
251 return ((BMWConnectedDriveActions) actions).getTimer2Enabled();
254 public static void setTimer2Enabled(ThingActions actions, @Nullable Boolean enabled) {
255 ((BMWConnectedDriveActions) actions).setTimer2Enabled(enabled);
258 public static Optional<LocalTime> getTimer3Departure(ThingActions actions) {
259 return ((BMWConnectedDriveActions) actions).getTimer3Departure();
262 public static void setTimer3Departure(ThingActions actions, @Nullable LocalTime time) {
263 ((BMWConnectedDriveActions) actions).setTimer3Departure(time);
266 public static Optional<Boolean> getTimer3Enabled(ThingActions actions) {
267 return ((BMWConnectedDriveActions) actions).getTimer3Enabled();
270 public static void setTimer3Enabled(ThingActions actions, @Nullable Boolean enabled) {
271 ((BMWConnectedDriveActions) actions).setTimer3Enabled(enabled);
274 public static Optional<LocalTime> getOverrideTimerDeparture(ThingActions actions) {
275 return ((BMWConnectedDriveActions) actions).getOverrideTimerDeparture();
278 public static void setOverrideTimerDeparture(ThingActions actions, @Nullable LocalTime time) {
279 ((BMWConnectedDriveActions) actions).setOverrideTimerDeparture(time);
282 public static Optional<Boolean> getOverrideTimerEnabled(ThingActions actions) {
283 return ((BMWConnectedDriveActions) actions).getOverrideTimerEnabled();
286 public static void setOverrideTimerEnabled(ThingActions actions, @Nullable Boolean enabled) {
287 ((BMWConnectedDriveActions) actions).setOverrideTimerEnabled(enabled);
290 public static Optional<LocalTime> getPreferredWindowStart(ThingActions actions) {
291 return ((BMWConnectedDriveActions) actions).getPreferredWindowStart();
294 public static void setPreferredWindowStart(ThingActions actions, @Nullable LocalTime time) {
295 ((BMWConnectedDriveActions) actions).setPreferredWindowStart(time);
298 public static Optional<LocalTime> getPreferredWindowEnd(ThingActions actions) {
299 return ((BMWConnectedDriveActions) actions).getPreferredWindowEnd();
302 public static void setPreferredWindowEnd(ThingActions actions, @Nullable LocalTime time) {
303 ((BMWConnectedDriveActions) actions).setPreferredWindowEnd(time);
306 public static Optional<Boolean> getClimatizationEnabled(ThingActions actions) {
307 return ((BMWConnectedDriveActions) actions).getClimatizationEnabled();
310 public static void setClimatizationEnabled(ThingActions actions, @Nullable Boolean enabled) {
311 ((BMWConnectedDriveActions) actions).setClimatizationEnabled(enabled);
314 public static Optional<String> getChargingMode(ThingActions actions) {
315 return ((BMWConnectedDriveActions) actions).getChargingMode();
318 public static void setChargingMode(ThingActions actions, @Nullable String mode) {
319 ((BMWConnectedDriveActions) actions).setChargingMode(mode);
322 public static Optional<Set<DayOfWeek>> getTimer1Days(ThingActions actions) {
323 return ((BMWConnectedDriveActions) actions).getTimer1Days();
326 public static void setTimer1Days(ThingActions actions, @Nullable Set<DayOfWeek> days) {
327 ((BMWConnectedDriveActions) actions).setTimer1Days(days);
330 public static Optional<Set<DayOfWeek>> getTimer2Days(ThingActions actions) {
331 return ((BMWConnectedDriveActions) actions).getTimer2Days();
334 public static void setTimer2Days(ThingActions actions, @Nullable Set<DayOfWeek> days) {
335 ((BMWConnectedDriveActions) actions).setTimer2Days(days);
338 public static Optional<Set<DayOfWeek>> getTimer3Days(ThingActions actions) {
339 return ((BMWConnectedDriveActions) actions).getTimer3Days();
342 public static void setTimer3Days(ThingActions actions, @Nullable Set<DayOfWeek> days) {
343 ((BMWConnectedDriveActions) actions).setTimer3Days(days);
346 public static Optional<Set<DayOfWeek>> getOverrideTimerDays(ThingActions actions) {
347 return ((BMWConnectedDriveActions) actions).getOverrideTimerDays();
350 public static void setOverrideTimerDays(ThingActions actions, @Nullable Set<DayOfWeek> days) {
351 ((BMWConnectedDriveActions) actions).setOverrideTimerDays(days);
354 public static void sendChargeProfile(ThingActions actions) {
355 ((BMWConnectedDriveActions) actions).sendChargeProfile();
358 public static void cancelEditChargeProfile(ThingActions actions) {
359 ((BMWConnectedDriveActions) actions).cancelEditChargeProfile();
363 public void setThingHandler(@Nullable ThingHandler handler) {
364 if (handler instanceof VehicleHandler) {
365 this.handler = Optional.of((VehicleHandler) handler);
370 public @Nullable ThingHandler getThingHandler() {
371 return handler.get();
374 private Optional<ChargeProfileWrapper> getProfile() {
375 if (profile.isEmpty()) {
376 profile = handler.flatMap(handle -> handle.getChargeProfileWrapper());
381 private Optional<LocalTime> getTime(ProfileKey key) {
382 return getProfile().map(profile -> profile.getTime(key));
385 private void setTime(ProfileKey key, @Nullable LocalTime time) {
386 getProfile().ifPresent(profile -> profile.setTime(key, time));
389 private Optional<Boolean> getEnabled(ProfileKey key) {
390 return getProfile().map(profile -> profile.isEnabled(key));
393 private void setEnabled(ProfileKey key, @Nullable Boolean enabled) {
394 getProfile().ifPresent(profile -> profile.setEnabled(key, enabled));
397 private Optional<Set<DayOfWeek>> getDays(ProfileKey key) {
398 return getProfile().map(profile -> profile.getDays(key));
401 private void setDays(ProfileKey key, @Nullable Set<DayOfWeek> days) {
402 getProfile().ifPresent(profile -> {
403 profile.setDays(key, days);