2 * Copyright (c) 2010-2024 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.ComponentChannelType;
23 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
24 import org.openhab.binding.mqtt.homeassistant.internal.listener.ExpireUpdateStateListener;
25 import org.openhab.binding.mqtt.homeassistant.internal.listener.OffDelayUpdateStateListener;
26 import org.openhab.core.thing.type.AutoUpdatePolicy;
28 import com.google.gson.annotations.SerializedName;
31 * A MQTT BinarySensor, following the https://www.home-assistant.io/components/binary_sensor.mqtt/ specification.
33 * @author David Graeff - Initial contribution
36 public class BinarySensor extends AbstractComponent<BinarySensor.ChannelConfiguration> {
37 public static final String SENSOR_CHANNEL_ID = "sensor"; // Randomly chosen channel "ID"
40 * Configuration class for MQTT component
42 static class ChannelConfiguration extends AbstractChannelConfiguration {
43 ChannelConfiguration() {
44 super("MQTT Binary Sensor");
47 @SerializedName("device_class")
48 protected @Nullable String deviceClass;
49 @SerializedName("force_update")
50 protected boolean forceUpdate = false;
51 @SerializedName("expire_after")
52 protected @Nullable Integer expireAfter;
53 @SerializedName("off_delay")
54 protected @Nullable Integer offDelay;
56 @SerializedName("state_topic")
57 protected String stateTopic = "";
58 @SerializedName("payload_on")
59 protected String payloadOn = "ON";
60 @SerializedName("payload_off")
61 protected String payloadOff = "OFF";
63 @SerializedName("json_attributes_topic")
64 protected @Nullable String jsonAttributesTopic;
65 @SerializedName("json_attributes_template")
66 protected @Nullable String jsonAttributesTemplate;
67 @SerializedName("json_attributes")
68 protected @Nullable List<String> jsonAttributes;
71 public BinarySensor(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
72 super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
74 OnOffValue value = new OnOffValue(channelConfiguration.payloadOn, channelConfiguration.payloadOff);
76 buildChannel(SENSOR_CHANNEL_ID, ComponentChannelType.SWITCH, value, getName(),
77 getListener(componentConfiguration, value))
78 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
79 .withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();
83 private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
85 ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
87 if (channelConfiguration.expireAfter != null) {
88 updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
89 componentConfiguration.getTracker(), componentConfiguration.getScheduler());
91 if (channelConfiguration.offDelay != null) {
92 updateListener = new OffDelayUpdateStateListener(updateListener, channelConfiguration.offDelay, value,
93 componentConfiguration.getScheduler());
96 return updateListener;