2 * Copyright (c) 2010-2024 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.mqtt.homeassistant.internal.component;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.mqtt.generic.values.RollershutterValue;
18 import org.openhab.binding.mqtt.generic.values.TextValue;
19 import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannel;
20 import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannelType;
21 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
22 import org.openhab.core.library.types.StopMoveType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.library.types.UpDownType;
25 import org.openhab.core.thing.type.AutoUpdatePolicy;
27 import com.google.gson.annotations.SerializedName;
30 * A MQTT Cover component, following the https://www.home-assistant.io/integrations/cover.mqtt specification.
32 * Supports reporting state and/or position, and commanding OPEN/CLOSE/STOP
34 * Does not yet support tilt or covers that don't go from 0-100.
36 * @author David Graeff - Initial contribution
37 * @author Cody Cutrer - Add support for position and discrete state strings
40 public class Cover extends AbstractComponent<Cover.ChannelConfiguration> {
41 public static final String COVER_CHANNEL_ID = "cover";
42 public static final String STATE_CHANNEL_ID = "state";
45 * Configuration class for MQTT component
47 static class ChannelConfiguration extends AbstractChannelConfiguration {
48 ChannelConfiguration() {
52 protected @Nullable Boolean optimistic;
54 @SerializedName("state_topic")
55 protected @Nullable String stateTopic;
56 @SerializedName("command_topic")
57 protected @Nullable String commandTopic;
58 @SerializedName("payload_open")
59 protected String payloadOpen = "OPEN";
60 @SerializedName("payload_close")
61 protected String payloadClose = "CLOSE";
62 @SerializedName("payload_stop")
63 protected String payloadStop = "STOP";
64 @SerializedName("position_closed")
65 protected int positionClosed = 0;
66 @SerializedName("position_open")
67 protected int positionOpen = 100;
68 @SerializedName("position_template")
69 protected @Nullable String positionTemplate;
70 @SerializedName("position_topic")
71 protected @Nullable String positionTopic;
72 @SerializedName("set_position_template")
73 protected @Nullable String setPositionTemplate;
74 @SerializedName("set_position_topic")
75 protected @Nullable String setPositionTopic;
76 @SerializedName("state_closed")
77 protected String stateClosed = "closed";
78 @SerializedName("state_closing")
79 protected String stateClosing = "closing";
80 @SerializedName("state_open")
81 protected String stateOpen = "open";
82 @SerializedName("state_opening")
83 protected String stateOpening = "opening";
84 @SerializedName("state_stopped")
85 protected String stateStopped = "stopped";
89 ComponentChannel stateChannel = null;
91 public Cover(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
92 super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
94 boolean optimistic = false;
95 Boolean localOptimistic = channelConfiguration.optimistic;
96 if (localOptimistic != null && localOptimistic == true
97 || channelConfiguration.stateTopic == null && channelConfiguration.positionTopic == null) {
100 String stateTopic = channelConfiguration.stateTopic;
102 // State can indicate additional information than just
103 // the current position, so expose it as a separate channel
104 if (stateTopic != null) {
105 TextValue value = new TextValue(new String[] { channelConfiguration.stateClosed,
106 channelConfiguration.stateClosing, channelConfiguration.stateOpen,
107 channelConfiguration.stateOpening, channelConfiguration.stateStopped });
108 buildChannel(STATE_CHANNEL_ID, ComponentChannelType.STRING, value, "State",
109 componentConfiguration.getUpdateListener()).stateTopic(stateTopic).isAdvanced(true).build();
112 if (channelConfiguration.commandTopic != null) {
113 hiddenChannels.add(stateChannel = buildChannel(STATE_CHANNEL_ID, ComponentChannelType.STRING,
114 new TextValue(), "State", componentConfiguration.getUpdateListener())
115 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
116 channelConfiguration.getQos())
119 // no command topic. we need to make sure we send
120 // integers for open and close
121 channelConfiguration.payloadOpen = String.valueOf(channelConfiguration.positionOpen);
122 channelConfiguration.payloadClose = String.valueOf(channelConfiguration.positionClosed);
125 // We will either have positionTopic or stateTopic.
126 // positionTopic is more useful, but if we only have stateTopic,
127 // still build a Rollershutter channel so that UP/DOWN/STOP
128 // commands can be sent
129 String rollershutterStateTopic = channelConfiguration.positionTopic;
130 String stateTemplate = channelConfiguration.positionTemplate;
131 if (rollershutterStateTopic == null) {
132 rollershutterStateTopic = stateTopic;
133 stateTemplate = channelConfiguration.getValueTemplate();
135 String rollershutterCommandTopic = channelConfiguration.setPositionTopic;
136 if (rollershutterCommandTopic == null) {
137 rollershutterCommandTopic = channelConfiguration.commandTopic;
140 boolean inverted = channelConfiguration.positionOpen > channelConfiguration.positionClosed;
141 final RollershutterValue value = new RollershutterValue(channelConfiguration.payloadOpen,
142 channelConfiguration.payloadClose, channelConfiguration.payloadStop, channelConfiguration.stateOpen,
143 channelConfiguration.stateClosed, inverted, channelConfiguration.setPositionTopic == null);
145 buildChannel(COVER_CHANNEL_ID, ComponentChannelType.ROLLERSHUTTER, value, "Cover",
146 componentConfiguration.getUpdateListener()).stateTopic(rollershutterStateTopic, stateTemplate)
147 .commandTopic(rollershutterCommandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos())
148 .commandFilter(command -> {
149 if (stateChannel == null) {
152 // If we have a state channel, and this is UP/DOWN/STOP, then
153 // we need to send the command to _that_ channel's topic, not
154 // the position topic.
155 if (command instanceof UpDownType || command instanceof StopMoveType) {
156 command = new StringType(value.getMQTTpublishValue(command, false));
157 stateChannel.getState().publishValue(command);
161 }).withAutoUpdatePolicy(optimistic ? AutoUpdatePolicy.RECOMMEND : null).build();