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.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;
30 import org.openhab.core.types.State;
33 * Handler for RadioRA dimmers
35 * @author Jeff Lauterbach - Initial Contribution
38 public class DimmerHandler extends LutronHandler {
41 * Used to internally keep track of dimmer level. This helps us better respond
42 * to external dimmer changes since RadioRA protocol does not send dimmer
43 * levels in their messages.
45 private AtomicInteger lastKnownIntensity = new AtomicInteger(100);
47 private AtomicBoolean switchEnabled = new AtomicBoolean(false);
49 public DimmerHandler(Thing thing) {
54 public void handleCommand(ChannelUID channelUID, Command command) {
55 DimmerConfig config = getConfigAs(DimmerConfig.class);
57 if (LutronBindingConstants.CHANNEL_LIGHTLEVEL.equals(channelUID.getId())) {
58 if (command instanceof PercentType) {
59 int intensity = ((PercentType) command).intValue();
61 SetDimmerLevelCommand cmd = new SetDimmerLevelCommand(config.getZoneNumber(), intensity);
62 getRS232Handler().sendCommand(cmd);
64 updateInternalState(intensity);
67 if (command instanceof OnOffType) {
68 OnOffType onOffCmd = (OnOffType) command;
70 SetSwitchLevelCommand cmd = new SetSwitchLevelCommand(config.getZoneNumber(), onOffCmd);
71 getRS232Handler().sendCommand(cmd);
73 updateInternalState(onOffCmd);
80 public void handleUpdate(ChannelUID channelUID, State newState) {
81 if (LutronBindingConstants.CHANNEL_LIGHTLEVEL.equals(channelUID.getId())) {
82 PercentType percent = (PercentType) newState.as(PercentType.class);
83 updateInternalState(percent.intValue());
88 public void handleFeedback(RadioRAFeedback feedback) {
89 if (feedback instanceof LocalZoneChangeFeedback) {
90 handleLocalZoneChangeFeedback((LocalZoneChangeFeedback) feedback);
91 } else if (feedback instanceof ZoneMapFeedback) {
92 handleZoneMapFeedback((ZoneMapFeedback) feedback);
96 private void handleZoneMapFeedback(ZoneMapFeedback feedback) {
97 char value = feedback.getZoneValue(getConfigAs(DimmerConfig.class).getZoneNumber());
99 turnDimmerOnToLastKnownIntensity();
100 } else if (value == '0') {
105 private void handleLocalZoneChangeFeedback(LocalZoneChangeFeedback feedback) {
106 if (feedback.getZoneNumber() == getConfigAs(DimmerConfig.class).getZoneNumber()) {
107 if (LocalZoneChangeFeedback.State.ON.equals(feedback.getState())) {
108 turnDimmerOnToLastKnownIntensity();
109 } else if (LocalZoneChangeFeedback.State.OFF.equals(feedback.getState())) {
116 private void turnDimmerOnToLastKnownIntensity() {
117 if (!switchEnabled.get()) {
118 updateState(LutronBindingConstants.CHANNEL_LIGHTLEVEL, new PercentType(lastKnownIntensity.get()));
120 switchEnabled.set(true);
123 private void turnDimmerOff() {
124 updateState(LutronBindingConstants.CHANNEL_LIGHTLEVEL, PercentType.ZERO);
125 switchEnabled.set(false);
128 private void updateInternalState(int intensity) {
130 lastKnownIntensity.set(intensity);
132 switchEnabled.set(intensity > 0);
135 private void updateInternalState(OnOffType type) {
136 switchEnabled.set(OnOffType.ON.equals(type));