]> git.basschouten.com Git - openhab-addons.git/blob
84f002cbca09975ca108a0118105629b2d609587
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.Collections;
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.LutronCommandType;
25 import org.openhab.binding.lutron.internal.protocol.LutronDuration;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.thing.binding.ThingHandlerService;
34 import org.openhab.core.types.Command;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Handler responsible for communicating with a light dimmer.
40  *
41  * @author Allan Tong - Initial contribution
42  * @author Bob Adair - Added initDeviceState method, and onLevel and onToLast parameters
43  */
44 public class DimmerHandler extends LutronHandler {
45     private static final Integer ACTION_ZONELEVEL = 1;
46
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 Collections.singletonList(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(ACTION_ZONELEVEL); // handleUpdate() will set thing status to online when response arrives
94             lastLightLevel.set(config.onLevel);
95         } else {
96             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
97         }
98     }
99
100     @Override
101     public void channelLinked(ChannelUID channelUID) {
102         if (channelUID.getId().equals(CHANNEL_LIGHTLEVEL)) {
103             // Refresh state when new item is linked.
104             queryOutput(ACTION_ZONELEVEL);
105         }
106     }
107
108     @Override
109     public void handleCommand(ChannelUID channelUID, Command command) {
110         if (channelUID.getId().equals(CHANNEL_LIGHTLEVEL)) {
111             if (command instanceof Number) {
112                 int level = ((Number) command).intValue();
113                 output(ACTION_ZONELEVEL, level, 0.25);
114             } else if (command.equals(OnOffType.ON)) {
115                 if (config.onToLast) {
116                     output(ACTION_ZONELEVEL, lastLightLevel.get(), fadeInTime);
117                 } else {
118                     output(ACTION_ZONELEVEL, config.onLevel, fadeInTime);
119                 }
120             } else if (command.equals(OnOffType.OFF)) {
121                 output(ACTION_ZONELEVEL, 0, fadeOutTime);
122             }
123         }
124     }
125
126     public void setLightLevel(BigDecimal level, LutronDuration fade, LutronDuration delay) {
127         int intLevel = level.intValue();
128         output(ACTION_ZONELEVEL, intLevel, fade, delay);
129     }
130
131     @Override
132     public void handleUpdate(LutronCommandType type, String... parameters) {
133         if (type == LutronCommandType.OUTPUT && parameters.length > 1
134                 && ACTION_ZONELEVEL.toString().equals(parameters[0])) {
135             BigDecimal level = new BigDecimal(parameters[1]);
136             if (getThing().getStatus() == ThingStatus.UNKNOWN) {
137                 updateStatus(ThingStatus.ONLINE);
138             }
139             if (level.compareTo(BigDecimal.ZERO) == 1) { // if (level > 0)
140                 lastLightLevel.set(level);
141             }
142             updateState(CHANNEL_LIGHTLEVEL, new PercentType(level));
143         }
144     }
145 }