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.luxom.internal.handler;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.concurrent.atomic.AtomicReference;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.luxom.internal.LuxomBindingConstants;
22 import org.openhab.binding.luxom.internal.handler.config.LuxomThingDimmerConfig;
23 import org.openhab.binding.luxom.internal.handler.util.PercentageConverter;
24 import org.openhab.binding.luxom.internal.protocol.LuxomAction;
25 import org.openhab.binding.luxom.internal.protocol.LuxomCommand;
26 import org.openhab.core.library.types.IncreaseDecreaseType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.PercentType;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingStatusDetail;
34 import org.openhab.core.types.Command;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link LuxomDimmerHandler} is responsible for handling commands, which are
40 * sent to one of the channels.
42 * @author Kris Jespers - Initial contribution
45 public class LuxomDimmerHandler extends LuxomThingHandler {
46 private final Logger logger = LoggerFactory.getLogger(LuxomDimmerHandler.class);
48 public LuxomDimmerHandler(Thing thing) {
52 private @Nullable LuxomThingDimmerConfig config;
53 private final AtomicReference<Integer> lastLightLevel = new AtomicReference<>(0);
56 public void initialize() {
58 config = getConfig().as(LuxomThingDimmerConfig.class);
60 logger.debug("Initializing Switch handler for address {}", getAddress());
66 protected void initDeviceState() {
67 logger.debug("Initializing device state for Switch {}", getAddress());
69 Bridge bridge = getBridge();
71 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
72 } else if (ThingStatus.ONLINE.equals(bridge.getStatus())) {
73 if (config != null && config.doesNotReply) {
74 logger.debug("Switch {} will not reply, so always keeping it ONLINE", getAddress());
75 updateStatus(ThingStatus.ONLINE);
77 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "@text/status.awaiting-initial-response");
78 ping(); // handleUpdate() will set thing status to online when response arrives
81 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
86 public void handleCommand(ChannelUID channelUID, Command command) {
87 logger.debug("dimmer at address {} received command {} for {}", getAddress(), command.toFullString(),
89 if (LuxomBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())) {
90 if (OnOffType.ON.equals(command)) {
92 } else if (OnOffType.OFF.equals(command)) {
95 } else if (LuxomBindingConstants.CHANNEL_BRIGHTNESS.equals(channelUID.getId()) && config != null) {
96 if (command instanceof Number number) {
97 int level = number.intValue();
98 logger.trace("dimmer at address {} just setting dimmer level", getAddress());
100 } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
101 int currentValue = lastLightLevel.get();
103 if (IncreaseDecreaseType.INCREASE.equals(increaseDecreaseCommand)) {
104 newValue = currentValue + config.stepPercentage;
105 // round down to step multiple
106 newValue = newValue - newValue % config.stepPercentage;
107 logger.trace("dimmer at address {} just increasing dimmer level", getAddress());
110 newValue = currentValue - config.stepPercentage;
111 // round up to step multiple
112 newValue = newValue + newValue % config.stepPercentage;
113 logger.trace("dimmer at address {} just increasing dimmer level", getAddress());
114 dim(Math.max(newValue, 0));
116 } else if (OnOffType.ON.equals(command)) {
117 if (config.onToLast) {
118 dim(lastLightLevel.get());
120 dim(config.onLevel.intValue());
122 } else if (OnOffType.OFF.equals(command)) {
129 public void handleCommandComingFromBridge(LuxomCommand command) {
130 updateStatus(ThingStatus.ONLINE);
131 if (LuxomAction.CLEAR_RESPONSE.equals(command.getAction())) {
132 updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.OFF);
133 } else if (LuxomAction.SET_RESPONSE.equals(command.getAction())) {
134 updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.ON);
135 } else if (LuxomAction.DATA_RESPONSE.equals(command.getAction())) {
136 int percentage = PercentageConverter.getPercentage(command.getData());
138 lastLightLevel.set(percentage);
139 updateState(LuxomBindingConstants.CHANNEL_BRIGHTNESS, new PercentType(percentage));
144 public void channelLinked(ChannelUID channelUID) {
145 logger.debug("dimmer at address {} linked to channel {}", getAddress(), channelUID);
146 if (LuxomBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())
147 || LuxomBindingConstants.CHANNEL_BRIGHTNESS.equals(channelUID.getId())) {
148 // Refresh state when new item is linked.
149 if (config != null && !config.doesNotReply) {
156 * example : *A,0,2,2B;*Z,057;
158 private void dim(int percentage) {
159 logger.debug("dimming dimmer at address {} to {} %", getAddress(), percentage);
160 List<CommandExecutionSpecification> commands = new ArrayList<>(3);
161 if (percentage == 0) {
162 commands.add(new CommandExecutionSpecification(LuxomAction.CLEAR.getCommand() + ",0," + getAddress()));
164 commands.add(new CommandExecutionSpecification(LuxomAction.SET.getCommand() + ",0," + getAddress()));
166 commands.add(new CommandExecutionSpecification(LuxomAction.DATA.getCommand() + ",0," + getAddress()));
167 commands.add(new CommandExecutionSpecification(
168 LuxomAction.DATA_BYTE.getCommand() + ",0" + PercentageConverter.getHexRepresentation(percentage)));
170 sendCommands(commands);