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.internal.handler;
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.CHANNEL_LIGHTLEVEL;
17 import java.math.BigDecimal;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.concurrent.atomic.AtomicReference;
22 import org.openhab.binding.lutron.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;
39 * Handler responsible for communicating with a light dimmer.
41 * @author Allan Tong - Initial contribution
42 * @author Bob Adair - Added initDeviceState method, and onLevel and onToLast parameters
44 public class DimmerHandler extends LutronHandler {
45 private static final Integer ACTION_ZONELEVEL = 1;
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<>();
53 public DimmerHandler(Thing thing) {
58 public Collection<Class<? extends ThingHandlerService>> getServices() {
59 return Collections.singletonList(DimmerActions.class);
63 public int getIntegrationId() {
65 throw new IllegalStateException("handler not initialized");
68 return config.integrationId;
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");
78 fadeInTime = new LutronDuration(config.fadeInTime);
79 fadeOutTime = new LutronDuration(config.fadeOutTime);
80 logger.debug("Initializing Dimmer handler for integration ID {}", getIntegrationId());
86 protected void initDeviceState() {
87 logger.debug("Initializing device state for Dimmer {}", getIntegrationId());
88 Bridge bridge = getBridge();
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);
96 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
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);
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);
118 output(ACTION_ZONELEVEL, config.onLevel, fadeInTime);
120 } else if (command.equals(OnOffType.OFF)) {
121 output(ACTION_ZONELEVEL, 0, fadeOutTime);
126 public void setLightLevel(BigDecimal level, LutronDuration fade, LutronDuration delay) {
127 int intLevel = level.intValue();
128 output(ACTION_ZONELEVEL, intLevel, fade, delay);
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);
139 if (level.compareTo(BigDecimal.ZERO) == 1) { // if (level > 0)
140 lastLightLevel.set(level);
142 updateState(CHANNEL_LIGHTLEVEL, new PercentType(level));