]> git.basschouten.com Git - openhab-addons.git/blob
45cbbef30abcad3d77d5605f14044af9170c9944
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 import java.util.stream.Collectors;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.SetpointMode;
22 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
23 import org.openhab.binding.netatmo.internal.handler.capability.EnergyCapability;
24 import org.openhab.core.automation.annotation.ActionInput;
25 import org.openhab.core.automation.annotation.RuleAction;
26 import org.openhab.core.thing.binding.ThingActions;
27 import org.openhab.core.thing.binding.ThingActionsScope;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link RoomActions} defines thing actions for RoomHandler.
34  *
35  * @author Markus Dillmann - Initial contribution
36  */
37 @ThingActionsScope(name = "netatmo")
38 @NonNullByDefault
39 public class RoomActions implements ThingActions {
40     private final Logger logger = LoggerFactory.getLogger(RoomActions.class);
41     private static final Set<SetpointMode> ALLOWED_MODES = Set.of(SetpointMode.MAX, SetpointMode.MANUAL,
42             SetpointMode.HOME);
43
44     private @Nullable CommonInterface handler;
45
46     public RoomActions() {
47         logger.debug("Netatmo RoomActions service created");
48     }
49
50     private Optional<EnergyCapability> getEnergyCapability() {
51         CommonInterface localHandler = handler;
52         if (localHandler != null) {
53             return localHandler.getHomeCapability(EnergyCapability.class);
54         }
55         return Optional.empty();
56     }
57
58     @Override
59     public void setThingHandler(@Nullable ThingHandler handler) {
60         if (handler instanceof CommonInterface commonHandler) {
61             this.handler = commonHandler;
62         }
63     }
64
65     @Override
66     public @Nullable ThingHandler getThingHandler() {
67         return (ThingHandler) handler;
68     }
69
70     @RuleAction(label = "@text/actionSetThermRoomTempSetpointLabel", description = "@text/actionSetThermRoomTempSetpointDesc")
71     public void setThermRoomTempSetpoint(
72             @ActionInput(name = "temp", label = "@text/actionInputSetpointLabel", description = "@text/actionInputSetpointDesc") @Nullable Double temp,
73             @ActionInput(name = "endtime", label = "@text/actionInputEndtimeLabel", description = "@text/actionInputEndtimeDesc") @Nullable Long endTime) {
74         CommonInterface roomHandler = handler;
75         if (roomHandler == null) {
76             logger.info("Handler not set for room thing actions.");
77             return;
78         } else if (temp == null) {
79             logger.info("Temperature is required, action ignored");
80             return;
81         } else if (endTime == null) {
82             logger.info("Temperature provided but no endtime given, action ignored");
83             return;
84         }
85         getEnergyCapability()
86                 .ifPresent(cap -> cap.setRoomThermTemp(roomHandler.getId(), SetpointMode.MANUAL, endTime, temp));
87     }
88
89     @RuleAction(label = "@text/actionSetThermRoomModeSetpointLabel", description = "@text/actionSetThermRoomModeSetpointDesc")
90     public void setThermRoomModeSetpoint(
91             @ActionInput(name = "mode", label = "@text/actionInputModeLabel", description = "@text/actionInputModeDesc") @Nullable String mode,
92             @ActionInput(name = "endtime", label = "@text/actionInputEndtimeLabel", description = "@text/actionInputEndtimeDesc") @Nullable Long endTime) {
93         CommonInterface roomHandler = handler;
94         if (roomHandler == null) {
95             logger.info("Handler not set for room thing actions.");
96             return;
97         } else if (mode == null) {
98             logger.info("Mode is required, action ignored");
99             return;
100         }
101
102         SetpointMode targetMode = SetpointMode.UNKNOWN;
103         try {
104             targetMode = SetpointMode.valueOf(mode);
105             if (!ALLOWED_MODES.contains(targetMode)) {
106                 logger.info("Mode can only be {} for a room",
107                         ALLOWED_MODES.stream().map(s -> s.name()).collect(Collectors.joining(", ")));
108                 return;
109             }
110         } catch (IllegalArgumentException e) {
111             logger.info("Invalid mode passed : {} - {}", mode, e.getMessage());
112             return;
113         }
114
115         Long targetEndTime = endTime;
116         if (SetpointMode.HOME.equals(targetMode)) {
117             targetEndTime = 0L;
118         } else if (targetEndTime == null) {
119             logger.info("No endtime given, action ignored");
120             return;
121         }
122
123         long setpointEnd = targetEndTime;
124         SetpointMode setpointMode = targetMode;
125         getEnergyCapability().ifPresent(cap -> cap.setRoomThermTemp(roomHandler.getId(), setpointMode, setpointEnd, 0));
126     }
127
128     public static void setThermRoomTempSetpoint(ThingActions actions, @Nullable Double temp, @Nullable Long endTime) {
129         ((RoomActions) actions).setThermRoomTempSetpoint(temp, endTime);
130     }
131
132     public static void setThermRoomModeSetpoint(ThingActions actions, @Nullable String mode, @Nullable Long endTime) {
133         ((RoomActions) actions).setThermRoomModeSetpoint(mode, endTime);
134     }
135 }