2 * Copyright (c) 2010-2023 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.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;
36 import com.google.gson.JsonObject;
39 * The {@link HandlerPercentageController} is responsible for the Alexa.PowerControllerInterface
41 * @author Lukas Knoeller - Initial contribution
42 * @author Michael Geramb - Initial contribution
45 public class HandlerPercentageController extends HandlerBase {
47 public static final String INTERFACE = "Alexa.PercentageController";
50 private static final ChannelTypeUID CHANNEL_TYPE_PERCENTAGE = new ChannelTypeUID(
51 AmazonEchoControlBindingConstants.BINDING_ID, "percentage");
53 // Channel definitions
54 private static final ChannelInfo PERCENTAGE = new ChannelInfo("percentage" /* propertyName */ ,
55 "percentage" /* ChannelId */, CHANNEL_TYPE_PERCENTAGE /* Channel Type */ ,
56 ITEM_TYPE_DIMMER /* Item Type */);
58 private @Nullable Integer lastPercentage;
60 public HandlerPercentageController(SmartHomeDeviceHandler smartHomeDeviceHandler) {
61 super(smartHomeDeviceHandler);
65 public String[] getSupportedInterface() {
66 return new String[] { INTERFACE };
70 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
71 if (PERCENTAGE.propertyName.equals(property)) {
72 return new ChannelInfo[] { PERCENTAGE };
78 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
79 Integer percentageValue = null;
80 for (JsonObject state : stateList) {
81 if (PERCENTAGE.propertyName.equals(state.get("name").getAsString())) {
82 int value = state.get("value").getAsInt();
83 // For groups take the maximum
84 if (percentageValue == null) {
85 percentageValue = value;
86 } else if (value > percentageValue) {
87 percentageValue = value;
91 if (percentageValue != null) {
92 lastPercentage = percentageValue;
94 updateState(PERCENTAGE.channelId, percentageValue == null ? UnDefType.UNDEF : new PercentType(percentageValue));
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(PERCENTAGE.channelId)) {
102 if (containsCapabilityProperty(capabilities, PERCENTAGE.propertyName)) {
103 if (command.equals(IncreaseDecreaseType.INCREASE)) {
104 Integer lastPercentage = this.lastPercentage;
105 if (lastPercentage != null) {
106 int newValue = lastPercentage++;
107 if (newValue > 100) {
110 this.lastPercentage = newValue;
111 connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, newValue);
114 } else if (command.equals(IncreaseDecreaseType.DECREASE)) {
115 Integer lastPercentage = this.lastPercentage;
116 if (lastPercentage != null) {
117 int newValue = lastPercentage--;
121 this.lastPercentage = newValue;
122 connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, newValue);
125 } else if (command.equals(OnOffType.OFF)) {
127 connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, 0);
129 } else if (command.equals(OnOffType.ON)) {
130 lastPercentage = 100;
131 connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, 100);
133 } else if (command instanceof PercentType) {
134 lastPercentage = ((PercentType) command).intValue();
135 connection.smartHomeCommand(entityId, "setPercentage", PERCENTAGE.propertyName, lastPercentage);
144 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
145 @Nullable Locale locale) {