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;
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Locale;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants;
24 import org.openhab.binding.amazonechocontrol.internal.Connection;
25 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
26 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
27 import org.openhab.core.library.types.OpenClosedType;
28 import org.openhab.core.thing.type.ChannelTypeUID;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.StateDescription;
31 import org.openhab.core.types.UnDefType;
33 import com.google.gson.JsonObject;
36 * The {@link HandlerAcousticEventSensor} is responsible for the Alexa.PowerControllerInterface
38 * @author Lukas Knoeller - Initial contribution
39 * @author Michael Geramb - Initial contribution
42 public class HandlerAcousticEventSensor extends HandlerBase {
44 public static final String INTERFACE = "Alexa.AcousticEventSensor";
47 private static final ChannelTypeUID CHANNEL_TYPE_GLASS_BREAK_DETECTION_STATE = new ChannelTypeUID(
48 AmazonEchoControlBindingConstants.BINDING_ID, "glassBreakDetectionState");
49 private static final ChannelTypeUID CHANNEL_TYPE_SMOKE_ALARM_DETECTION_STATE = new ChannelTypeUID(
50 AmazonEchoControlBindingConstants.BINDING_ID, "smokeAlarmDetectionState");
52 // Channel definitions
53 private static final ChannelInfo GLASS_BREAK_DETECTION_STATE = new ChannelInfo(
54 "glassBreakDetectionState" /* propertyName */ , "glassBreakDetectionState" /* ChannelId */,
55 CHANNEL_TYPE_GLASS_BREAK_DETECTION_STATE /* Channel Type */ , ITEM_TYPE_CONTACT /* Item Type */);
56 private static final ChannelInfo SMOKE_ALARM_DETECTION_STATE = new ChannelInfo(
57 "smokeAlarmDetectionState" /* propertyName */ , "smokeAlarmDetectionState" /* ChannelId */,
58 CHANNEL_TYPE_SMOKE_ALARM_DETECTION_STATE /* Channel Type */ , ITEM_TYPE_CONTACT /* Item Type */);
60 private ChannelInfo[] getAlarmChannels() {
61 return new ChannelInfo[] { GLASS_BREAK_DETECTION_STATE, SMOKE_ALARM_DETECTION_STATE };
65 public String[] getSupportedInterface() {
66 return new String[] { INTERFACE };
70 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
71 for (ChannelInfo channelInfo : getAlarmChannels()) {
72 if (channelInfo.propertyName.equals(property)) {
73 return new ChannelInfo[] { channelInfo };
80 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
81 Boolean glassBreakDetectionStateValue = null;
82 Boolean smokeAlarmDetectionStateValue = null;
83 for (JsonObject state : stateList) {
84 if (GLASS_BREAK_DETECTION_STATE.propertyName.equals(state.get("name").getAsString())) {
85 if (glassBreakDetectionStateValue == null) {
86 glassBreakDetectionStateValue = !"NOT_DETECTED"
87 .equals(state.get("value").getAsJsonObject().get("value").getAsString());
89 } else if (SMOKE_ALARM_DETECTION_STATE.propertyName.equals(state.get("name").getAsString())) {
90 if (smokeAlarmDetectionStateValue == null) {
91 smokeAlarmDetectionStateValue = !"NOT_DETECTED"
92 .equals(state.get("value").getAsJsonObject().get("value").getAsString());
96 updateState(GLASS_BREAK_DETECTION_STATE.channelId, glassBreakDetectionStateValue == null ? UnDefType.UNDEF
97 : (glassBreakDetectionStateValue ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
98 updateState(SMOKE_ALARM_DETECTION_STATE.channelId, smokeAlarmDetectionStateValue == null ? UnDefType.UNDEF
99 : (smokeAlarmDetectionStateValue ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
103 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
104 SmartHomeCapability[] capabilties, String channelId, Command command) throws IOException {
109 public @Nullable StateDescription findStateDescription(String channelUID, StateDescription originalStateDescription,
110 @Nullable Locale locale) {