]> git.basschouten.com Git - openhab-addons.git/blob
df183554b98e310a9478326d5796d284bdd25474
[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.lutron.internal.action;
14
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17
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;
29
30 /**
31  * The {@link DimmerActions} defines thing actions for DimmerHandler.
32  *
33  * @author Bob Adair - Initial contribution
34  */
35 @ThingActionsScope(name = "lutron")
36 @NonNullByDefault
37 public class DimmerActions implements ThingActions {
38     private final Logger logger = LoggerFactory.getLogger(DimmerActions.class);
39
40     private @Nullable DimmerHandler handler;
41
42     public DimmerActions() {
43         logger.trace("Lutron Dimmer actions service created");
44     }
45
46     @Override
47     public void setThingHandler(@Nullable ThingHandler handler) {
48         if (handler instanceof DimmerHandler dimmerHandler) {
49             this.handler = dimmerHandler;
50         }
51     }
52
53     @Override
54     public @Nullable ThingHandler getThingHandler() {
55         return handler;
56     }
57
58     /**
59      * The setLevel dimmer thing action
60      */
61     @RuleAction(label = "send a set level command", description = "Send set level command with fade and delay times.")
62     public void setLevel(
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.");
69             return;
70         }
71         if (level == null) {
72             logger.debug("Ignoring setLevel command due to null level value.");
73             return;
74         }
75         if (fadeTime == null) {
76             logger.debug("Ignoring setLevel command due to null value for fadeTime.");
77             return;
78         }
79         if (delayTime == null) {
80             logger.debug("Ignoring setLevel command due to null value for delayTime.");
81             return;
82         }
83
84         Double lightLevel = level;
85         if (lightLevel > 100.0) {
86             lightLevel = 100.0;
87         } else if (lightLevel < 0.0) {
88             lightLevel = 0.0;
89         }
90         try {
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());
95         }
96     }
97
98     /**
99      * Static setLevel method for Rules DSL backward compatibility
100      */
101     public static void setLevel(ThingActions actions, @Nullable Double level, @Nullable Double fadeTime,
102             @Nullable Double delayTime) {
103         ((DimmerActions) actions).setLevel(level, fadeTime, delayTime);
104     }
105 }