]> git.basschouten.com Git - openhab-addons.git/blob
8c94cb0f7337e5d54d4bce1bf13b3e6ca77e9619
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.hue.internal.action;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.hue.internal.handler.HueLightHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.RuleAction;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.thing.binding.ThingActions;
22 import org.openhab.core.thing.binding.ThingActionsScope;
23 import org.openhab.core.thing.binding.ThingHandler;
24 import org.openhab.core.types.Command;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The {@link LightActions} defines the thing actions for the hue binding.
30  *
31  * @author Jochen Leopold - Initial contribution
32  * @author Laurent Garnier - new method invokeMethodOf + interface ILightActions
33  */
34 @ThingActionsScope(name = "hue")
35 @NonNullByDefault
36 public class LightActions implements ThingActions {
37     private final Logger logger = LoggerFactory.getLogger(LightActions.class);
38     private @Nullable HueLightHandler handler;
39
40     @Override
41     public void setThingHandler(@Nullable ThingHandler handler) {
42         this.handler = (HueLightHandler) handler;
43     }
44
45     @Override
46     public @Nullable ThingHandler getThingHandler() {
47         return handler;
48     }
49
50     @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
51     public void fadingLightCommand(
52             @ActionInput(name = "channel", label = "@text/actionInputChannelLabel", description = "@text/actionInputChannelDesc") @Nullable String channel,
53             @ActionInput(name = "command", label = "@text/actionInputCommandLabel", description = "@text/actionInputCommandDesc") @Nullable Command command,
54             @ActionInput(name = "fadeTime", label = "@text/actionInputFadeTimeLabel", description = "@text/actionInputFadeTimeDesc") @Nullable DecimalType fadeTime) {
55         HueLightHandler lightHandler = handler;
56         if (lightHandler == null) {
57             logger.warn("Hue Action service ThingHandler is null!");
58             return;
59         }
60
61         if (channel == null) {
62             logger.debug("skipping Hue fadingLightCommand to channel '{}' due to null value.", channel);
63             return;
64         }
65
66         if (command == null) {
67             logger.debug("skipping Hue fadingLightCommand to command '{}' due to null value.", command);
68             return;
69         }
70         if (fadeTime == null) {
71             logger.debug("skipping Hue fadingLightCommand to fadeTime '{}' due to null value.", fadeTime);
72             return;
73         }
74
75         lightHandler.handleCommand(channel, command, fadeTime.longValue());
76         logger.debug("send LightAction to {} with {}ms of fadeTime", channel, fadeTime);
77     }
78
79     public static void fadingLightCommand(ThingActions actions, @Nullable String channel, @Nullable Command command,
80             @Nullable DecimalType fadeTime) {
81         ((LightActions) actions).fadingLightCommand(channel, command, fadeTime);
82     }
83 }