]> git.basschouten.com Git - openhab-addons.git/blob
97aa60f01b0562b02bafd89d33ede08352b03d73
[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.TextValue;
18 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
19
20 import com.google.gson.annotations.SerializedName;
21
22 /**
23  * An MQTT button, following the https://www.home-assistant.io/integrations/button.mqtt/ specification.
24  *
25  * @author Cody Cutrer - Initial contribution
26  */
27 @NonNullByDefault
28 public class Button extends AbstractComponent<Button.ChannelConfiguration> {
29     public static final String BUTTON_CHANNEL_ID = "button";
30
31     /**
32      * Configuration class for MQTT component
33      */
34     static class ChannelConfiguration extends AbstractChannelConfiguration {
35         ChannelConfiguration() {
36             super("MQTT Button");
37         }
38
39         protected @Nullable Boolean optimistic;
40
41         @SerializedName("command_topic")
42         protected @Nullable String commandTopic;
43
44         @SerializedName("payload_press")
45         protected String payloadPress = "PRESS";
46     }
47
48     public Button(ComponentFactory.ComponentConfiguration componentConfiguration) {
49         super(componentConfiguration, ChannelConfiguration.class);
50
51         TextValue value = new TextValue(new String[] { channelConfiguration.payloadPress });
52
53         buildChannel(BUTTON_CHANNEL_ID, value, getName(), componentConfiguration.getUpdateListener())
54                 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
55                         channelConfiguration.getQos())
56                 .build();
57     }
58 }