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