2 * Copyright (c) 2010-2020 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.lutron.action;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.lutron.internal.handler.DimmerHandler;
22 import org.openhab.binding.lutron.internal.protocol.LutronDuration;
23 import org.openhab.core.automation.annotation.ActionInput;
24 import org.openhab.core.automation.annotation.RuleAction;
25 import org.openhab.core.thing.binding.ThingActions;
26 import org.openhab.core.thing.binding.ThingActionsScope;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The {@link DimmerActions} defines thing actions for DimmerHandler.
34 * @author Bob Adair - Initial contribution
36 @ThingActionsScope(name = "lutron")
38 public class DimmerActions implements ThingActions, IDimmerActions {
39 private final Logger logger = LoggerFactory.getLogger(DimmerActions.class);
41 private @Nullable DimmerHandler handler;
43 public DimmerActions() {
44 logger.trace("Lutron Dimmer actions service created");
48 public void setThingHandler(@Nullable ThingHandler handler) {
49 if (handler instanceof DimmerHandler) {
50 this.handler = (DimmerHandler) handler;
55 public @Nullable ThingHandler getThingHandler() {
60 * The setLevel dimmer thing action
63 @RuleAction(label = "setLevel", description = "Send set level command with fade and delay times")
65 @ActionInput(name = "level", label = "Dimmer Level", description = "New dimmer level (0-100)") @Nullable Double level,
66 @ActionInput(name = "fadeTime", label = "Fade Time", description = "Time to fade to new level (seconds)") @Nullable Double fadeTime,
67 @ActionInput(name = "delayTime", label = "Delay Time", description = "Delay before starting fade (seconds)") @Nullable Double delayTime) {
68 DimmerHandler dimmerHandler = handler;
69 if (dimmerHandler == null) {
70 logger.debug("Handler not set for Dimmer thing actions.");
74 logger.debug("Ignoring setLevel command due to null level value.");
77 if (fadeTime == null) {
78 logger.debug("Ignoring setLevel command due to null value for fadeTime.");
81 if (delayTime == null) {
82 logger.debug("Ignoring setLevel command due to null value for delayTime.");
86 Double lightLevel = level;
87 if (lightLevel > 100.0) {
89 } else if (lightLevel < 0.0) {
93 dimmerHandler.setLightLevel(new BigDecimal(lightLevel).setScale(2, BigDecimal.ROUND_HALF_UP),
94 new LutronDuration(fadeTime), new LutronDuration(delayTime));
95 } catch (IllegalArgumentException e) {
96 logger.debug("Ignoring setLevel command due to illegal argument exception: {}", e.getMessage());
101 * Static setLevel method for Rules DSL backward compatibility
103 public static void setLevel(@Nullable ThingActions actions, @Nullable Double level, @Nullable Double fadeTime,
104 @Nullable Double delayTime) {
105 invokeMethodOf(actions).setLevel(level, fadeTime, delayTime); // Replace when core issue #1536 is fixed
109 * This is only necessary to work around a bug in openhab-core (issue #1536). It should be removed once that is
112 private static IDimmerActions invokeMethodOf(@Nullable ThingActions actions) {
113 if (actions == null) {
114 throw new IllegalArgumentException("actions cannot be null");
116 if (actions.getClass().getName().equals(DimmerActions.class.getName())) {
117 if (actions instanceof IDimmerActions) {
118 return (IDimmerActions) actions;
120 return (IDimmerActions) Proxy.newProxyInstance(IDimmerActions.class.getClassLoader(),
121 new Class[] { IDimmerActions.class }, (Object proxy, Method method, Object[] args) -> {
122 Method m = actions.getClass().getDeclaredMethod(method.getName(),
123 method.getParameterTypes());
124 return m.invoke(actions, args);
128 throw new IllegalArgumentException("Actions is not an instance of DimmerActions");