]> git.basschouten.com Git - openhab-addons.git/blob
e42b9055b10e64a1a7a0f384d8d88f5d65932634
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.amazonechocontrol.internal.smarthome;
14
15 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.ITEM_TYPE_CONTACT;
16
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Locale;
20
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;
33
34 import com.google.gson.JsonObject;
35
36 /**
37  * The {@link HandlerAcousticEventSensor} is responsible for the Alexa.PowerControllerInterface
38  *
39  * @author Lukas Knoeller - Initial contribution
40  * @author Michael Geramb - Initial contribution
41  */
42 @NonNullByDefault
43 public class HandlerAcousticEventSensor extends HandlerBase {
44     // Interface
45     public static final String INTERFACE = "Alexa.AcousticEventSensor";
46
47     // Channel types
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");
52
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 */);
60
61     public HandlerAcousticEventSensor(SmartHomeDeviceHandler smartHomeDeviceHandler) {
62         super(smartHomeDeviceHandler);
63     }
64
65     private ChannelInfo[] getAlarmChannels() {
66         return new ChannelInfo[] { GLASS_BREAK_DETECTION_STATE, SMOKE_ALARM_DETECTION_STATE };
67     }
68
69     @Override
70     public String[] getSupportedInterface() {
71         return new String[] { INTERFACE };
72     }
73
74     @Override
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 };
79             }
80         }
81         return null;
82     }
83
84     @Override
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());
93                 }
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());
98                 }
99             }
100         }
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));
105     }
106
107     @Override
108     public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
109             List<SmartHomeCapability> capabilities, String channelId, Command command) throws IOException {
110         return false;
111     }
112
113     @Override
114     public @Nullable StateDescription findStateDescription(String channelUID, StateDescription originalStateDescription,
115             @Nullable Locale locale) {
116         return null;
117     }
118 }