2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
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;
22 import com.google.gson.annotations.SerializedName;
25 * A MQTT Device Trigger, following the https://www.home-assistant.io/integrations/device_trigger.mqtt/ specification.
27 * @author Cody Cutrer - Initial contribution
30 public class DeviceTrigger extends AbstractComponent<DeviceTrigger.ChannelConfiguration> {
32 * Configuration class for MQTT component
34 static class ChannelConfiguration extends AbstractChannelConfiguration {
35 ChannelConfiguration() {
36 super("MQTT Device Trigger");
39 @SerializedName("automation_type")
40 protected String automationType = "trigger";
41 protected String topic = "";
42 protected String type = "";
43 protected String subtype = "";
45 protected @Nullable String payload;
48 public DeviceTrigger(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
49 super(componentConfiguration, ChannelConfiguration.class, newStyleChannels, true);
51 if (!"trigger".equals(channelConfiguration.automationType)) {
52 throw new ConfigurationException("Component:DeviceTrigger must have automation_type 'trigger'");
54 if (channelConfiguration.type.isBlank()) {
55 throw new ConfigurationException("Component:DeviceTrigger must have a type");
57 if (channelConfiguration.subtype.isBlank()) {
58 throw new ConfigurationException("Component:DeviceTrigger must have a subtype");
62 String payload = channelConfiguration.payload;
63 if (payload != null) {
64 value = new TextValue(new String[] { payload });
66 value = new TextValue();
69 buildChannel(channelConfiguration.type, ComponentChannelType.TRIGGER, value, getName(),
70 componentConfiguration.getUpdateListener())
71 .stateTopic(channelConfiguration.topic, channelConfiguration.getValueTemplate()).trigger(true).build();