2 * Copyright (c) 2010-2022 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 HandlerBrightnessController} is responsible for the Alexa.PowerControllerInterface
41 * @author Lukas Knoeller - Initial contribution
42 * @author Michael Geramb - Initial contribution
45 public class HandlerBrightnessController extends HandlerBase {
47 public static final String INTERFACE = "Alexa.BrightnessController";
50 private static final ChannelTypeUID CHANNEL_TYPE_BRIGHTNESS = new ChannelTypeUID(
51 AmazonEchoControlBindingConstants.BINDING_ID, "brightness");
53 // Channel definitions
54 private static final ChannelInfo BRIGHTNESS = new ChannelInfo("brightness" /* propertyName */ ,
55 "brightness" /* ChannelId */, CHANNEL_TYPE_BRIGHTNESS /* Channel Type */ ,
56 ITEM_TYPE_DIMMER /* Item Type */);
58 private @Nullable Integer lastBrightness;
60 public HandlerBrightnessController(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 (BRIGHTNESS.propertyName.equals(property)) {
72 return new ChannelInfo[] { BRIGHTNESS };
78 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
79 Integer brightnessValue = null;
80 for (JsonObject state : stateList) {
81 if (BRIGHTNESS.propertyName.equals(state.get("name").getAsString())) {
82 int value = state.get("value").getAsInt();
83 // For groups take the maximum
84 if (brightnessValue == null) {
85 brightnessValue = value;
86 } else if (value > brightnessValue) {
87 brightnessValue = value;
91 if (brightnessValue != null) {
92 lastBrightness = brightnessValue;
94 updateState(BRIGHTNESS.channelId, brightnessValue == null ? UnDefType.UNDEF : new PercentType(brightnessValue));
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(BRIGHTNESS.channelId)) {
102 if (containsCapabilityProperty(capabilities, BRIGHTNESS.propertyName)) {
103 if (command.equals(IncreaseDecreaseType.INCREASE)) {
104 Integer lastBrightness = this.lastBrightness;
105 if (lastBrightness != null) {
106 int newValue = lastBrightness++;
107 if (newValue > 100) {
110 this.lastBrightness = newValue;
111 connection.smartHomeCommand(entityId, "setBrightness", BRIGHTNESS.propertyName, newValue);
114 } else if (command.equals(IncreaseDecreaseType.DECREASE)) {
115 Integer lastBrightness = this.lastBrightness;
116 if (lastBrightness != null) {
117 int newValue = lastBrightness--;
121 this.lastBrightness = newValue;
122 connection.smartHomeCommand(entityId, "setBrightness", BRIGHTNESS.propertyName, newValue);
125 } else if (command.equals(OnOffType.OFF)) {
127 connection.smartHomeCommand(entityId, "setBrightness", BRIGHTNESS.propertyName, 0);
129 } else if (command.equals(OnOffType.ON)) {
130 lastBrightness = 100;
131 connection.smartHomeCommand(entityId, "setBrightness", BRIGHTNESS.propertyName, 100);
133 } else if (command instanceof PercentType) {
134 lastBrightness = ((PercentType) command).intValue();
135 connection.smartHomeCommand(entityId, "setBrightness", BRIGHTNESS.propertyName,
136 ((PercentType) command).floatValue() / 100);
145 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
146 @Nullable Locale locale) {