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_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.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.OnOffType;
29 import org.openhab.core.thing.type.ChannelTypeUID;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.StateDescription;
32 import org.openhab.core.types.UnDefType;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.gson.JsonObject;
39 * The {@link HandlerPowerController} is responsible for the Alexa.PowerControllerInterface
41 * @author Lukas Knoeller - Initial contribution
42 * @author Michael Geramb - Initial contribution
45 public class HandlerPowerController extends HandlerBase {
46 private final Logger logger = LoggerFactory.getLogger(HandlerPowerController.class);
49 public static final String INTERFACE = "Alexa.PowerController";
52 private static final ChannelTypeUID CHANNEL_TYPE_POWER_STATE = new ChannelTypeUID(
53 AmazonEchoControlBindingConstants.BINDING_ID, "powerState");
55 // Channel definitions
56 private static final ChannelInfo POWER_STATE = new ChannelInfo("powerState" /* propertyName */ ,
57 "powerState" /* ChannelId */, CHANNEL_TYPE_POWER_STATE /* Channel Type */ ,
58 ITEM_TYPE_SWITCH /* Item Type */);
60 public HandlerPowerController(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_STATE.propertyName.equals(property)) {
72 return new ChannelInfo[] { POWER_STATE };
78 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
79 logger.trace("{} received {}", this.smartHomeDeviceHandler.getId(), stateList);
80 Boolean powerStateValue = null;
81 for (JsonObject state : stateList) {
82 if (POWER_STATE.propertyName.equals(state.get("name").getAsString())) {
83 String value = state.get("value").getAsString();
84 // For groups take true if all true
85 powerStateValue = "ON".equals(value);
88 logger.trace("{} final state {}", this.smartHomeDeviceHandler.getId(), powerStateValue);
89 updateState(POWER_STATE.channelId, powerStateValue == null ? UnDefType.UNDEF : OnOffType.from(powerStateValue));
93 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
94 List<SmartHomeCapability> capabilities, String channelId, Command command)
95 throws IOException, InterruptedException {
96 if (channelId.equals(POWER_STATE.channelId)) {
97 if (containsCapabilityProperty(capabilities, POWER_STATE.propertyName)) {
98 if (command.equals(OnOffType.ON)) {
99 connection.smartHomeCommand(entityId, "turnOn");
101 } else if (command.equals(OnOffType.OFF)) {
102 connection.smartHomeCommand(entityId, "turnOff");
111 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
112 @Nullable Locale locale) {