]> git.basschouten.com Git - openhab-addons.git/blob
7f57ab65ab6b81c955e964ab3040f169ed5d7577
[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_DIMMER;
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.IncreaseDecreaseType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
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;
34
35 import com.google.gson.JsonObject;
36
37 /**
38  * The {@link HandlerPercentageController} is responsible for the Alexa.PowerControllerInterface
39  *
40  * @author Lukas Knoeller - Initial contribution
41  * @author Michael Geramb - Initial contribution
42  */
43 @NonNullByDefault
44 public class HandlerPercentageController extends HandlerBase {
45     // Interface
46     public static final String INTERFACE = "Alexa.PercentageController";
47
48     // Channel types
49     private static final ChannelTypeUID CHANNEL_TYPE_PERCENTAGE = new ChannelTypeUID(
50             AmazonEchoControlBindingConstants.BINDING_ID, "percentage");
51
52     // Channel definitions
53     private static final ChannelInfo PERCENTAGE = new ChannelInfo("percentage" /* propertyName */ ,
54             "percentage" /* ChannelId */, CHANNEL_TYPE_PERCENTAGE /* Channel Type */ ,
55             ITEM_TYPE_DIMMER /* Item Type */);
56
57     private @Nullable Integer lastPercentage;
58
59     @Override
60     public String[] getSupportedInterface() {
61         return new String[] { INTERFACE };
62     }
63
64     @Override
65     protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
66         if (PERCENTAGE.propertyName.equals(property)) {
67             return new ChannelInfo[] { PERCENTAGE };
68         }
69         return null;
70     }
71
72     @Override
73     public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
74         Integer percentageValue = null;
75         for (JsonObject state : stateList) {
76             if (PERCENTAGE.propertyName.equals(state.get("name").getAsString())) {
77                 int value = state.get("value").getAsInt();
78                 // For groups take the maximum
79                 if (percentageValue == null) {
80                     percentageValue = value;
81                 } else if (value > percentageValue) {
82                     percentageValue = value;
83                 }
84             }
85         }
86         if (percentageValue != null) {
87             lastPercentage = percentageValue;
88         }
89         updateState(PERCENTAGE.channelId, percentageValue == null ? UnDefType.UNDEF : new PercentType(percentageValue));
90     }
91
92     @Override
93     public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
94             SmartHomeCapability[] capabilties, String channelId, Command command)
95             throws IOException, InterruptedException {
96         if (channelId.equals(PERCENTAGE.channelId)) {
97             if (containsCapabilityProperty(capabilties, PERCENTAGE.propertyName)) {
98                 if (command.equals(IncreaseDecreaseType.INCREASE)) {
99                     Integer lastPercentage = this.lastPercentage;
100                     if (lastPercentage != null) {
101                         int newValue = lastPercentage++;
102                         if (newValue > 100) {
103                             newValue = 100;
104                         }
105                         this.lastPercentage = newValue;
106                         connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, newValue);
107                         return true;
108                     }
109                 } else if (command.equals(IncreaseDecreaseType.DECREASE)) {
110                     Integer lastPercentage = this.lastPercentage;
111                     if (lastPercentage != null) {
112                         int newValue = lastPercentage--;
113                         if (newValue < 0) {
114                             newValue = 0;
115                         }
116                         this.lastPercentage = newValue;
117                         connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, newValue);
118                         return true;
119                     }
120                 } else if (command.equals(OnOffType.OFF)) {
121                     lastPercentage = 0;
122                     connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, 0);
123                     return true;
124                 } else if (command.equals(OnOffType.ON)) {
125                     lastPercentage = 100;
126                     connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, 100);
127                     return true;
128                 } else if (command instanceof PercentType) {
129                     lastPercentage = ((PercentType) command).intValue();
130                     connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, lastPercentage);
131                     return true;
132                 }
133             }
134         }
135         return false;
136     }
137
138     @Override
139     public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
140             @Nullable Locale locale) {
141         return null;
142     }
143 }