]> git.basschouten.com Git - openhab-addons.git/blob
bc3dfe144f89ec3dd574b6d6cb7da09dfc0bf2d7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.amazonechocontrol.internal.smarthome;
14
15 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.ITEM_TYPE_SWITCH;
16
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Locale;
20
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;
35
36 import com.google.gson.JsonObject;
37
38 /**
39  * The {@link HandlerPowerController} is responsible for the Alexa.PowerControllerInterface
40  *
41  * @author Lukas Knoeller - Initial contribution
42  * @author Michael Geramb - Initial contribution
43  */
44 @NonNullByDefault
45 public class HandlerPowerController extends HandlerBase {
46     private final Logger logger = LoggerFactory.getLogger(HandlerPowerController.class);
47
48     // Interface
49     public static final String INTERFACE = "Alexa.PowerController";
50
51     // Channel types
52     private static final ChannelTypeUID CHANNEL_TYPE_POWER_STATE = new ChannelTypeUID(
53             AmazonEchoControlBindingConstants.BINDING_ID, "powerState");
54
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 */);
59
60     public HandlerPowerController(SmartHomeDeviceHandler smartHomeDeviceHandler) {
61         super(smartHomeDeviceHandler);
62     }
63
64     @Override
65     public String[] getSupportedInterface() {
66         return new String[] { INTERFACE };
67     }
68
69     @Override
70     protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
71         if (POWER_STATE.propertyName.equals(property)) {
72             return new ChannelInfo[] { POWER_STATE };
73         }
74         return null;
75     }
76
77     @Override
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);
86             }
87         }
88         logger.trace("{} final state {}", this.smartHomeDeviceHandler.getId(), powerStateValue);
89         updateState(POWER_STATE.channelId, powerStateValue == null ? UnDefType.UNDEF : OnOffType.from(powerStateValue));
90     }
91
92     @Override
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");
100                     return true;
101                 } else if (command.equals(OnOffType.OFF)) {
102                     connection.smartHomeCommand(entityId, "turnOff");
103                     return true;
104                 }
105             }
106         }
107         return false;
108     }
109
110     @Override
111     public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
112             @Nullable Locale locale) {
113         return null;
114     }
115 }