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