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