]> git.basschouten.com Git - openhab-addons.git/blob
9fc40e9f95b9d03481e040a361b2ed7640701fb0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.handler;
14
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.CHANNEL_LIGHTLEVEL;
16
17 import java.math.BigDecimal;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.concurrent.atomic.AtomicReference;
21
22 import org.openhab.binding.lutron.internal.action.DimmerActions;
23 import org.openhab.binding.lutron.internal.config.DimmerConfig;
24 import org.openhab.binding.lutron.internal.protocol.LutronDuration;
25 import org.openhab.binding.lutron.internal.protocol.OutputCommand;
26 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
27 import org.openhab.binding.lutron.internal.protocol.lip.TargetType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.thing.binding.ThingHandlerService;
36 import org.openhab.core.types.Command;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Handler responsible for communicating with a light dimmer.
42  *
43  * @author Allan Tong - Initial contribution
44  * @author Bob Adair - Added initDeviceState method, and onLevel and onToLast parameters
45  */
46 public class DimmerHandler extends LutronHandler {
47     private final Logger logger = LoggerFactory.getLogger(DimmerHandler.class);
48     private DimmerConfig config;
49     private LutronDuration fadeInTime;
50     private LutronDuration fadeOutTime;
51     private final AtomicReference<BigDecimal> lastLightLevel = new AtomicReference<>();
52
53     public DimmerHandler(Thing thing) {
54         super(thing);
55     }
56
57     @Override
58     public Collection<Class<? extends ThingHandlerService>> getServices() {
59         return List.of(DimmerActions.class);
60     }
61
62     @Override
63     public int getIntegrationId() {
64         if (config == null) {
65             throw new IllegalStateException("handler not initialized");
66         }
67
68         return config.integrationId;
69     }
70
71     @Override
72     public void initialize() {
73         config = getThing().getConfiguration().as(DimmerConfig.class);
74         if (config.integrationId <= 0) {
75             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId configured");
76             return;
77         }
78         fadeInTime = new LutronDuration(config.fadeInTime);
79         fadeOutTime = new LutronDuration(config.fadeOutTime);
80         logger.debug("Initializing Dimmer handler for integration ID {}", getIntegrationId());
81
82         initDeviceState();
83     }
84
85     @Override
86     protected void initDeviceState() {
87         logger.debug("Initializing device state for Dimmer {}", getIntegrationId());
88         Bridge bridge = getBridge();
89         if (bridge == null) {
90             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
91         } else if (bridge.getStatus() == ThingStatus.ONLINE) {
92             updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "Awaiting initial response");
93             queryOutput(TargetType.DIMMER, OutputCommand.ACTION_ZONELEVEL);
94             // handleUpdate() will set thing status to online when response arrives
95             lastLightLevel.set(config.onLevel);
96         } else {
97             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
98         }
99     }
100
101     @Override
102     public void channelLinked(ChannelUID channelUID) {
103         if (channelUID.getId().equals(CHANNEL_LIGHTLEVEL)) {
104             // Refresh state when new item is linked.
105             queryOutput(TargetType.DIMMER, OutputCommand.ACTION_ZONELEVEL);
106         }
107     }
108
109     @Override
110     public void handleCommand(ChannelUID channelUID, Command command) {
111         if (channelUID.getId().equals(CHANNEL_LIGHTLEVEL)) {
112             if (command instanceof Number number) {
113                 int level = number.intValue();
114                 output(TargetType.DIMMER, OutputCommand.ACTION_ZONELEVEL, level, new LutronDuration("0.25"), null);
115             } else if (command.equals(OnOffType.ON)) {
116                 if (config.onToLast) {
117                     output(TargetType.DIMMER, OutputCommand.ACTION_ZONELEVEL, lastLightLevel.get(), fadeInTime, null);
118                 } else {
119                     output(TargetType.DIMMER, OutputCommand.ACTION_ZONELEVEL, config.onLevel, fadeInTime, null);
120                 }
121             } else if (command.equals(OnOffType.OFF)) {
122                 output(TargetType.DIMMER, OutputCommand.ACTION_ZONELEVEL, 0, fadeOutTime, null);
123             }
124         }
125     }
126
127     public void setLightLevel(BigDecimal level, LutronDuration fade, LutronDuration delay) {
128         int intLevel = level.intValue();
129         output(TargetType.DIMMER, OutputCommand.ACTION_ZONELEVEL, intLevel, fade, delay);
130     }
131
132     @Override
133     public void handleUpdate(LutronCommandType type, String... parameters) {
134         if (type == LutronCommandType.OUTPUT && parameters.length > 1
135                 && OutputCommand.ACTION_ZONELEVEL.toString().equals(parameters[0])) {
136             BigDecimal level = new BigDecimal(parameters[1]);
137             if (getThing().getStatus() == ThingStatus.UNKNOWN) {
138                 updateStatus(ThingStatus.ONLINE);
139             }
140             if (level.compareTo(BigDecimal.ZERO) == 1) { // if (level > 0)
141                 lastLightLevel.set(level);
142             }
143             updateState(CHANNEL_LIGHTLEVEL, new PercentType(level));
144         }
145     }
146 }