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