]> git.basschouten.com Git - openhab-addons.git/blob
de70b1acf28ea9096a2e1af75700ef5c06826319
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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;
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
19 /**
20  * A MQTT alarm control panel, following the https://www.home-assistant.io/components/alarm_control_panel.mqtt/
21  * specification.
22  *
23  * The implemented provides three state-less switches (For disarming, arming@home, arming@away) and one alarm state
24  * text.
25  *
26  * @author David Graeff - Initial contribution
27  */
28 @NonNullByDefault
29 public class ComponentAlarmControlPanel extends AbstractComponent<ComponentAlarmControlPanel.ChannelConfiguration> {
30     public static final String stateChannelID = "alarm"; // Randomly chosen channel "ID"
31     public static final String switchDisarmChannelID = "disarm"; // Randomly chosen channel "ID"
32     public static final String switchArmHomeChannelID = "armhome"; // Randomly chosen channel "ID"
33     public static final String switchArmAwayChannelID = "armaway"; // Randomly chosen channel "ID"
34
35     /**
36      * Configuration class for MQTT component
37      */
38     static class ChannelConfiguration extends BaseChannelConfiguration {
39         ChannelConfiguration() {
40             super("MQTT Alarm");
41         }
42
43         protected @Nullable String code;
44
45         protected String state_topic = "";
46         protected String state_disarmed = "disarmed";
47         protected String state_armed_home = "armed_home";
48         protected String state_armed_away = "armed_away";
49         protected String state_pending = "pending";
50         protected String state_triggered = "triggered";
51
52         protected @Nullable String command_topic;
53         protected String payload_disarm = "DISARM";
54         protected String payload_arm_home = "ARM_HOME";
55         protected String payload_arm_away = "ARM_AWAY";
56     }
57
58     public ComponentAlarmControlPanel(CFactory.ComponentConfiguration componentConfiguration) {
59         super(componentConfiguration, ChannelConfiguration.class);
60
61         final String[] state_enum = { channelConfiguration.state_disarmed, channelConfiguration.state_armed_home,
62                 channelConfiguration.state_armed_away, channelConfiguration.state_pending,
63                 channelConfiguration.state_triggered };
64         buildChannel(stateChannelID, new TextValue(state_enum), channelConfiguration.name,
65                 componentConfiguration.getUpdateListener())
66                         .stateTopic(channelConfiguration.state_topic, channelConfiguration.value_template)//
67                         .build();
68
69         String command_topic = channelConfiguration.command_topic;
70         if (command_topic != null) {
71             buildChannel(switchDisarmChannelID, new TextValue(new String[] { channelConfiguration.payload_disarm }),
72                     channelConfiguration.name, componentConfiguration.getUpdateListener())
73                             .commandTopic(command_topic, channelConfiguration.retain).build();
74
75             buildChannel(switchArmHomeChannelID, new TextValue(new String[] { channelConfiguration.payload_arm_home }),
76                     channelConfiguration.name, componentConfiguration.getUpdateListener())
77                             .commandTopic(command_topic, channelConfiguration.retain).build();
78
79             buildChannel(switchArmAwayChannelID, new TextValue(new String[] { channelConfiguration.payload_arm_away }),
80                     channelConfiguration.name, componentConfiguration.getUpdateListener())
81                             .commandTopic(command_topic, channelConfiguration.retain).build();
82         }
83     }
84 }