2 * Copyright (c) 2010-2022 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.handler.SmartHomeDeviceHandler;
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.thing.type.ChannelTypeUID;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.StateDescription;
32 import org.openhab.core.types.UnDefType;
34 import com.google.gson.JsonObject;
37 * The {@link HandlerAcousticEventSensor} is responsible for the Alexa.PowerControllerInterface
39 * @author Lukas Knoeller - Initial contribution
40 * @author Michael Geramb - Initial contribution
43 public class HandlerAcousticEventSensor extends HandlerBase {
45 public static final String INTERFACE = "Alexa.AcousticEventSensor";
48 private static final ChannelTypeUID CHANNEL_TYPE_GLASS_BREAK_DETECTION_STATE = new ChannelTypeUID(
49 AmazonEchoControlBindingConstants.BINDING_ID, "glassBreakDetectionState");
50 private static final ChannelTypeUID CHANNEL_TYPE_SMOKE_ALARM_DETECTION_STATE = new ChannelTypeUID(
51 AmazonEchoControlBindingConstants.BINDING_ID, "smokeAlarmDetectionState");
53 // Channel definitions
54 private static final ChannelInfo GLASS_BREAK_DETECTION_STATE = new ChannelInfo(
55 "glassBreakDetectionState" /* propertyName */ , "glassBreakDetectionState" /* ChannelId */,
56 CHANNEL_TYPE_GLASS_BREAK_DETECTION_STATE /* Channel Type */ , ITEM_TYPE_CONTACT /* Item Type */);
57 private static final ChannelInfo SMOKE_ALARM_DETECTION_STATE = new ChannelInfo(
58 "smokeAlarmDetectionState" /* propertyName */ , "smokeAlarmDetectionState" /* ChannelId */,
59 CHANNEL_TYPE_SMOKE_ALARM_DETECTION_STATE /* Channel Type */ , ITEM_TYPE_CONTACT /* Item Type */);
61 public HandlerAcousticEventSensor(SmartHomeDeviceHandler smartHomeDeviceHandler) {
62 super(smartHomeDeviceHandler);
65 private ChannelInfo[] getAlarmChannels() {
66 return new ChannelInfo[] { GLASS_BREAK_DETECTION_STATE, SMOKE_ALARM_DETECTION_STATE };
70 public String[] getSupportedInterface() {
71 return new String[] { INTERFACE };
75 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
76 for (ChannelInfo channelInfo : getAlarmChannels()) {
77 if (channelInfo.propertyName.equals(property)) {
78 return new ChannelInfo[] { channelInfo };
85 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
86 Boolean glassBreakDetectionStateValue = null;
87 Boolean smokeAlarmDetectionStateValue = null;
88 for (JsonObject state : stateList) {
89 if (GLASS_BREAK_DETECTION_STATE.propertyName.equals(state.get("name").getAsString())) {
90 if (glassBreakDetectionStateValue == null) {
91 glassBreakDetectionStateValue = !"NOT_DETECTED"
92 .equals(state.get("value").getAsJsonObject().get("value").getAsString());
94 } else if (SMOKE_ALARM_DETECTION_STATE.propertyName.equals(state.get("name").getAsString())) {
95 if (smokeAlarmDetectionStateValue == null) {
96 smokeAlarmDetectionStateValue = !"NOT_DETECTED"
97 .equals(state.get("value").getAsJsonObject().get("value").getAsString());
101 updateState(GLASS_BREAK_DETECTION_STATE.channelId, glassBreakDetectionStateValue == null ? UnDefType.UNDEF
102 : (glassBreakDetectionStateValue ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
103 updateState(SMOKE_ALARM_DETECTION_STATE.channelId, smokeAlarmDetectionStateValue == null ? UnDefType.UNDEF
104 : (smokeAlarmDetectionStateValue ? OpenClosedType.CLOSED : OpenClosedType.OPEN));
108 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
109 List<SmartHomeCapability> capabilities, String channelId, Command command) throws IOException {
114 public @Nullable StateDescription findStateDescription(String channelUID, StateDescription originalStateDescription,
115 @Nullable Locale locale) {