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 HandlerPowerLevelController} is responsible for the Alexa.PowerControllerInterface
41 * @author Lukas Knoeller - Initial contribution
42 * @author Michael Geramb - Initial contribution
45 public class HandlerPowerLevelController extends HandlerBase {
47 public static final String INTERFACE = "Alexa.PowerLevelController";
50 private static final ChannelTypeUID CHANNEL_TYPE_POWER_LEVEL = new ChannelTypeUID(
51 AmazonEchoControlBindingConstants.BINDING_ID, "powerLevel");
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 */);
58 private @Nullable Integer lastPowerLevel;
60 public HandlerPowerLevelController(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 (POWER_LEVEL.propertyName.equals(property)) {
72 return new ChannelInfo[] { POWER_LEVEL };
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;
91 if (powerLevelValue != null) {
92 lastPowerLevel = powerLevelValue;
94 updateState(POWER_LEVEL.channelId,
95 powerLevelValue == null ? UnDefType.UNDEF : new PercentType(powerLevelValue));
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) {
111 this.lastPowerLevel = newValue;
112 connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, newValue);
115 } else if (command.equals(IncreaseDecreaseType.DECREASE)) {
116 Integer lastPowerLevel = this.lastPowerLevel;
117 if (lastPowerLevel != null) {
118 int newValue = lastPowerLevel--;
122 this.lastPowerLevel = newValue;
123 connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, newValue);
126 } else if (command.equals(OnOffType.OFF)) {
128 connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, 0);
130 } else if (command.equals(OnOffType.ON)) {
131 lastPowerLevel = 100;
132 connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName, 100);
134 } else if (command instanceof PercentType) {
135 lastPowerLevel = ((PercentType) command).intValue();
136 connection.smartHomeCommand(entityId, "setPowerLevel", POWER_LEVEL.propertyName,
137 ((PercentType) command).floatValue() / 100);
146 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
147 @Nullable Locale locale) {