]> git.basschouten.com Git - openhab-addons.git/blob
62936cfc3cf18ae44f9688debcba03c3912dbffc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 java.util.List;
16
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;
24
25 /**
26  * A MQTT BinarySensor, following the https://www.home-assistant.io/components/binary_sensor.mqtt/ specification.
27  *
28  * @author David Graeff - Initial contribution
29  */
30 @NonNullByDefault
31 public class ComponentBinarySensor extends AbstractComponent<ComponentBinarySensor.ChannelConfiguration> {
32     public static final String sensorChannelID = "sensor"; // Randomly chosen channel "ID"
33
34     /**
35      * Configuration class for MQTT component
36      */
37     static class ChannelConfiguration extends BaseChannelConfiguration {
38         ChannelConfiguration() {
39             super("MQTT Binary Sensor");
40         }
41
42         protected @Nullable String device_class;
43         protected boolean force_update = false;
44         protected @Nullable Integer expire_after;
45         protected @Nullable Integer off_delay;
46
47         protected String state_topic = "";
48         protected String payload_on = "ON";
49         protected String payload_off = "OFF";
50
51         protected @Nullable String json_attributes_topic;
52         protected @Nullable String json_attributes_template;
53         protected @Nullable List<String> json_attributes;
54     }
55
56     public ComponentBinarySensor(CFactory.ComponentConfiguration componentConfiguration) {
57         super(componentConfiguration, ChannelConfiguration.class);
58
59         OnOffValue value = new OnOffValue(channelConfiguration.payload_on, channelConfiguration.payload_off);
60
61         buildChannel(sensorChannelID, value, "value", getListener(componentConfiguration, value))
62                 .stateTopic(channelConfiguration.state_topic, channelConfiguration.value_template).build();
63     }
64
65     private ChannelStateUpdateListener getListener(CFactory.ComponentConfiguration componentConfiguration,
66             Value value) {
67         ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
68
69         if (channelConfiguration.expire_after != null) {
70             updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expire_after, value,
71                     componentConfiguration.getTracker(), componentConfiguration.getScheduler());
72         }
73         if (channelConfiguration.off_delay != null) {
74             updateListener = new OffDelayUpdateStateListener(updateListener, channelConfiguration.off_delay, value,
75                     componentConfiguration.getScheduler());
76         }
77
78         return updateListener;
79     }
80 }