2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
15 import java.util.List;
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;
27 import com.google.gson.annotations.SerializedName;
30 * A MQTT BinarySensor, following the https://www.home-assistant.io/components/binary_sensor.mqtt/ specification.
32 * @author David Graeff - Initial contribution
35 public class BinarySensor extends AbstractComponent<BinarySensor.ChannelConfiguration> {
36 public static final String SENSOR_CHANNEL_ID = "sensor"; // Randomly chosen channel "ID"
39 * Configuration class for MQTT component
41 static class ChannelConfiguration extends AbstractChannelConfiguration {
42 ChannelConfiguration() {
43 super("MQTT Binary Sensor");
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;
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";
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;
70 public BinarySensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
71 super(componentConfiguration, ChannelConfiguration.class);
73 OnOffValue value = new OnOffValue(channelConfiguration.payloadOn, channelConfiguration.payloadOff);
75 buildChannel(SENSOR_CHANNEL_ID, value, "value", getListener(componentConfiguration, value))
76 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
77 .withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();
80 private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
82 ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
84 if (channelConfiguration.expireAfter != null) {
85 updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
86 componentConfiguration.getTracker(), componentConfiguration.getScheduler());
88 if (channelConfiguration.offDelay != null) {
89 updateListener = new OffDelayUpdateStateListener(updateListener, channelConfiguration.offDelay, value,
90 componentConfiguration.getScheduler());
93 return updateListener;