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.loxone.internal.controls;
15 import static org.openhab.binding.loxone.internal.LxBindingConstants.*;
17 import java.io.IOException;
19 import org.openhab.binding.loxone.internal.types.LxUuid;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.type.ChannelTypeUID;
23 import org.openhab.core.types.Command;
26 * An UpDownDigital type of control on Loxone Miniserver.
28 * According to Loxone API documentation, UpDownDigital control is a virtual input that is digital and has an input type
29 * up-down buttons. Buttons act like on an integrated up/down arrows switch - only one direction can be active at a
30 * time. Pushing button in one direction will automatically set the other direction to off.
31 * This control has no states and can only accept commands. Only up/down on/off commands are generated. Pulse
32 * commands are not supported, because of lack of corresponding feature in openHAB. Pulse can be emulated by quickly
33 * alternating between ON and OFF commands. Because this control has no states, there will be no openHAB state changes
34 * triggered by the Miniserver and we need to take care of updating the states inside this class.
36 * @author Pawel Pieczul - initial contribution
39 class LxControlUpDownDigital extends LxControl {
41 static class Factory extends LxControlInstance {
43 LxControl create(LxUuid uuid) {
44 return new LxControlUpDownDigital(uuid);
49 return "updowndigital";
53 private static final String CMD_UP_ON = "UpOn";
54 private static final String CMD_UP_OFF = "UpOff";
55 private static final String CMD_DOWN_ON = "DownOn";
56 private static final String CMD_DOWN_OFF = "DownOff";
58 private OnOffType upState = OnOffType.OFF;
59 private OnOffType downState = OnOffType.OFF;
60 private ChannelUID upChannelId;
61 private ChannelUID downChannelId;
63 LxControlUpDownDigital(LxUuid uuid) {
68 public void initialize(LxControlConfig config) {
69 initialize(config, " / Up", "Up/Down Digital: Up", " / Down", "Up/Down Digital: Down");
72 void initialize(LxControlConfig config, String upChannelLabel, String upChannelDescription, String downChannelLabel,
73 String downChannelDescription) {
74 super.initialize(config);
75 upChannelId = addChannel("Switch", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_SWITCH),
76 defaultChannelLabel + upChannelLabel, upChannelDescription, tags, this::handleUpCommands,
78 downChannelId = addChannel("Switch", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_SWITCH),
79 defaultChannelLabel + downChannelLabel, downChannelDescription, tags, this::handleDownCommands,
83 private void handleUpCommands(Command command) throws IOException {
84 if (command instanceof OnOffType onOffCommand) {
85 if (onOffCommand == OnOffType.ON && upState == OnOffType.OFF) {
86 setStates(OnOffType.ON, OnOffType.OFF);
87 sendAction(CMD_UP_ON);
88 } else if (upState == OnOffType.ON) {
89 setStates(OnOffType.OFF, OnOffType.OFF);
90 sendAction(CMD_UP_OFF);
95 private void handleDownCommands(Command command) throws IOException {
96 if (command instanceof OnOffType onOffCommand) {
97 if (onOffCommand == OnOffType.ON && downState == OnOffType.OFF) {
98 setStates(OnOffType.OFF, OnOffType.ON);
99 sendAction(CMD_DOWN_ON);
100 } else if (downState == OnOffType.ON) {
101 setStates(OnOffType.OFF, OnOffType.OFF);
102 sendAction(CMD_DOWN_OFF);
107 private void setStates(OnOffType upState, OnOffType downState) {
108 this.upState = upState;
109 this.downState = downState;
110 setChannelState(upChannelId, upState);
111 setChannelState(downChannelId, downState);