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