2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.amazonechocontrol.internal.smarthome;
15 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.ITEM_TYPE_DIMMER;
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Locale;
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;
35 import com.google.gson.JsonObject;
38 * The {@link HandlerPercentageController} is responsible for the Alexa.PowerControllerInterface
40 * @author Lukas Knoeller - Initial contribution
41 * @author Michael Geramb - Initial contribution
44 public class HandlerPercentageController extends HandlerBase {
46 public static final String INTERFACE = "Alexa.PercentageController";
49 private static final ChannelTypeUID CHANNEL_TYPE_PERCENTAGE = new ChannelTypeUID(
50 AmazonEchoControlBindingConstants.BINDING_ID, "percentage");
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 */);
57 private @Nullable Integer lastPercentage;
60 public String[] getSupportedInterface() {
61 return new String[] { INTERFACE };
65 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
66 if (PERCENTAGE.propertyName.equals(property)) {
67 return new ChannelInfo[] { PERCENTAGE };
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;
86 if (percentageValue != null) {
87 lastPercentage = percentageValue;
89 updateState(PERCENTAGE.channelId, percentageValue == null ? UnDefType.UNDEF : new PercentType(percentageValue));
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) {
105 this.lastPercentage = newValue;
106 connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, newValue);
109 } else if (command.equals(IncreaseDecreaseType.DECREASE)) {
110 Integer lastPercentage = this.lastPercentage;
111 if (lastPercentage != null) {
112 int newValue = lastPercentage--;
116 this.lastPercentage = newValue;
117 connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, newValue);
120 } else if (command.equals(OnOffType.OFF)) {
122 connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, 0);
124 } else if (command.equals(OnOffType.ON)) {
125 lastPercentage = 100;
126 connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, 100);
128 } else if (command instanceof PercentType) {
129 lastPercentage = ((PercentType) command).intValue();
130 connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, lastPercentage);
139 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
140 @Nullable Locale locale) {