2 * Copyright (c) 2010-2023 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.handler;
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.CHANNEL_SHADELEVEL;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.lutron.internal.protocol.OutputCommand;
21 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
22 import org.openhab.binding.lutron.internal.protocol.lip.TargetType;
23 import org.openhab.core.library.types.PercentType;
24 import org.openhab.core.library.types.StopMoveType;
25 import org.openhab.core.library.types.UpDownType;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.RefreshType;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Handler responsible for communicating with a Lutron Sivoia QS shade
39 * @author Bob Adair - Initial contribution based on Alan Tong's DimmerHandler
42 public class ShadeHandler extends LutronHandler {
43 private static final Integer PARAMETER_POSITION_UPDATE = 2; // undocumented in integration protocol guide
45 private final Logger logger = LoggerFactory.getLogger(ShadeHandler.class);
47 protected int integrationId;
48 private boolean leap = false;
50 public ShadeHandler(Thing thing) {
55 public int getIntegrationId() {
60 public void initialize() {
61 Number id = (Number) getThing().getConfiguration().get("integrationId");
63 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId");
66 integrationId = id.intValue();
67 logger.debug("Initializing Shade handler for integration ID {}", id);
69 LutronBridgeHandler bridgeHandler = getBridgeHandler();
70 if (bridgeHandler instanceof LeapBridgeHandler) {
78 protected void initDeviceState() {
79 logger.debug("Initializing device state for Shade {}", getIntegrationId());
80 Bridge bridge = getBridge();
82 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
83 } else if (bridge.getStatus() == ThingStatus.ONLINE) {
84 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "Awaiting initial response");
85 queryOutput(TargetType.SHADE, OutputCommand.ACTION_ZONELEVEL);
86 // handleUpdate() will set thing status to online when response arrives
88 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
93 public void channelLinked(ChannelUID channelUID) {
94 // Refresh state when new item is linked.
95 if (channelUID.getId().equals(CHANNEL_SHADELEVEL)) {
96 queryOutput(TargetType.SHADE, OutputCommand.ACTION_ZONELEVEL);
101 public void handleCommand(ChannelUID channelUID, Command command) {
102 if (channelUID.getId().equals(CHANNEL_SHADELEVEL)) {
103 if (command instanceof PercentType) {
104 int level = ((PercentType) command).intValue();
105 output(TargetType.SHADE, OutputCommand.ACTION_ZONELEVEL, level, null, null);
107 // LEAP may not send back a position update
108 updateState(CHANNEL_SHADELEVEL, new PercentType(level));
110 } else if (command.equals(UpDownType.UP)) {
111 output(TargetType.SHADE, OutputCommand.ACTION_STARTRAISING, null, null, null);
113 // LEAP won't send a position update when fully open
114 updateState(CHANNEL_SHADELEVEL, new PercentType(100));
116 } else if (command.equals(UpDownType.DOWN)) {
117 output(TargetType.SHADE, OutputCommand.ACTION_STARTLOWERING, null, null, null);
119 // LEAP won't send a position update when fully closed
120 updateState(CHANNEL_SHADELEVEL, new PercentType(0));
122 } else if (command.equals(StopMoveType.STOP)) {
123 output(TargetType.SHADE, OutputCommand.ACTION_STOP, null, null, null);
124 } else if (command instanceof RefreshType) {
125 queryOutput(TargetType.SHADE, OutputCommand.ACTION_ZONELEVEL);
131 public void handleUpdate(LutronCommandType type, String... parameters) {
132 if (type == LutronCommandType.OUTPUT && parameters.length >= 2) {
133 if (OutputCommand.ACTION_ZONELEVEL.toString().equals(parameters[0])) {
134 BigDecimal level = new BigDecimal(parameters[1]);
135 if (getThing().getStatus() == ThingStatus.UNKNOWN) {
136 updateStatus(ThingStatus.ONLINE);
138 logger.trace("Shade {} received zone level: {}", getIntegrationId(), level);
139 updateState(CHANNEL_SHADELEVEL, new PercentType(level));
140 } else if (OutputCommand.ACTION_POSITION_UPDATE.toString().equals(parameters[0])
141 && PARAMETER_POSITION_UPDATE.toString().equals(parameters[1]) && parameters.length >= 3) {
142 BigDecimal level = new BigDecimal(parameters[2]);
143 logger.trace("Shade {} received position update: {}", getIntegrationId(), level);
144 updateState(CHANNEL_SHADELEVEL, new PercentType(level));