2 * Copyright (c) 2010-2021 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;
27 * A MQTT BinarySensor, following the https://www.home-assistant.io/components/binary_sensor.mqtt/ specification.
29 * @author David Graeff - Initial contribution
32 public class BinarySensor extends AbstractComponent<BinarySensor.ChannelConfiguration> {
33 public static final String sensorChannelID = "sensor"; // Randomly chosen channel "ID"
36 * Configuration class for MQTT component
38 static class ChannelConfiguration extends AbstractChannelConfiguration {
39 ChannelConfiguration() {
40 super("MQTT Binary Sensor");
43 protected @Nullable String device_class;
44 protected boolean force_update = false;
45 protected @Nullable Integer expire_after;
46 protected @Nullable Integer off_delay;
48 protected String state_topic = "";
49 protected String payload_on = "ON";
50 protected String payload_off = "OFF";
52 protected @Nullable String json_attributes_topic;
53 protected @Nullable String json_attributes_template;
54 protected @Nullable List<String> json_attributes;
57 public BinarySensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
58 super(componentConfiguration, ChannelConfiguration.class);
60 OnOffValue value = new OnOffValue(channelConfiguration.payload_on, channelConfiguration.payload_off);
62 buildChannel(sensorChannelID, value, "value", getListener(componentConfiguration, value))
63 .stateTopic(channelConfiguration.state_topic, channelConfiguration.getValueTemplate()).build();
66 private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
68 ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
70 if (channelConfiguration.expire_after != null) {
71 updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expire_after, value,
72 componentConfiguration.getTracker(), componentConfiguration.getScheduler());
74 if (channelConfiguration.off_delay != null) {
75 updateListener = new OffDelayUpdateStateListener(updateListener, channelConfiguration.off_delay, value,
76 componentConfiguration.getScheduler());
79 return updateListener;