]> git.basschouten.com Git - openhab-addons.git/blob
8a841e8f6303eb1f281432947e11af71b3d36140
[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.lutron.internal.handler;
14
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.CHANNEL_SHADELEVEL;
16
17 import java.math.BigDecimal;
18
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;
35
36 /**
37  * Handler responsible for communicating with a Lutron Sivoia QS shade
38  *
39  * @author Bob Adair - Initial contribution based on Alan Tong's DimmerHandler
40  */
41 @NonNullByDefault
42 public class ShadeHandler extends LutronHandler {
43     private static final Integer PARAMETER_POSITION_UPDATE = 2; // undocumented in integration protocol guide
44
45     private final Logger logger = LoggerFactory.getLogger(ShadeHandler.class);
46
47     protected int integrationId;
48     private boolean leap = false;
49
50     public ShadeHandler(Thing thing) {
51         super(thing);
52     }
53
54     @Override
55     public int getIntegrationId() {
56         return integrationId;
57     }
58
59     @Override
60     public void initialize() {
61         Number id = (Number) getThing().getConfiguration().get("integrationId");
62         if (id == null) {
63             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId");
64             return;
65         }
66         integrationId = id.intValue();
67         logger.debug("Initializing Shade handler for integration ID {}", id);
68
69         LutronBridgeHandler bridgeHandler = getBridgeHandler();
70         if (bridgeHandler instanceof LeapBridgeHandler) {
71             leap = true;
72         }
73
74         initDeviceState();
75     }
76
77     @Override
78     protected void initDeviceState() {
79         logger.debug("Initializing device state for Shade {}", getIntegrationId());
80         Bridge bridge = getBridge();
81         if (bridge == null) {
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
87         } else {
88             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
89         }
90     }
91
92     @Override
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);
97         }
98     }
99
100     @Override
101     public void handleCommand(ChannelUID channelUID, Command command) {
102         if (channelUID.getId().equals(CHANNEL_SHADELEVEL)) {
103             if (command instanceof PercentType shadePercent) {
104                 int level = shadePercent.intValue();
105                 output(TargetType.SHADE, OutputCommand.ACTION_ZONELEVEL, level, null, null);
106                 if (leap) {
107                     // LEAP may not send back a position update
108                     updateState(CHANNEL_SHADELEVEL, new PercentType(level));
109                 }
110             } else if (command.equals(UpDownType.UP)) {
111                 output(TargetType.SHADE, OutputCommand.ACTION_STARTRAISING, null, null, null);
112                 if (leap) {
113                     // LEAP won't send a position update when fully open
114                     updateState(CHANNEL_SHADELEVEL, new PercentType(100));
115                 }
116             } else if (command.equals(UpDownType.DOWN)) {
117                 output(TargetType.SHADE, OutputCommand.ACTION_STARTLOWERING, null, null, null);
118                 if (leap) {
119                     // LEAP won't send a position update when fully closed
120                     updateState(CHANNEL_SHADELEVEL, new PercentType(0));
121                 }
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);
126             }
127         }
128     }
129
130     @Override
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);
137                 }
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));
145             }
146         }
147     }
148 }