]> git.basschouten.com Git - openhab-addons.git/blob
37969151c46787942f954c01232ec50b05bdbd94
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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_SWITCH;
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.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
26 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
27 import org.openhab.core.library.types.OnOffType;
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;
32
33 import com.google.gson.JsonObject;
34
35 /**
36  * The {@link HandlerPowerController} is responsible for the Alexa.PowerControllerInterface
37  *
38  * @author Lukas Knoeller - Initial contribution
39  * @author Michael Geramb - Initial contribution
40  */
41 @NonNullByDefault
42 public class HandlerPowerController extends HandlerBase {
43     // Interface
44     public static final String INTERFACE = "Alexa.PowerController";
45
46     // Channel types
47     private static final ChannelTypeUID CHANNEL_TYPE_POWER_STATE = new ChannelTypeUID(
48             AmazonEchoControlBindingConstants.BINDING_ID, "powerState");
49
50     // Channel definitions
51     private static final ChannelInfo POWER_STATE = new ChannelInfo("powerState" /* propertyName */ ,
52             "powerState" /* ChannelId */, CHANNEL_TYPE_POWER_STATE /* Channel Type */ ,
53             ITEM_TYPE_SWITCH /* Item Type */);
54
55     @Override
56     public String[] getSupportedInterface() {
57         return new String[] { INTERFACE };
58     }
59
60     @Override
61     protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
62         if (POWER_STATE.propertyName.equals(property)) {
63             return new ChannelInfo[] { POWER_STATE };
64         }
65         return null;
66     }
67
68     @Override
69     public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
70         Boolean powerStateValue = null;
71         for (JsonObject state : stateList) {
72             if (POWER_STATE.propertyName.equals(state.get("name").getAsString())) {
73                 String value = state.get("value").getAsString();
74                 // For groups take true if all true
75                 if ("ON".equals(value)) {
76                     powerStateValue = true;
77                 } else if (powerStateValue == null) {
78                     powerStateValue = false;
79                 }
80
81             }
82         }
83         updateState(POWER_STATE.channelId,
84                 powerStateValue == null ? UnDefType.UNDEF : (powerStateValue ? OnOffType.ON : OnOffType.OFF));
85     }
86
87     @Override
88     public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
89             SmartHomeCapability[] capabilities, String channelId, Command command) throws IOException {
90         if (channelId.equals(POWER_STATE.channelId)) {
91             if (containsCapabilityProperty(capabilities, POWER_STATE.propertyName)) {
92                 if (command.equals(OnOffType.ON)) {
93                     connection.smartHomeCommand(entityId, "turnOn");
94                     return true;
95                 } else if (command.equals(OnOffType.OFF)) {
96                     connection.smartHomeCommand(entityId, "turnOff");
97                     return true;
98                 }
99             }
100         }
101         return false;
102     }
103
104     @Override
105     public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
106             @Nullable Locale locale) {
107         return null;
108     }
109 }