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