]> git.basschouten.com Git - openhab-addons.git/blob
192735630eba132fdc1acdd84789e0f878a01d3d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.netatmo.internal.action;
14
15 import java.util.Optional;
16 import java.util.Set;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.SetpointMode;
21 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
22 import org.openhab.binding.netatmo.internal.handler.capability.EnergyCapability;
23 import org.openhab.core.automation.annotation.ActionInput;
24 import org.openhab.core.automation.annotation.RuleAction;
25 import org.openhab.core.thing.binding.ThingActions;
26 import org.openhab.core.thing.binding.ThingActionsScope;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link RoomActions} defines thing actions for RoomHandler.
33  *
34  * @author Markus Dillmann - Initial contribution
35  */
36 @ThingActionsScope(name = "netatmo")
37 @NonNullByDefault
38 public class RoomActions implements ThingActions {
39     private final Logger logger = LoggerFactory.getLogger(RoomActions.class);
40     private static final Set<SetpointMode> ALLOWED_MODES = Set.of(SetpointMode.MAX, SetpointMode.MANUAL,
41             SetpointMode.HOME);
42
43     private @Nullable CommonInterface handler;
44     private Optional<EnergyCapability> energy = Optional.empty();
45
46     public RoomActions() {
47         logger.debug("Netatmo RoomActions service created");
48     }
49
50     @Override
51     public void setThingHandler(@Nullable ThingHandler handler) {
52         if (handler instanceof CommonInterface) {
53             CommonInterface commonHandler = (CommonInterface) handler;
54             this.handler = commonHandler;
55             energy = commonHandler.getHomeCapability(EnergyCapability.class);
56         }
57     }
58
59     @Override
60     public @Nullable ThingHandler getThingHandler() {
61         return (ThingHandler) handler;
62     }
63
64     /**
65      * The setThermpoint room thing action
66      */
67     @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
68     public void setThermpoint(
69             @ActionInput(name = "setpoint", label = "@text/actionInputSetpointLabel", description = "@text/actionInputSetpointDesc") @Nullable Double temp,
70             @ActionInput(name = "endtime", label = "@text/actionInputEndtimeLabel", description = "@text/actionInputEndtimeDesc") @Nullable Long endTime) {
71         setThermpoint(temp, endTime, "MANUAL");
72     }
73
74     @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
75     public void seThermpoint(
76             @ActionInput(name = "mode", label = "@text/actionInputModeLabel", description = "@text/actionInputModeDesc") @Nullable String mode,
77             @ActionInput(name = "endtime", label = "@text/actionInputEndtimeLabel", description = "@text/actionInputEndtimeDesc") @Nullable Long endTime) {
78         setThermpoint(null, endTime, mode);
79     }
80
81     @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
82     public void setThermpoint(
83             @ActionInput(name = "setpoint", label = "@text/actionInputSetpointLabel", description = "@text/actionInputSetpointDesc") @Nullable Double temp,
84             @ActionInput(name = "endtime", label = "@text/actionInputEndtimeLabel", description = "@text/actionInputEndtimeDesc") @Nullable Long endTime,
85             @ActionInput(name = "mode", label = "@text/actionInputModeLabel", description = "@text/actionInputModeDesc") @Nullable String mode) {
86         CommonInterface roomHandler = handler;
87         if (roomHandler != null) {
88             String roomId = roomHandler.getId();
89             SetpointMode targetMode = SetpointMode.UNKNOWN;
90             Long targetEndTime = endTime;
91             Double targetTemp = temp;
92             if (mode != null) {
93                 try {
94                     targetMode = SetpointMode.valueOf(mode);
95                     if (!ALLOWED_MODES.contains(targetMode)) {
96                         logger.info("Mode can only be MAX, HOME or MANUAL for a room");
97                         return;
98                     }
99                 } catch (IllegalArgumentException e) {
100                     logger.info("Invalid mode passed : {} - {}", mode, e.getMessage());
101                     return;
102                 }
103             }
104             if (temp != null) {
105                 logger.debug("Temperature provided, mode forced to MANUAL.");
106                 targetMode = SetpointMode.MANUAL;
107                 if (targetEndTime == null) {
108                     logger.info("Temperature provided but no endtime given, action ignored");
109                     return;
110                 }
111             } else {
112                 if (SetpointMode.HOME.equals(targetMode)) {
113                     targetEndTime = 0L;
114                     targetTemp = 0.0;
115                 } else {
116                     logger.info("mode is required if no temperature setpoint provided");
117                     return;
118                 }
119             }
120
121             try {
122                 double setpointTemp = targetTemp != null ? targetTemp : 0;
123                 long setpointEnd = targetEndTime;
124                 SetpointMode setpointMode = targetMode;
125                 energy.ifPresent(cap -> cap.setRoomThermTemp(roomId, setpointTemp, setpointEnd, setpointMode));
126             } catch (IllegalArgumentException e) {
127                 logger.debug("Ignoring setRoomThermpoint command due to illegal argument exception: {}",
128                         e.getMessage());
129             }
130         } else {
131             logger.info("Handler not set for room thing actions.");
132         }
133     }
134
135     /**
136      * Static setThermpoint method for Rules DSL backward compatibility
137      */
138     public static void setThermpoint(ThingActions actions, @Nullable Double temp, @Nullable Long endTime,
139             @Nullable String mode) {
140         ((RoomActions) actions).setThermpoint(temp, endTime, mode);
141     }
142
143     public static void setThermpoint(ThingActions actions, @Nullable Double temp, @Nullable Long endTime) {
144         setThermpoint(actions, temp, endTime, null);
145     }
146
147     public static void setThermpoint(ThingActions actions, @Nullable String mode, @Nullable Long endTime) {
148         setThermpoint(actions, null, endTime, mode);
149     }
150 }