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 HandlerPowerLevelController} is responsible for the Alexa.PowerControllerInterface
40 * @author Lukas Knoeller - Initial contribution
41 * @author Michael Geramb - Initial contribution
44 public class HandlerPowerLevelController extends HandlerBase {
46 public static final String INTERFACE = "Alexa.PowerLevelController";
49 private static final ChannelTypeUID CHANNEL_TYPE_POWER_LEVEL = new ChannelTypeUID(
50 AmazonEchoControlBindingConstants.BINDING_ID, "powerLevel");
52 // Channel definitions
53 private static final ChannelInfo POWER_LEVEL = new ChannelInfo("powerLevel" /* propertyName */ ,
54 "powerLevel" /* ChannelId */, CHANNEL_TYPE_POWER_LEVEL /* Channel Type */ ,
55 ITEM_TYPE_DIMMER /* Item Type */);
57 private @Nullable Integer lastPowerLevel;
60 public String[] getSupportedInterface() {
61 return new String[] { INTERFACE };
65 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
66 if (POWER_LEVEL.propertyName.equals(property)) {
67 return new ChannelInfo[] { POWER_LEVEL };
73 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
74 Integer powerLevelValue = null;
75 for (JsonObject state : stateList) {
76 if (POWER_LEVEL.propertyName.equals(state.get("name").getAsString())) {
77 int value = state.get("value").getAsInt();
78 // For groups take the maximum
79 if (powerLevelValue == null) {
80 powerLevelValue = value;
81 } else if (value > powerLevelValue) {
82 powerLevelValue = value;
86 if (powerLevelValue != null) {
87 lastPowerLevel = powerLevelValue;
89 updateState(POWER_LEVEL.channelId,
90 powerLevelValue == null ? UnDefType.UNDEF : new PercentType(powerLevelValue));
94 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
95 SmartHomeCapability[] capabilties, String channelId, Command command)
96 throws IOException, InterruptedException {
97 if (channelId.equals(POWER_LEVEL.channelId)) {
98 if (containsCapabilityProperty(capabilties, POWER_LEVEL.propertyName)) {
99 if (command.equals(IncreaseDecreaseType.INCREASE)) {
100 Integer lastPowerLevel = this.lastPowerLevel;
101 if (lastPowerLevel != null) {
102 int newValue = lastPowerLevel++;
103 if (newValue > 100) {
106 this.lastPowerLevel = newValue;
107 connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, newValue);
110 } else if (command.equals(IncreaseDecreaseType.DECREASE)) {
111 Integer lastPowerLevel = this.lastPowerLevel;
112 if (lastPowerLevel != null) {
113 int newValue = lastPowerLevel--;
117 this.lastPowerLevel = newValue;
118 connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, newValue);
121 } else if (command.equals(OnOffType.OFF)) {
123 connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, 0);
125 } else if (command.equals(OnOffType.ON)) {
126 lastPowerLevel = 100;
127 connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, 100);
129 } else if (command instanceof PercentType) {
130 lastPowerLevel = ((PercentType) command).intValue();
131 connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName,
132 ((PercentType) command).floatValue() / 100);
141 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
142 @Nullable Locale locale) {