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;
18 import java.math.RoundingMode;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.lutron.internal.handler.DimmerHandler;
23 import org.openhab.binding.lutron.internal.protocol.LutronDuration;
24 import org.openhab.core.automation.annotation.ActionInput;
25 import org.openhab.core.automation.annotation.RuleAction;
26 import org.openhab.core.thing.binding.ThingActions;
27 import org.openhab.core.thing.binding.ThingActionsScope;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The {@link DimmerActions} defines thing actions for DimmerHandler.
35 * @author Bob Adair - Initial contribution
37 @ThingActionsScope(name = "lutron")
39 public class DimmerActions implements ThingActions, IDimmerActions {
40 private final Logger logger = LoggerFactory.getLogger(DimmerActions.class);
42 private @Nullable DimmerHandler handler;
44 public DimmerActions() {
45 logger.trace("Lutron Dimmer actions service created");
49 public void setThingHandler(@Nullable ThingHandler handler) {
50 if (handler instanceof DimmerHandler) {
51 this.handler = (DimmerHandler) handler;
56 public @Nullable ThingHandler getThingHandler() {
61 * The setLevel dimmer thing action
64 @RuleAction(label = "setLevel", description = "Send set level command with fade and delay times")
66 @ActionInput(name = "level", label = "Dimmer Level", description = "New dimmer level (0-100)") @Nullable Double level,
67 @ActionInput(name = "fadeTime", label = "Fade Time", description = "Time to fade to new level (seconds)") @Nullable Double fadeTime,
68 @ActionInput(name = "delayTime", label = "Delay Time", description = "Delay before starting fade (seconds)") @Nullable Double delayTime) {
69 DimmerHandler dimmerHandler = handler;
70 if (dimmerHandler == null) {
71 logger.debug("Handler not set for Dimmer thing actions.");
75 logger.debug("Ignoring setLevel command due to null level value.");
78 if (fadeTime == null) {
79 logger.debug("Ignoring setLevel command due to null value for fadeTime.");
82 if (delayTime == null) {
83 logger.debug("Ignoring setLevel command due to null value for delayTime.");
87 Double lightLevel = level;
88 if (lightLevel > 100.0) {
90 } else if (lightLevel < 0.0) {
94 dimmerHandler.setLightLevel(new BigDecimal(lightLevel).setScale(2, RoundingMode.HALF_UP),
95 new LutronDuration(fadeTime), new LutronDuration(delayTime));
96 } catch (IllegalArgumentException e) {
97 logger.debug("Ignoring setLevel command due to illegal argument exception: {}", e.getMessage());
102 * Static setLevel method for Rules DSL backward compatibility
104 public static void setLevel(@Nullable ThingActions actions, @Nullable Double level, @Nullable Double fadeTime,
105 @Nullable Double delayTime) {
106 invokeMethodOf(actions).setLevel(level, fadeTime, delayTime); // Replace when core issue #1536 is fixed
110 * This is only necessary to work around a bug in openhab-core (issue #1536). It should be removed once that is
113 private static IDimmerActions invokeMethodOf(@Nullable ThingActions actions) {
114 if (actions == null) {
115 throw new IllegalArgumentException("actions cannot be null");
117 if (actions.getClass().getName().equals(DimmerActions.class.getName())) {
118 if (actions instanceof IDimmerActions) {
119 return (IDimmerActions) actions;
121 return (IDimmerActions) Proxy.newProxyInstance(IDimmerActions.class.getClassLoader(),
122 new Class[] { IDimmerActions.class }, (Object proxy, Method method, Object[] args) -> {
123 Method m = actions.getClass().getDeclaredMethod(method.getName(),
124 method.getParameterTypes());
125 return m.invoke(actions, args);
129 throw new IllegalArgumentException("Actions is not an instance of DimmerActions");