]> git.basschouten.com Git - openhab-addons.git/blob
d5e98c04a66b193395357075b6f172b268e6160f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 import com.google.gson.annotations.SerializedName;
27
28 /**
29  * A MQTT BinarySensor, following the https://www.home-assistant.io/components/binary_sensor.mqtt/ specification.
30  *
31  * @author David Graeff - Initial contribution
32  */
33 @NonNullByDefault
34 public class BinarySensor extends AbstractComponent<BinarySensor.ChannelConfiguration> {
35     public static final String SENSOR_CHANNEL_ID = "sensor"; // Randomly chosen channel "ID"
36
37     /**
38      * Configuration class for MQTT component
39      */
40     static class ChannelConfiguration extends AbstractChannelConfiguration {
41         ChannelConfiguration() {
42             super("MQTT Binary Sensor");
43         }
44
45         @SerializedName("device_class")
46         protected @Nullable String deviceClass;
47         @SerializedName("force_update")
48         protected boolean forceUpdate = false;
49         @SerializedName("expire_after")
50         protected @Nullable Integer expireAfter;
51         @SerializedName("off_delay")
52         protected @Nullable Integer offDelay;
53
54         @SerializedName("state_topic")
55         protected String stateTopic = "";
56         @SerializedName("payload_on")
57         protected String payloadOn = "ON";
58         @SerializedName("payload_off")
59         protected String payloadOff = "OFF";
60
61         @SerializedName("json_attributes_topic")
62         protected @Nullable String jsonAttributesTopic;
63         @SerializedName("json_attributes_template")
64         protected @Nullable String jsonAttributesTemplate;
65         @SerializedName("json_attributes")
66         protected @Nullable List<String> jsonAttributes;
67     }
68
69     public BinarySensor(ComponentFactory.ComponentConfiguration componentConfiguration) {
70         super(componentConfiguration, ChannelConfiguration.class);
71
72         OnOffValue value = new OnOffValue(channelConfiguration.payloadOn, channelConfiguration.payloadOff);
73
74         buildChannel(SENSOR_CHANNEL_ID, value, "value", getListener(componentConfiguration, value))
75                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate()).build();
76     }
77
78     private ChannelStateUpdateListener getListener(ComponentFactory.ComponentConfiguration componentConfiguration,
79             Value value) {
80         ChannelStateUpdateListener updateListener = componentConfiguration.getUpdateListener();
81
82         if (channelConfiguration.expireAfter != null) {
83             updateListener = new ExpireUpdateStateListener(updateListener, channelConfiguration.expireAfter, value,
84                     componentConfiguration.getTracker(), componentConfiguration.getScheduler());
85         }
86         if (channelConfiguration.offDelay != null) {
87             updateListener = new OffDelayUpdateStateListener(updateListener, channelConfiguration.offDelay, value,
88                     componentConfiguration.getScheduler());
89         }
90
91         return updateListener;
92     }
93 }