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_SWITCH;
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.OnOffType;
28 import org.openhab.core.thing.type.ChannelTypeUID;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.StateDescription;
31 import org.openhab.core.types.UnDefType;
33 import com.google.gson.JsonObject;
36 * The {@link HandlerPowerController} is responsible for the Alexa.PowerControllerInterface
38 * @author Lukas Knoeller - Initial contribution
39 * @author Michael Geramb - Initial contribution
42 public class HandlerPowerController extends HandlerBase {
44 public static final String INTERFACE = "Alexa.PowerController";
47 private static final ChannelTypeUID CHANNEL_TYPE_POWER_STATE = new ChannelTypeUID(
48 AmazonEchoControlBindingConstants.BINDING_ID, "powerState");
50 // Channel definitions
51 private static final ChannelInfo POWER_STATE = new ChannelInfo("powerState" /* propertyName */ ,
52 "powerState" /* ChannelId */, CHANNEL_TYPE_POWER_STATE /* Channel Type */ ,
53 ITEM_TYPE_SWITCH /* Item Type */);
56 public String[] getSupportedInterface() {
57 return new String[] { INTERFACE };
61 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
62 if (POWER_STATE.propertyName.equals(property)) {
63 return new ChannelInfo[] { POWER_STATE };
69 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
70 Boolean powerStateValue = null;
71 for (JsonObject state : stateList) {
72 if (POWER_STATE.propertyName.equals(state.get("name").getAsString())) {
73 String value = state.get("value").getAsString();
74 // For groups take true if all true
75 if ("ON".equals(value)) {
76 powerStateValue = true;
77 } else if (powerStateValue == null) {
78 powerStateValue = false;
83 updateState(POWER_STATE.channelId,
84 powerStateValue == null ? UnDefType.UNDEF : (powerStateValue ? OnOffType.ON : OnOffType.OFF));
88 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
89 SmartHomeCapability[] capabilities, String channelId, Command command) throws IOException {
90 if (channelId.equals(POWER_STATE.channelId)) {
91 if (containsCapabilityProperty(capabilities, POWER_STATE.propertyName)) {
92 if (command.equals(OnOffType.ON)) {
93 connection.smartHomeCommand(entityId, "turnOn");
95 } else if (command.equals(OnOffType.OFF)) {
96 connection.smartHomeCommand(entityId, "turnOff");
105 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
106 @Nullable Locale locale) {