2 * Copyright (c) 2010-2020 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.amazonechocontrol.internal.smarthome;
15 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.ITEM_TYPE_CONTACT;
16 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.ITEM_TYPE_STRING;
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.Locale;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants;
25 import org.openhab.binding.amazonechocontrol.internal.Connection;
26 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
27 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
28 import org.openhab.core.library.types.OpenClosedType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.thing.type.ChannelTypeUID;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.StateDescription;
33 import org.openhab.core.types.UnDefType;
35 import com.google.gson.JsonObject;
38 * The {@link HandlerSecurityPanelController} is responsible for the Alexa.PowerControllerInterface
40 * @author Lukas Knoeller - Initial contribution
41 * @author Michael Geramb - Initial contribution
44 public class HandlerSecurityPanelController extends HandlerBase {
46 public static final String INTERFACE = "Alexa.SecurityPanelController";
49 private static final ChannelTypeUID CHANNEL_TYPE_ARM_STATE = new ChannelTypeUID(
50 AmazonEchoControlBindingConstants.BINDING_ID, "armState");
52 private static final ChannelTypeUID CHANNEL_TYPE_BURGLARY_ALARM = new ChannelTypeUID(
53 AmazonEchoControlBindingConstants.BINDING_ID, "burglaryAlarm");
55 private static final ChannelTypeUID CHANNEL_TYPE_CARBON_MONOXIDE_ALARM = new ChannelTypeUID(
56 AmazonEchoControlBindingConstants.BINDING_ID, "carbonMonoxideAlarm");
58 private static final ChannelTypeUID CHANNEL_TYPE_FIRE_ALARM = new ChannelTypeUID(
59 AmazonEchoControlBindingConstants.BINDING_ID, "fireAlarm");
61 private static final ChannelTypeUID CHANNEL_TYPE_WATER_ALARM = new ChannelTypeUID(
62 AmazonEchoControlBindingConstants.BINDING_ID, "waterAlarm");
64 // Channel definitions
65 private static final ChannelInfo ARM_STATE = new ChannelInfo("armState" /* propertyName */ ,
66 "armState" /* ChannelId */, CHANNEL_TYPE_ARM_STATE /* Channel Type */ , ITEM_TYPE_STRING /* Item Type */);
68 private static final ChannelInfo BURGLARY_ALARM = new ChannelInfo("burglaryAlarm" /* propertyName */ ,
69 "burglaryAlarm" /* ChannelId */, CHANNEL_TYPE_BURGLARY_ALARM /* Channel Type */ ,
70 ITEM_TYPE_CONTACT /* Item Type */);
72 private static final ChannelInfo CARBON_MONOXIDE_ALARM = new ChannelInfo("carbonMonoxideAlarm" /* propertyName */ ,
73 "carbonMonoxideAlarm" /* ChannelId */, CHANNEL_TYPE_CARBON_MONOXIDE_ALARM /* Channel Type */ ,
74 ITEM_TYPE_CONTACT /* Item Type */);
76 private static final ChannelInfo FIRE_ALARM = new ChannelInfo("fireAlarm" /* propertyName */ ,
77 "fireAlarm" /* ChannelId */, CHANNEL_TYPE_FIRE_ALARM /* Channel Type */ ,
78 ITEM_TYPE_CONTACT /* Item Type */);
80 private static final ChannelInfo WATER_ALARM = new ChannelInfo("waterAlarm" /* propertyName */ ,
81 "waterAlarm" /* ChannelId */, CHANNEL_TYPE_WATER_ALARM /* Channel Type */ ,
82 ITEM_TYPE_CONTACT /* Item Type */);
84 private ChannelInfo[] getAlarmChannels() {
85 return new ChannelInfo[] { BURGLARY_ALARM, CARBON_MONOXIDE_ALARM, FIRE_ALARM, WATER_ALARM };
89 public String[] getSupportedInterface() {
90 return new String[] { INTERFACE };
94 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
95 if (ARM_STATE.propertyName.equals(property)) {
96 return new ChannelInfo[] { ARM_STATE };
98 for (ChannelInfo channelInfo : getAlarmChannels()) {
99 if (channelInfo.propertyName.equals(property)) {
100 return new ChannelInfo[] { channelInfo };
107 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
108 String armStateValue = null;
109 Boolean burglaryAlarmValue = null;
110 Boolean carbonMonoxideAlarmValue = null;
111 Boolean fireAlarmValue = null;
112 Boolean waterAlarmValue = null;
113 for (JsonObject state : stateList) {
114 if (ARM_STATE.propertyName.equals(state.get("name").getAsString())) {
115 if (armStateValue == null) {
116 armStateValue = state.get("value").getAsString();
118 } else if (BURGLARY_ALARM.propertyName.equals(state.get("name").getAsString())) {
119 if (burglaryAlarmValue == null) {
120 burglaryAlarmValue = "ALARM".equals(state.get("value").getAsString());
122 } else if (CARBON_MONOXIDE_ALARM.propertyName.equals(state.get("name").getAsString())) {
123 if (carbonMonoxideAlarmValue == null) {
124 carbonMonoxideAlarmValue = "ALARM".equals(state.get("value").getAsString());
126 } else if (FIRE_ALARM.propertyName.equals(state.get("name").getAsString())) {
127 if (fireAlarmValue == null) {
128 fireAlarmValue = "ALARM".equals(state.get("value").getAsString());
130 } else if (WATER_ALARM.propertyName.equals(state.get("name").getAsString())) {
131 if (waterAlarmValue == null) {
132 waterAlarmValue = "ALARM".equals(state.get("value").getAsString());
136 updateState(ARM_STATE.channelId, armStateValue == null ? UnDefType.UNDEF : new StringType(armStateValue));
137 updateState(BURGLARY_ALARM.channelId, burglaryAlarmValue == null ? UnDefType.UNDEF
138 : (burglaryAlarmValue ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
139 updateState(CARBON_MONOXIDE_ALARM.channelId, carbonMonoxideAlarmValue == null ? UnDefType.UNDEF
140 : (carbonMonoxideAlarmValue ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
141 updateState(FIRE_ALARM.channelId, fireAlarmValue == null ? UnDefType.UNDEF
142 : (fireAlarmValue ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
143 updateState(WATER_ALARM.channelId, waterAlarmValue == null ? UnDefType.UNDEF
144 : (waterAlarmValue ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
148 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
149 SmartHomeCapability[] capabilities, String channelId, Command command)
150 throws IOException, InterruptedException {
151 if (channelId.equals(ARM_STATE.channelId)) {
152 if (containsCapabilityProperty(capabilities, ARM_STATE.propertyName)) {
153 if (command instanceof StringType) {
154 String armStateValue = command.toFullString();
155 if (!armStateValue.isEmpty()) {
156 connection.smartHomeCommand(entityId, "controlSecurityPanel", ARM_STATE.propertyName,
167 public @Nullable StateDescription findStateDescription(String channelUID, StateDescription originalStateDescription,
168 @Nullable Locale locale) {