]> git.basschouten.com Git - openhab-addons.git/blob
2d636e95254f5e9729362610c62eb4da0dc21f99
[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 java.util.List;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
20 import org.openhab.binding.mqtt.generic.values.OnOffValue;
21 import org.openhab.binding.mqtt.generic.values.Value;
22 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
23 import org.openhab.binding.mqtt.homeassistant.internal.listener.ExpireUpdateStateListener;
24 import org.openhab.binding.mqtt.homeassistant.internal.listener.OffDelayUpdateStateListener;
25 import org.openhab.core.thing.type.AutoUpdatePolicy;
26
27 import com.google.gson.annotations.SerializedName;
28
29 /**
30  * A MQTT BinarySensor, following the https://www.home-assistant.io/components/binary_sensor.mqtt/ specification.
31  *
32  * @author David Graeff - Initial contribution
33  */
34 @NonNullByDefault
35 public class BinarySensor extends AbstractComponent<BinarySensor.ChannelConfiguration> {
36     public static final String SENSOR_CHANNEL_ID = "sensor"; // Randomly chosen channel "ID"
37
38     /**
39      * Configuration class for MQTT component
40      */
41     static class ChannelConfiguration extends AbstractChannelConfiguration {
42         ChannelConfiguration() {
43             super("MQTT Binary Sensor");
44         }
45
46         @SerializedName("device_class")
47         protected @Nullable String deviceClass;
48         @SerializedName("force_update")
49         protected boolean forceUpdate = false;
50         @SerializedName("expire_after")
51         protected @Nullable Integer expireAfter;
52         @SerializedName("off_delay")
53         protected @Nullable Integer offDelay;
54
55         @SerializedName("state_topic")
56         protected String stateTopic = "";
57         @SerializedName("payload_on")
58         protected String payloadOn = "ON";
59         @SerializedName("payload_off")
60         protected String payloadOff = "OFF";
61
62         @SerializedName("json_attributes_topic")
63         protected @Nullable String jsonAttributesTopic;
64         @SerializedName("json_attributes_template")
65         protected @Nullable String jsonAttributesTemplate;
66         @SerializedName("json_attributes")
67         protected @Nullable List<String> jsonAttributes;
68     }
69
70     public BinarySensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
71         super(componentConfiguration, ChannelConfiguration.class);
72
73         OnOffValue value = new OnOffValue(channelConfiguration.payloadOn, channelConfiguration.payloadOff);
74
75         buildChannel(SENSOR_CHANNEL_ID, value, "value", getListener(componentConfiguration, value))
76                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
77                 .withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();
78     }
79
80     private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
81             Value value) {
82         ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
83
84         if (channelConfiguration.expireAfter != null) {
85             updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
86                     componentConfiguration.getTracker(), componentConfiguration.getScheduler());
87         }
88         if (channelConfiguration.offDelay != null) {
89             updateListener = new OffDelayUpdateStateListener(updateListener, channelConfiguration.offDelay, value,
90                     componentConfiguration.getScheduler());
91         }
92
93         return updateListener;
94     }
95 }