2 * Copyright (c) 2010-2023 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.List;
20 import java.util.concurrent.atomic.AtomicReference;
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;
41 * Handler responsible for communicating with a light dimmer.
43 * @author Allan Tong - Initial contribution
44 * @author Bob Adair - Added initDeviceState method, and onLevel and onToLast parameters
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<>();
53 public DimmerHandler(Thing thing) {
58 public Collection<Class<? extends ThingHandlerService>> getServices() {
59 return List.of(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(TargetType.DIMMER, OutputCommand.ACTION_ZONELEVEL);
94 // handleUpdate() will set thing status to online when response arrives
95 lastLightLevel.set(config.onLevel);
97 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
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);
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);
119 output(TargetType.DIMMER, OutputCommand.ACTION_ZONELEVEL, config.onLevel, fadeInTime, null);
121 } else if (command.equals(OnOffType.OFF)) {
122 output(TargetType.DIMMER, OutputCommand.ACTION_ZONELEVEL, 0, fadeOutTime, null);
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);
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);
140 if (level.compareTo(BigDecimal.ZERO) == 1) { // if (level > 0)
141 lastLightLevel.set(level);
143 updateState(CHANNEL_LIGHTLEVEL, new PercentType(level));