]> git.basschouten.com Git - openhab-addons.git/blob
313a14078c925238ed70f6d0a39e8b882d0339d0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.homeassistant.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.mqtt.generic.values.OnOffValue;
18
19 /**
20  * A MQTT BinarySensor, following the https://www.home-assistant.io/components/binary_sensor.mqtt/ specification.
21  *
22  * @author David Graeff - Initial contribution
23  */
24 @NonNullByDefault
25 public class ComponentBinarySensor extends AbstractComponent<ComponentBinarySensor.ChannelConfiguration> {
26     public static final String sensorChannelID = "sensor"; // Randomly chosen channel "ID"
27
28     /**
29      * Configuration class for MQTT component
30      */
31     static class ChannelConfiguration extends BaseChannelConfiguration {
32         ChannelConfiguration() {
33             super("MQTT Binary Sensor");
34         }
35
36         protected @Nullable String device_class;
37         protected boolean force_update = false;
38         protected int expire_after = 0;
39
40         protected String state_topic = "";
41         protected String payload_on = "ON";
42         protected String payload_off = "OFF";
43     }
44
45     public ComponentBinarySensor(CFactory.ComponentConfiguration componentConfiguration) {
46         super(componentConfiguration, ChannelConfiguration.class);
47
48         if (channelConfiguration.force_update) {
49             throw new UnsupportedOperationException("Component:Sensor does not support forced updates");
50         }
51
52         buildChannel(sensorChannelID, new OnOffValue(channelConfiguration.payload_on, channelConfiguration.payload_off),
53                 channelConfiguration.name, componentConfiguration.getUpdateListener())//
54                         .stateTopic(channelConfiguration.state_topic, channelConfiguration.value_template)//
55                         .build();
56     }
57 }