]> git.basschouten.com Git - openhab-addons.git/blob
c8b307635074377194388d94cb53530bcfed0669
[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.*;
16
17 import java.math.BigDecimal;
18
19 import org.openhab.binding.lutron.internal.config.BlindConfig;
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 Lutron blinds
38  *
39  * @author Bob Adair - Initial contribution based on Alan Tong's DimmerHandler
40  */
41 public class BlindHandler extends LutronHandler {
42     private static final Integer PARAMETER_POSITION_UPDATE = 2; // undocumented in integration protocol guide
43
44     private int tiltMax = 100; // max 50 for horizontal sheer, 100 for venetian
45
46     private final Logger logger = LoggerFactory.getLogger(BlindHandler.class);
47
48     private BlindConfig config;
49
50     public BlindHandler(Thing thing) {
51         super(thing);
52     }
53
54     @Override
55     public int getIntegrationId() {
56         if (config == null) {
57             throw new IllegalStateException("handler configuration not initialized");
58         }
59         return config.integrationId;
60     }
61
62     @Override
63     public void initialize() {
64         config = getThing().getConfiguration().as(BlindConfig.class);
65         if (config.integrationId <= 0) {
66             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId configured");
67             return;
68         }
69         if (config.type == null || (!(BLIND_TYPE_SHEER.equalsIgnoreCase(config.type))
70                 && !(BLIND_TYPE_VENETIAN.equalsIgnoreCase(config.type)))) {
71             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
72                     "Parameter type not set to valid value");
73             return;
74         }
75         String blindType = config.type;
76         if (BLIND_TYPE_SHEER.equalsIgnoreCase(blindType)) {
77             tiltMax = 50;
78         }
79         logger.debug("Initializing Blind handler with type {} for integration ID {}", blindType, config.integrationId);
80         initDeviceState();
81     }
82
83     @Override
84     protected void initDeviceState() {
85         logger.debug("Initializing device state for Shade {}", getIntegrationId());
86         Bridge bridge = getBridge();
87         if (bridge == null) {
88             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
89         } else if (bridge.getStatus() == ThingStatus.ONLINE) {
90             updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "Awaiting initial response");
91             queryOutput(TargetType.BLIND, OutputCommand.ACTION_LIFTLEVEL);
92             // handleUpdate() will set thing status to online when response arrives
93             queryOutput(TargetType.BLIND, OutputCommand.ACTION_TILTLEVEL);
94         } else {
95             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
96         }
97     }
98
99     @Override
100     public void channelLinked(ChannelUID channelUID) {
101         // Refresh state when new item is linked.
102         if (channelUID.getId().equals(CHANNEL_BLINDLIFTLEVEL)) {
103             queryOutput(TargetType.BLIND, OutputCommand.ACTION_LIFTLEVEL);
104         } else if (channelUID.getId().equals(CHANNEL_BLINDTILTLEVEL)) {
105             queryOutput(TargetType.BLIND, OutputCommand.ACTION_TILTLEVEL);
106         }
107     }
108
109     @Override
110     public void handleCommand(ChannelUID channelUID, Command command) {
111         if (channelUID.getId().equals(CHANNEL_BLINDLIFTLEVEL)) {
112             handleLiftCommand(command);
113         } else if (channelUID.getId().equals(CHANNEL_BLINDTILTLEVEL)) {
114             handleTiltCommand(command);
115         }
116     }
117
118     private void handleLiftCommand(Command command) {
119         if (command instanceof PercentType liftPercent) {
120             int level = liftPercent.intValue();
121             output(TargetType.BLIND, OutputCommand.ACTION_LIFTLEVEL, level, null, null);
122         } else if (command.equals(UpDownType.UP)) {
123             output(TargetType.BLIND, OutputCommand.ACTION_STARTRAISINGLIFT, null, null, null);
124         } else if (command.equals(UpDownType.DOWN)) {
125             output(TargetType.BLIND, OutputCommand.ACTION_STARTLOWERINGLIFT, null, null, null);
126         } else if (command.equals(StopMoveType.STOP)) {
127             output(TargetType.BLIND, OutputCommand.ACTION_STOPLIFT, null, null, null);
128         } else if (command instanceof RefreshType) {
129             queryOutput(TargetType.BLIND, OutputCommand.ACTION_LIFTLEVEL);
130         }
131     }
132
133     private void handleTiltCommand(Command command) {
134         if (command instanceof PercentType tiltPercent) {
135             int level = tiltPercent.intValue();
136             output(TargetType.BLIND, OutputCommand.ACTION_TILTLEVEL, Math.min(level, tiltMax), null, null);
137         } else if (command.equals(UpDownType.UP)) {
138             output(TargetType.BLIND, OutputCommand.ACTION_STARTRAISINGTILT, null, null, null);
139         } else if (command.equals(UpDownType.DOWN)) {
140             output(TargetType.BLIND, OutputCommand.ACTION_STARTLOWERINGTILT, null, null, null);
141         } else if (command.equals(StopMoveType.STOP)) {
142             output(TargetType.BLIND, OutputCommand.ACTION_STOPTILT, null, null, null);
143         } else if (command instanceof RefreshType) {
144             queryOutput(TargetType.BLIND, OutputCommand.ACTION_TILTLEVEL);
145         }
146     }
147
148     @Override
149     public void handleUpdate(LutronCommandType type, String... parameters) {
150         if (type == LutronCommandType.OUTPUT && parameters.length >= 2) {
151             if (getThing().getStatus() == ThingStatus.UNKNOWN) {
152                 updateStatus(ThingStatus.ONLINE);
153             }
154
155             if (OutputCommand.ACTION_LIFTLEVEL.toString().equals(parameters[0])) {
156                 BigDecimal liftLevel = new BigDecimal(parameters[1]);
157                 logger.trace("Blind {} received lift level: {}", getIntegrationId(), liftLevel);
158                 updateState(CHANNEL_BLINDLIFTLEVEL, new PercentType(liftLevel));
159             } else if (OutputCommand.ACTION_TILTLEVEL.toString().equals(parameters[0])) {
160                 BigDecimal tiltLevel = new BigDecimal(parameters[1]);
161                 logger.trace("Blind {} received tilt level: {}", getIntegrationId(), tiltLevel);
162                 updateState(CHANNEL_BLINDTILTLEVEL, new PercentType(tiltLevel));
163             } else if (OutputCommand.ACTION_LIFTTILTLEVEL.toString().equals(parameters[0]) && parameters.length > 2) {
164                 BigDecimal liftLevel = new BigDecimal(parameters[1]);
165                 BigDecimal tiltLevel = new BigDecimal(parameters[2]);
166                 logger.trace("Blind {} received lift/tilt level: {} {}", getIntegrationId(), liftLevel, tiltLevel);
167                 updateState(CHANNEL_BLINDLIFTLEVEL, new PercentType(liftLevel));
168                 updateState(CHANNEL_BLINDTILTLEVEL, new PercentType(tiltLevel));
169             } else if (OutputCommand.ACTION_POSITION_UPDATE.toString().equals(parameters[0])
170                     && PARAMETER_POSITION_UPDATE.toString().equals(parameters[1]) && parameters.length >= 3) {
171                 BigDecimal level = new BigDecimal(parameters[2]);
172                 logger.trace("Blind {} received lift level position update: {}", getIntegrationId(), level);
173                 updateState(CHANNEL_BLINDLIFTLEVEL, new PercentType(level));
174             }
175         }
176     }
177 }