]> git.basschouten.com Git - openhab-addons.git/blob
1ab991f3ac30f5c3c6e2498bd10cb699d70294ec
[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 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     private Optional<EnergyCapability> energy = Optional.empty();
46
47     public RoomActions() {
48         logger.debug("Netatmo RoomActions service created");
49     }
50
51     @Override
52     public void setThingHandler(@Nullable ThingHandler handler) {
53         if (handler instanceof CommonInterface) {
54             CommonInterface commonHandler = (CommonInterface) handler;
55             this.handler = commonHandler;
56             energy = commonHandler.getHomeCapability(EnergyCapability.class);
57         }
58     }
59
60     @Override
61     public @Nullable ThingHandler getThingHandler() {
62         return (ThingHandler) handler;
63     }
64
65     @RuleAction(label = "@text/actionSetThermRoomTempSetpointLabel", description = "@text/actionSetThermRoomTempSetpointDesc")
66     public void setThermRoomTempSetpoint(
67             @ActionInput(name = "temp", label = "@text/actionInputSetpointLabel", description = "@text/actionInputSetpointDesc") @Nullable Double temp,
68             @ActionInput(name = "endtime", label = "@text/actionInputEndtimeLabel", description = "@text/actionInputEndtimeDesc") @Nullable Long endTime) {
69         CommonInterface roomHandler = handler;
70         if (roomHandler == null) {
71             logger.info("Handler not set for room thing actions.");
72             return;
73         } else if (temp == null) {
74             logger.info("Temperature is required, action ignored");
75             return;
76         } else if (endTime == null) {
77             logger.info("Temperature provided but no endtime given, action ignored");
78             return;
79         }
80         energy.ifPresent(cap -> cap.setRoomThermTemp(roomHandler.getId(), temp, endTime, SetpointMode.MANUAL));
81     }
82
83     @RuleAction(label = "@text/actionSetThermRoomModeSetpointLabel", description = "@text/actionSetThermRoomModeSetpointDesc")
84     public void setThermRoomModeSetpoint(
85             @ActionInput(name = "mode", label = "@text/actionInputModeLabel", description = "@text/actionInputModeDesc") @Nullable String mode,
86             @ActionInput(name = "endtime", label = "@text/actionInputEndtimeLabel", description = "@text/actionInputEndtimeDesc") @Nullable Long endTime) {
87         CommonInterface roomHandler = handler;
88         if (roomHandler == null) {
89             logger.info("Handler not set for room thing actions.");
90             return;
91         } else if (mode == null) {
92             logger.info("Mode is required, action ignored");
93             return;
94         }
95
96         SetpointMode targetMode = SetpointMode.UNKNOWN;
97         try {
98             targetMode = SetpointMode.valueOf(mode);
99             if (!ALLOWED_MODES.contains(targetMode)) {
100                 logger.info("Mode can only be {} for a room",
101                         ALLOWED_MODES.stream().map(s -> s.name()).collect(Collectors.joining(", ")));
102                 return;
103             }
104         } catch (IllegalArgumentException e) {
105             logger.info("Invalid mode passed : {} - {}", mode, e.getMessage());
106             return;
107         }
108
109         Long targetEndTime = endTime;
110         if (SetpointMode.HOME.equals(targetMode)) {
111             targetEndTime = 0L;
112         } else if (targetEndTime == null) {
113             logger.info("No endtime given, action ignored");
114             return;
115         }
116
117         long setpointEnd = targetEndTime;
118         SetpointMode setpointMode = targetMode;
119         energy.ifPresent(cap -> cap.setRoomThermTemp(roomHandler.getId(), 0, setpointEnd, setpointMode));
120     }
121
122     public static void setThermRoomTempSetpoint(ThingActions actions, @Nullable Double temp, @Nullable Long endTime) {
123         ((RoomActions) actions).setThermRoomTempSetpoint(temp, endTime);
124     }
125
126     public static void setThermRoomModeSetpoint(ThingActions actions, @Nullable String mode, @Nullable Long endTime) {
127         ((RoomActions) actions).setThermRoomModeSetpoint(mode, endTime);
128     }
129 }