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