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.netatmo.internal.action;
15 import java.util.Optional;
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;
32 * The {@link RoomActions} defines thing actions for RoomHandler.
34 * @author Markus Dillmann - Initial contribution
36 @ThingActionsScope(name = "netatmo")
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,
43 private @Nullable CommonInterface handler;
44 private Optional<EnergyCapability> energy = Optional.empty();
46 public RoomActions() {
47 logger.debug("Netatmo RoomActions service created");
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);
60 public @Nullable ThingHandler getThingHandler() {
61 return (ThingHandler) handler;
65 * The setThermpoint room thing action
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");
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);
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;
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");
99 } catch (IllegalArgumentException e) {
100 logger.info("Invalid mode passed : {} - {}", mode, e.getMessage());
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");
112 if (SetpointMode.HOME.equals(targetMode)) {
116 logger.info("mode is required if no temperature setpoint provided");
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: {}",
131 logger.info("Handler not set for room thing actions.");
136 * Static setThermpoint method for Rules DSL backward compatibility
138 public static void setThermpoint(ThingActions actions, @Nullable Double temp, @Nullable Long endTime,
139 @Nullable String mode) {
140 ((RoomActions) actions).setThermpoint(temp, endTime, mode);
143 public static void setThermpoint(ThingActions actions, @Nullable Double temp, @Nullable Long endTime) {
144 setThermpoint(actions, temp, endTime, null);
147 public static void setThermpoint(ThingActions actions, @Nullable String mode, @Nullable Long endTime) {
148 setThermpoint(actions, null, endTime, mode);