]> git.basschouten.com Git - openhab-addons.git/blob
45ba4b32000b91934086b977648711308c5010f1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.ServiceScope;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * TP-Link Smart Home Rule Actions.
33  *
34  * @author Hilbrand Bouwkamp - Initial contribution
35  */
36 @Component(scope = ServiceScope.PROTOTYPE, service = TPLinkSmartHomeActions.class)
37 @ThingActionsScope(name = "tplinksmarthome")
38 @NonNullByDefault
39 public class TPLinkSmartHomeActions implements ThingActions, ThingHandlerService {
40
41     private final Logger logger = LoggerFactory.getLogger(TPLinkSmartHomeActions.class);
42
43     private @Nullable SmartHomeHandler handler;
44
45     @RuleAction(label = "@text/actions.tplinksmarthome.send.label", description = "@text/actions.tplinksmarthome.send.description")
46     @ActionOutput(name = "response", label = "@text/actions.tplinksmarthome.send.response.label", description = "@text/actions.tplinksmarthome.send.response.description", type = "java.lang.String")
47     public String send(
48             @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)
49             throws IOException {
50         if (handler instanceof SmartHomeHandler) {
51             return handler.getConnection().sendCommand(command);
52         } else {
53             logger.warn("Could not send command to tplink device because handler not set.");
54             return "";
55         }
56     }
57
58     public static String send(final ThingActions actions, final String command) throws IOException {
59         return ((TPLinkSmartHomeActions) actions).send(command);
60     }
61
62     @Override
63     public void setThingHandler(final ThingHandler handler) {
64         if (handler instanceof SmartHomeHandler smartHomeHandler) {
65             this.handler = smartHomeHandler;
66         }
67     }
68
69     @Override
70     public @Nullable ThingHandler getThingHandler() {
71         return handler;
72     }
73 }