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;
26 import com.google.gson.annotations.SerializedName;
29 * A MQTT BinarySensor, following the https://www.home-assistant.io/components/binary_sensor.mqtt/ specification.
31 * @author David Graeff - Initial contribution
34 public class BinarySensor extends AbstractComponent<BinarySensor.ChannelConfiguration> {
35 public static final String SENSOR_CHANNEL_ID = "sensor"; // Randomly chosen channel "ID"
38 * Configuration class for MQTT component
40 static class ChannelConfiguration extends AbstractChannelConfiguration {
41 ChannelConfiguration() {
42 super("MQTT Binary Sensor");
45 @SerializedName("device_class")
46 protected @Nullable String deviceClass;
47 @SerializedName("force_update")
48 protected boolean forceUpdate = false;
49 @SerializedName("expire_after")
50 protected @Nullable Integer expireAfter;
51 @SerializedName("off_delay")
52 protected @Nullable Integer offDelay;
54 @SerializedName("state_topic")
55 protected String stateTopic = "";
56 @SerializedName("payload_on")
57 protected String payloadOn = "ON";
58 @SerializedName("payload_off")
59 protected String payloadOff = "OFF";
61 @SerializedName("json_attributes_topic")
62 protected @Nullable String jsonAttributesTopic;
63 @SerializedName("json_attributes_template")
64 protected @Nullable String jsonAttributesTemplate;
65 @SerializedName("json_attributes")
66 protected @Nullable List<String> jsonAttributes;
69 public BinarySensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
70 super(componentConfiguration, ChannelConfiguration.class);
72 OnOffValue value = new OnOffValue(channelConfiguration.payloadOn, channelConfiguration.payloadOff);
74 buildChannel(SENSOR_CHANNEL_ID, value, "value", getListener(componentConfiguration, value))
75 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate()).build();
78 private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
80 ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
82 if (channelConfiguration.expireAfter != null) {
83 updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
84 componentConfiguration.getTracker(), componentConfiguration.getScheduler());
86 if (channelConfiguration.offDelay != null) {
87 updateListener = new OffDelayUpdateStateListener(updateListener, channelConfiguration.offDelay, value,
88 componentConfiguration.getScheduler());
91 return updateListener;