]> git.basschouten.com Git - openhab-addons.git/blob
026ce5048556cb871f984a109b7191b481b17a3f
[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
21 import com.google.gson.annotations.SerializedName;
22
23 /**
24  * A MQTT alarm control panel, following the https://www.home-assistant.io/components/alarm_control_panel.mqtt/
25  * specification.
26  *
27  * The implemented provides three state-less switches (For disarming, arming@home, arming@away) and one alarm state
28  * text.
29  *
30  * @author David Graeff - Initial contribution
31  */
32 @NonNullByDefault
33 public class AlarmControlPanel extends AbstractComponent<AlarmControlPanel.ChannelConfiguration> {
34     public static final String STATE_CHANNEL_ID = "alarm"; // Randomly chosen channel "ID"
35     public static final String SWITCH_DISARM_CHANNEL_ID = "disarm"; // Randomly chosen channel "ID"
36     public static final String SWITCH_ARM_HOME_CHANNEL_ID = "armhome"; // Randomly chosen channel "ID"
37     public static final String SWITCH_ARM_AWAY_CHANNEL_ID = "armaway"; // Randomly chosen channel "ID"
38
39     /**
40      * Configuration class for MQTT component
41      */
42     static class ChannelConfiguration extends AbstractChannelConfiguration {
43         ChannelConfiguration() {
44             super("MQTT Alarm");
45         }
46
47         protected @Nullable String code;
48
49         @SerializedName("state_topic")
50         protected String stateTopic = "";
51         @SerializedName("state_disarmed")
52         protected String stateDisarmed = "disarmed";
53         @SerializedName("state_armed_home")
54         protected String stateArmedHome = "armed_home";
55         @SerializedName("state_armed_away")
56         protected String stateArmedAway = "armed_away";
57         @SerializedName("state_pending")
58         protected String statePending = "pending";
59         @SerializedName("state_triggered")
60         protected String stateTriggered = "triggered";
61
62         @SerializedName("command_topic")
63         protected @Nullable String commandTopic;
64         @SerializedName("payload_disarm")
65         protected String payloadDisarm = "DISARM";
66         @SerializedName("payload_arm_home")
67         protected String payloadArmHome = "ARM_HOME";
68         @SerializedName("payload_arm_away")
69         protected String payloadArmAway = "ARM_AWAY";
70     }
71
72     public AlarmControlPanel(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
73         super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
74
75         final String[] stateEnum = { channelConfiguration.stateDisarmed, channelConfiguration.stateArmedHome,
76                 channelConfiguration.stateArmedAway, channelConfiguration.statePending,
77                 channelConfiguration.stateTriggered };
78         buildChannel(STATE_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(stateEnum), getName(),
79                 componentConfiguration.getUpdateListener())
80                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())//
81                 .build();
82
83         String commandTopic = channelConfiguration.commandTopic;
84         if (commandTopic != null) {
85             buildChannel(SWITCH_DISARM_CHANNEL_ID, ComponentChannelType.STRING,
86                     new TextValue(new String[] { channelConfiguration.payloadDisarm }), getName(),
87                     componentConfiguration.getUpdateListener())
88                     .commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos()).build();
89
90             buildChannel(SWITCH_ARM_HOME_CHANNEL_ID, ComponentChannelType.STRING,
91                     new TextValue(new String[] { channelConfiguration.payloadArmHome }), getName(),
92                     componentConfiguration.getUpdateListener())
93                     .commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos()).build();
94
95             buildChannel(SWITCH_ARM_AWAY_CHANNEL_ID, ComponentChannelType.STRING,
96                     new TextValue(new String[] { channelConfiguration.payloadArmAway }), getName(),
97                     componentConfiguration.getUpdateListener())
98                     .commandTopic(commandTopic, channelConfiguration.isRetain(), channelConfiguration.getQos()).build();
99         }
100     }
101 }