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.*;
17 import java.math.BigDecimal;
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;
37 * Handler responsible for communicating with Lutron blinds
39 * @author Bob Adair - Initial contribution based on Alan Tong's DimmerHandler
41 public class BlindHandler extends LutronHandler {
42 private static final Integer PARAMETER_POSITION_UPDATE = 2; // undocumented in integration protocol guide
44 private int tiltMax = 100; // max 50 for horizontal sheer, 100 for venetian
46 private final Logger logger = LoggerFactory.getLogger(BlindHandler.class);
48 private BlindConfig config;
50 public BlindHandler(Thing thing) {
55 public int getIntegrationId() {
57 throw new IllegalStateException("handler configuration not initialized");
59 return config.integrationId;
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");
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");
75 String blindType = config.type;
76 if (BLIND_TYPE_SHEER.equalsIgnoreCase(blindType)) {
79 logger.debug("Initializing Blind handler with type {} for integration ID {}", blindType, config.integrationId);
84 protected void initDeviceState() {
85 logger.debug("Initializing device state for Shade {}", getIntegrationId());
86 Bridge bridge = getBridge();
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);
95 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
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);
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);
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);
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);
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);
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));