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