2 * Copyright (c) 2010-2021 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.radiora.handler;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.concurrent.atomic.AtomicInteger;
18 import org.openhab.binding.lutron.internal.LutronBindingConstants;
19 import org.openhab.binding.lutron.internal.radiora.config.DimmerConfig;
20 import org.openhab.binding.lutron.internal.radiora.protocol.LocalZoneChangeFeedback;
21 import org.openhab.binding.lutron.internal.radiora.protocol.RadioRAFeedback;
22 import org.openhab.binding.lutron.internal.radiora.protocol.SetDimmerLevelCommand;
23 import org.openhab.binding.lutron.internal.radiora.protocol.SetSwitchLevelCommand;
24 import org.openhab.binding.lutron.internal.radiora.protocol.ZoneMapFeedback;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.PercentType;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.types.Command;
32 * Handler for RadioRA dimmers
34 * @author Jeff Lauterbach - Initial Contribution
37 public class DimmerHandler extends LutronHandler {
40 * Used to internally keep track of dimmer level. This helps us better respond
41 * to external dimmer changes since RadioRA protocol does not send dimmer
42 * levels in their messages.
44 private AtomicInteger lastKnownIntensity = new AtomicInteger(100);
46 private AtomicBoolean switchEnabled = new AtomicBoolean(false);
48 public DimmerHandler(Thing thing) {
53 public void handleCommand(ChannelUID channelUID, Command command) {
54 DimmerConfig config = getConfigAs(DimmerConfig.class);
56 if (LutronBindingConstants.CHANNEL_LIGHTLEVEL.equals(channelUID.getId())) {
57 if (command instanceof PercentType) {
58 int intensity = ((PercentType) command).intValue();
60 SetDimmerLevelCommand cmd = new SetDimmerLevelCommand(config.getZoneNumber(), intensity);
61 getRS232Handler().sendCommand(cmd);
63 updateInternalState(intensity);
66 if (command instanceof OnOffType) {
67 OnOffType onOffCmd = (OnOffType) command;
69 SetSwitchLevelCommand cmd = new SetSwitchLevelCommand(config.getZoneNumber(), onOffCmd);
70 getRS232Handler().sendCommand(cmd);
72 updateInternalState(onOffCmd);
79 public void handleFeedback(RadioRAFeedback feedback) {
80 if (feedback instanceof LocalZoneChangeFeedback) {
81 handleLocalZoneChangeFeedback((LocalZoneChangeFeedback) feedback);
82 } else if (feedback instanceof ZoneMapFeedback) {
83 handleZoneMapFeedback((ZoneMapFeedback) feedback);
87 private void handleZoneMapFeedback(ZoneMapFeedback feedback) {
88 char value = feedback.getZoneValue(getConfigAs(DimmerConfig.class).getZoneNumber());
90 turnDimmerOnToLastKnownIntensity();
91 } else if (value == '0') {
96 private void handleLocalZoneChangeFeedback(LocalZoneChangeFeedback feedback) {
97 if (feedback.getZoneNumber() == getConfigAs(DimmerConfig.class).getZoneNumber()) {
98 if (LocalZoneChangeFeedback.State.ON.equals(feedback.getState())) {
99 turnDimmerOnToLastKnownIntensity();
100 } else if (LocalZoneChangeFeedback.State.OFF.equals(feedback.getState())) {
107 private void turnDimmerOnToLastKnownIntensity() {
108 if (!switchEnabled.get()) {
109 updateState(LutronBindingConstants.CHANNEL_LIGHTLEVEL, new PercentType(lastKnownIntensity.get()));
111 switchEnabled.set(true);
114 private void turnDimmerOff() {
115 updateState(LutronBindingConstants.CHANNEL_LIGHTLEVEL, PercentType.ZERO);
116 switchEnabled.set(false);
119 private void updateInternalState(int intensity) {
121 lastKnownIntensity.set(intensity);
123 switchEnabled.set(intensity > 0);
126 private void updateInternalState(OnOffType type) {
127 switchEnabled.set(OnOffType.ON.equals(type));