]> git.basschouten.com Git - openhab-addons.git/blob
2fb7be06efd1ce9ba0077ed3f4e85cb6d2dbae29
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 HandlerPowerLevelController} 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 HandlerPowerLevelController extends HandlerBase {
45     // Interface
46     public static final String INTERFACE = "Alexa.PowerLevelController";
47
48     // Channel types
49     private static final ChannelTypeUID CHANNEL_TYPE_POWER_LEVEL = new ChannelTypeUID(
50             AmazonEchoControlBindingConstants.BINDING_ID, "powerLevel");
51
52     // Channel definitions
53     private static final ChannelInfo POWER_LEVEL = new ChannelInfo("powerLevel" /* propertyName */ ,
54             "powerLevel" /* ChannelId */, CHANNEL_TYPE_POWER_LEVEL /* Channel Type */ ,
55             ITEM_TYPE_DIMMER /* Item Type */);
56
57     private @Nullable Integer lastPowerLevel;
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 (POWER_LEVEL.propertyName.equals(property)) {
67             return new ChannelInfo[] { POWER_LEVEL };
68         }
69         return null;
70     }
71
72     @Override
73     public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
74         Integer powerLevelValue = null;
75         for (JsonObject state : stateList) {
76             if (POWER_LEVEL.propertyName.equals(state.get("name").getAsString())) {
77                 int value = state.get("value").getAsInt();
78                 // For groups take the maximum
79                 if (powerLevelValue == null) {
80                     powerLevelValue = value;
81                 } else if (value > powerLevelValue) {
82                     powerLevelValue = value;
83                 }
84             }
85         }
86         if (powerLevelValue != null) {
87             lastPowerLevel = powerLevelValue;
88         }
89         updateState(POWER_LEVEL.channelId,
90                 powerLevelValue == null ? UnDefType.UNDEF : new PercentType(powerLevelValue));
91     }
92
93     @Override
94     public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
95             SmartHomeCapability[] capabilties, String channelId, Command command)
96             throws IOException, InterruptedException {
97         if (channelId.equals(POWER_LEVEL.channelId)) {
98             if (containsCapabilityProperty(capabilties, POWER_LEVEL.propertyName)) {
99                 if (command.equals(IncreaseDecreaseType.INCREASE)) {
100                     Integer lastPowerLevel = this.lastPowerLevel;
101                     if (lastPowerLevel != null) {
102                         int newValue = lastPowerLevel++;
103                         if (newValue > 100) {
104                             newValue = 100;
105                         }
106                         this.lastPowerLevel = newValue;
107                         connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, newValue);
108                         return true;
109                     }
110                 } else if (command.equals(IncreaseDecreaseType.DECREASE)) {
111                     Integer lastPowerLevel = this.lastPowerLevel;
112                     if (lastPowerLevel != null) {
113                         int newValue = lastPowerLevel--;
114                         if (newValue < 0) {
115                             newValue = 0;
116                         }
117                         this.lastPowerLevel = newValue;
118                         connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, newValue);
119                         return true;
120                     }
121                 } else if (command.equals(OnOffType.OFF)) {
122                     lastPowerLevel = 0;
123                     connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, 0);
124                     return true;
125                 } else if (command.equals(OnOffType.ON)) {
126                     lastPowerLevel = 100;
127                     connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, 100);
128                     return true;
129                 } else if (command instanceof PercentType) {
130                     lastPowerLevel = ((PercentType) command).intValue();
131                     connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName,
132                             ((PercentType) command).floatValue() / 100);
133                     return true;
134                 }
135             }
136         }
137         return false;
138     }
139
140     @Override
141     public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
142             @Nullable Locale locale) {
143         return null;
144     }
145 }