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