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.lutron.internal.protocol;
15 import java.util.Locale;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.lutron.internal.handler.LeapBridgeHandler;
20 import org.openhab.binding.lutron.internal.protocol.leap.CommandType;
21 import org.openhab.binding.lutron.internal.protocol.leap.LeapCommand;
22 import org.openhab.binding.lutron.internal.protocol.leap.Request;
23 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
24 import org.openhab.binding.lutron.internal.protocol.lip.LutronOperation;
25 import org.openhab.binding.lutron.internal.protocol.lip.TargetType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Lutron OUTPUT command object
32 * @author Bob Adair - Initial contribution
35 public class OutputCommand extends LutronCommandNew {
36 // shade, blind, dimmer defs
37 public static final Integer ACTION_ZONELEVEL = 1;
38 public static final Integer ACTION_STARTRAISING = 2;
39 public static final Integer ACTION_STARTLOWERING = 3;
40 public static final Integer ACTION_STOP = 4;
41 public static final Integer ACTION_POSITION_UPDATE = 32; // For shades/blinds. Undocumented in protocol guide.
44 public static final Integer ACTION_LIFTLEVEL = 1;
45 public static final Integer ACTION_TILTLEVEL = 9;
46 public static final Integer ACTION_LIFTTILTLEVEL = 10;
47 public static final Integer ACTION_STARTRAISINGTILT = 11;
48 public static final Integer ACTION_STARTLOWERINGTILT = 12;
49 public static final Integer ACTION_STOPTILT = 13;
50 public static final Integer ACTION_STARTRAISINGLIFT = 14;
51 public static final Integer ACTION_STARTLOWERINGLIFT = 15;
52 public static final Integer ACTION_STOPLIFT = 16;
55 public static final Integer ACTION_STATE = 1;
56 public static final Integer ACTION_PULSE = 6;
58 private final Logger logger = LoggerFactory.getLogger(OutputCommand.class);
60 private final Integer action;
61 private final @Nullable Number parameter;
62 private final @Nullable LutronDuration fadeTime;
63 private final @Nullable LutronDuration delayTime;
64 private final FanSpeedType fanSpeed;
67 * OutputCommand constructor
71 * @param integrationId
77 public OutputCommand(TargetType targetType, LutronOperation operation, Integer integrationId, Integer action,
78 @Nullable Number parameter, @Nullable LutronDuration fadeTime, @Nullable LutronDuration delayTime) {
79 super(targetType, operation, LutronCommandType.OUTPUT, integrationId);
81 this.parameter = parameter;
82 if (parameter != null) {
83 this.fanSpeed = FanSpeedType.toFanSpeedType(parameter.intValue());
85 this.fanSpeed = FanSpeedType.OFF;
87 this.fadeTime = fadeTime;
88 this.delayTime = delayTime;
92 * OutputCommand constructor for fan commands
96 * @param integrationId
102 public OutputCommand(TargetType targetType, LutronOperation operation, Integer integrationId, Integer action,
103 FanSpeedType fanSpeed, @Nullable LutronDuration fadeTime, @Nullable LutronDuration delayTime) {
104 super(targetType, operation, LutronCommandType.OUTPUT, integrationId);
105 this.action = action;
106 this.fanSpeed = fanSpeed;
107 this.parameter = fanSpeed.speed();
108 this.fadeTime = fadeTime;
109 this.delayTime = delayTime;
113 public String lipCommand() {
114 StringBuilder builder = new StringBuilder().append(operation).append(commandType);
115 builder.append(',').append(integrationId);
116 builder.append(',').append(action);
118 if (parameter != null && targetType == TargetType.CCO && action.equals(OutputCommand.ACTION_PULSE)) {
119 builder.append(',').append(String.format(Locale.ROOT, "%.2f", parameter));
120 } else if (parameter != null) {
121 builder.append(',').append(parameter);
124 if (fadeTime != null) {
125 builder.append(',').append(fadeTime);
126 } else if (fadeTime == null && delayTime != null) {
127 // must add 0 placeholder here in order to set delay time
128 builder.append(',').append("0");
130 if (delayTime != null) {
131 builder.append(',').append(delayTime);
134 return builder.toString();
138 public @Nullable LeapCommand leapCommand(LeapBridgeHandler bridgeHandler, @Nullable Integer leapZone) {
140 Number parameter = this.parameter;
142 if (leapZone == null) {
148 if (operation == LutronOperation.QUERY) {
149 if (action.equals(OutputCommand.ACTION_ZONELEVEL)) {
150 return new LeapCommand(Request.getZoneStatus(zone));
152 logger.debug("Ignoring unsupported query action");
155 } else if (operation == LutronOperation.EXECUTE) {
156 if (targetType == TargetType.SWITCH) {
157 if (action.equals(OutputCommand.ACTION_ZONELEVEL) && parameter != null) {
158 return new LeapCommand(Request.goToLevel(zone, parameter.intValue()));
160 logger.debug("Ignoring unsupported switch action");
163 } else if (targetType == TargetType.DIMMER) {
164 if (action.equals(OutputCommand.ACTION_ZONELEVEL) && parameter != null) {
165 if (fadeTime == null && delayTime == null) {
166 return new LeapCommand(Request.goToLevel(zone, parameter.intValue()));
168 LutronDuration fade = (fadeTime == null) ? new LutronDuration(0) : fadeTime;
169 LutronDuration delay = (delayTime == null) ? new LutronDuration(0) : delayTime;
170 return new LeapCommand(Request.goToDimmedLevel(zone, parameter.intValue(), fade.asLeapString(),
171 delay.asLeapString()));
174 logger.debug("Ignoring unsupported dimmer action");
177 } else if (targetType == TargetType.FAN) {
178 if (action.equals(OutputCommand.ACTION_ZONELEVEL)) {
179 return new LeapCommand(Request.goToFanSpeed(zone, fanSpeed));
181 logger.debug("Ignoring unsupported fan action");
184 } else if (targetType == TargetType.SHADE) {
185 if (action.equals(OutputCommand.ACTION_ZONELEVEL) && parameter != null) {
186 return new LeapCommand(Request.goToLevel(zone, parameter.intValue()));
187 } else if (action.equals(OutputCommand.ACTION_STARTRAISING)) {
188 return new LeapCommand(Request.zoneCommand(zone, CommandType.RAISE));
189 } else if (action.equals(OutputCommand.ACTION_STARTLOWERING)) {
190 return new LeapCommand(Request.zoneCommand(zone, CommandType.LOWER));
191 } else if (action.equals(OutputCommand.ACTION_STOP)) {
192 return new LeapCommand(Request.zoneCommand(zone, CommandType.STOP));
194 logger.debug("Ignoring unsupported shade action");
198 logger.debug("Ignoring unsupported target type: {}", targetType);
202 logger.debug("Ignoring unsupported operation: {}", operation);
208 public String toString() {