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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.mqtt.generic.values.OnOffValue;
20 * A MQTT BinarySensor, following the https://www.home-assistant.io/components/binary_sensor.mqtt/ specification.
22 * @author David Graeff - Initial contribution
25 public class ComponentBinarySensor extends AbstractComponent<ComponentBinarySensor.ChannelConfiguration> {
26 public static final String sensorChannelID = "sensor"; // Randomly chosen channel "ID"
29 * Configuration class for MQTT component
31 static class ChannelConfiguration extends BaseChannelConfiguration {
32 ChannelConfiguration() {
33 super("MQTT Binary Sensor");
36 protected @Nullable String device_class;
37 protected boolean force_update = false;
38 protected int expire_after = 0;
40 protected String state_topic = "";
41 protected String payload_on = "ON";
42 protected String payload_off = "OFF";
45 public ComponentBinarySensor(CFactory.ComponentConfiguration componentConfiguration) {
46 super(componentConfiguration, ChannelConfiguration.class);
48 if (channelConfiguration.force_update) {
49 throw new UnsupportedOperationException("Component:Sensor does not support forced updates");
52 buildChannel(sensorChannelID, new OnOffValue(channelConfiguration.payload_on, channelConfiguration.payload_off),
53 channelConfiguration.name, componentConfiguration.getUpdateListener())//
54 .stateTopic(channelConfiguration.state_topic, channelConfiguration.value_template)//