2 * Copyright (c) 2010-2024 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.internal.action;
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.lutron.internal.handler.DimmerHandler;
21 import org.openhab.binding.lutron.internal.protocol.LutronDuration;
22 import org.openhab.core.automation.annotation.ActionInput;
23 import org.openhab.core.automation.annotation.RuleAction;
24 import org.openhab.core.thing.binding.ThingActions;
25 import org.openhab.core.thing.binding.ThingActionsScope;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link DimmerActions} defines thing actions for DimmerHandler.
33 * @author Bob Adair - Initial contribution
35 @ThingActionsScope(name = "lutron")
37 public class DimmerActions implements ThingActions {
38 private final Logger logger = LoggerFactory.getLogger(DimmerActions.class);
40 private @Nullable DimmerHandler handler;
42 public DimmerActions() {
43 logger.trace("Lutron Dimmer actions service created");
47 public void setThingHandler(@Nullable ThingHandler handler) {
48 if (handler instanceof DimmerHandler dimmerHandler) {
49 this.handler = dimmerHandler;
54 public @Nullable ThingHandler getThingHandler() {
59 * The setLevel dimmer thing action
61 @RuleAction(label = "send a set level command", description = "Send set level command with fade and delay times.")
63 @ActionInput(name = "level", label = "Dimmer Level", description = "New dimmer level (0-100)") @Nullable Double level,
64 @ActionInput(name = "fadeTime", label = "Fade Time", description = "Time to fade to new level (seconds)") @Nullable Double fadeTime,
65 @ActionInput(name = "delayTime", label = "Delay Time", description = "Delay before starting fade (seconds)") @Nullable Double delayTime) {
66 DimmerHandler dimmerHandler = handler;
67 if (dimmerHandler == null) {
68 logger.debug("Handler not set for Dimmer thing actions.");
72 logger.debug("Ignoring setLevel command due to null level value.");
75 if (fadeTime == null) {
76 logger.debug("Ignoring setLevel command due to null value for fadeTime.");
79 if (delayTime == null) {
80 logger.debug("Ignoring setLevel command due to null value for delayTime.");
84 Double lightLevel = level;
85 if (lightLevel > 100.0) {
87 } else if (lightLevel < 0.0) {
91 dimmerHandler.setLightLevel(new BigDecimal(lightLevel).setScale(2, RoundingMode.HALF_UP),
92 new LutronDuration(fadeTime), new LutronDuration(delayTime));
93 } catch (IllegalArgumentException e) {
94 logger.debug("Ignoring setLevel command due to illegal argument exception: {}", e.getMessage());
99 * Static setLevel method for Rules DSL backward compatibility
101 public static void setLevel(ThingActions actions, @Nullable Double level, @Nullable Double fadeTime,
102 @Nullable Double delayTime) {
103 ((DimmerActions) actions).setLevel(level, fadeTime, delayTime);