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