]> git.basschouten.com Git - openhab-addons.git/blob
105fd1bdca9bb99ff0296a109aa040c16608552a
[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.tplinksmarthome.internal.handler;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.automation.annotation.ActionInput;
20 import org.openhab.core.automation.annotation.ActionOutput;
21 import org.openhab.core.automation.annotation.RuleAction;
22 import org.openhab.core.thing.binding.ThingActions;
23 import org.openhab.core.thing.binding.ThingActionsScope;
24 import org.openhab.core.thing.binding.ThingHandler;
25 import org.openhab.core.thing.binding.ThingHandlerService;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * TP-Link Smart Home Rule Actions.
31  *
32  * @author Hilbrand Bouwkamp - Initial contribution
33  */
34 @ThingActionsScope(name = "tplinksmarthome")
35 @NonNullByDefault
36 public class TPLinkSmartHomeActions implements ThingActions, ThingHandlerService {
37
38     private final Logger logger = LoggerFactory.getLogger(TPLinkSmartHomeActions.class);
39
40     private @Nullable SmartHomeHandler handler;
41
42     @RuleAction(label = "@text/actions.tplinksmarthome.send.label", description = "@text/actions.tplinksmarthome.send.description")
43     @ActionOutput(name = "response", label = "@text/actions.tplinksmarthome.send.response.label", description = "@text/actions.tplinksmarthome.send.response.description", type = "java.lang.String")
44     public String send(
45             @ActionInput(name = "command", label = "@text/actions.tplinksmarthome.send.command.label", description = "@text/actions.tplinksmarthome.send.command.description", type = "java.lang.String", required = true) final String command)
46             throws IOException {
47         if (handler instanceof SmartHomeHandler) {
48             return handler.getConnection().sendCommand(command);
49         } else {
50             logger.warn("Could not send command to tplink device because handler not set.");
51             return "";
52         }
53     }
54
55     public static String send(final ThingActions actions, final String command) throws IOException {
56         return ((TPLinkSmartHomeActions) actions).send(command);
57     }
58
59     @Override
60     public void setThingHandler(final ThingHandler handler) {
61         if (handler instanceof SmartHomeHandler smartHomeHandler) {
62             this.handler = smartHomeHandler;
63         }
64     }
65
66     @Override
67     public @Nullable ThingHandler getThingHandler() {
68         return handler;
69     }
70 }