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