2 * Copyright (c) 2010-2020 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;
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.listener.ExpireUpdateStateListener;
23 import org.openhab.binding.mqtt.homeassistant.internal.listener.OffDelayUpdateStateListener;
26 * A MQTT BinarySensor, following the https://www.home-assistant.io/components/binary_sensor.mqtt/ specification.
28 * @author David Graeff - Initial contribution
31 public class ComponentBinarySensor extends AbstractComponent<ComponentBinarySensor.ChannelConfiguration> {
32 public static final String sensorChannelID = "sensor"; // Randomly chosen channel "ID"
35 * Configuration class for MQTT component
37 static class ChannelConfiguration extends BaseChannelConfiguration {
38 ChannelConfiguration() {
39 super("MQTT Binary Sensor");
42 protected @Nullable String device_class;
43 protected boolean force_update = false;
44 protected @Nullable Integer expire_after;
45 protected @Nullable Integer off_delay;
47 protected String state_topic = "";
48 protected String payload_on = "ON";
49 protected String payload_off = "OFF";
51 protected @Nullable String json_attributes_topic;
52 protected @Nullable String json_attributes_template;
53 protected @Nullable List<String> json_attributes;
56 public ComponentBinarySensor(CFactory.ComponentConfiguration componentConfiguration) {
57 super(componentConfiguration, ChannelConfiguration.class);
59 OnOffValue value = new OnOffValue(channelConfiguration.payload_on, channelConfiguration.payload_off);
61 buildChannel(sensorChannelID, value, "value", getListener(componentConfiguration, value))
62 .stateTopic(channelConfiguration.state_topic, channelConfiguration.value_template).build();
65 private ChannelStateUpdateListener getListener(CFactory.ComponentConfiguration componentConfiguration,
67 ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
69 if (channelConfiguration.expire_after != null) {
70 updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expire_after, value,
71 componentConfiguration.getTracker(), componentConfiguration.getScheduler());
73 if (channelConfiguration.off_delay != null) {
74 updateListener = new OffDelayUpdateStateListener(updateListener, channelConfiguration.off_delay, value,
75 componentConfiguration.getScheduler());
78 return updateListener;