]> git.basschouten.com Git - openhab-addons.git/blob
0ff48e027381c686cf22a5f4ab9c6a77575871da
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.mqtt.generic.values.TextValue;
18 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
19 import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
20
21 import com.google.gson.annotations.SerializedName;
22
23 /**
24  * A MQTT Device Trigger, following the https://www.home-assistant.io/integrations/device_trigger.mqtt/ specification.
25  *
26  * @author Cody Cutrer - Initial contribution
27  */
28 @NonNullByDefault
29 public class DeviceTrigger extends AbstractComponent<DeviceTrigger.ChannelConfiguration> {
30     /**
31      * Configuration class for MQTT component
32      */
33     static class ChannelConfiguration extends AbstractChannelConfiguration {
34         ChannelConfiguration() {
35             super("MQTT Device Trigger");
36         }
37
38         @SerializedName("automation_type")
39         protected String automationType = "trigger";
40         protected String topic = "";
41         protected String type = "";
42         protected String subtype = "";
43
44         protected @Nullable String payload;
45     }
46
47     public DeviceTrigger(ComponentFactory.ComponentConfiguration componentConfiguration) {
48         super(componentConfiguration, ChannelConfiguration.class);
49
50         if (!"trigger".equals(channelConfiguration.automationType)) {
51             throw new ConfigurationException("Component:DeviceTrigger must have automation_type 'trigger'");
52         }
53         if (channelConfiguration.type.isBlank()) {
54             throw new ConfigurationException("Component:DeviceTrigger must have a type");
55         }
56         if (channelConfiguration.subtype.isBlank()) {
57             throw new ConfigurationException("Component:DeviceTrigger must have a subtype");
58         }
59
60         TextValue value;
61         String payload = channelConfiguration.payload;
62         if (payload != null) {
63             value = new TextValue(new String[] { payload });
64         } else {
65             value = new TextValue();
66         }
67
68         buildChannel(channelConfiguration.type, value, getName(), componentConfiguration.getUpdateListener())
69                 .stateTopic(channelConfiguration.topic, channelConfiguration.getValueTemplate()).trigger(true).build();
70     }
71 }