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) {
97 int level = ((Number) command).intValue();
98 logger.trace("dimmer at address {} just setting dimmer level", getAddress());
100 } else if (command instanceof IncreaseDecreaseType) {
101 IncreaseDecreaseType s = (IncreaseDecreaseType) command;
102 int currentValue = lastLightLevel.get();
104 if (IncreaseDecreaseType.INCREASE.equals(s)) {
105 newValue = currentValue + config.stepPercentage;
106 // round down to step multiple
107 newValue = newValue - newValue % config.stepPercentage;
108 logger.trace("dimmer at address {} just increasing dimmer level", getAddress());
111 newValue = currentValue - config.stepPercentage;
112 // round up to step multiple
113 newValue = newValue + newValue % config.stepPercentage;
114 logger.trace("dimmer at address {} just increasing dimmer level", getAddress());
115 dim(Math.max(newValue, 0));
117 } else if (OnOffType.ON.equals(command)) {
118 if (config.onToLast) {
119 dim(lastLightLevel.get());
121 dim(config.onLevel.intValue());
123 } else if (OnOffType.OFF.equals(command)) {
130 public void handleCommandComingFromBridge(LuxomCommand command) {
131 updateStatus(ThingStatus.ONLINE);
132 if (LuxomAction.CLEAR_RESPONSE.equals(command.getAction())) {
133 updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.OFF);
134 } else if (LuxomAction.SET_RESPONSE.equals(command.getAction())) {
135 updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.ON);
136 } else if (LuxomAction.DATA_RESPONSE.equals(command.getAction())) {
137 int percentage = PercentageConverter.getPercentage(command.getData());
139 lastLightLevel.set(percentage);
140 updateState(LuxomBindingConstants.CHANNEL_BRIGHTNESS, new PercentType(percentage));
145 public void channelLinked(ChannelUID channelUID) {
146 logger.debug("dimmer at address {} linked to channel {}", getAddress(), channelUID);
147 if (LuxomBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())
148 || LuxomBindingConstants.CHANNEL_BRIGHTNESS.equals(channelUID.getId())) {
149 // Refresh state when new item is linked.
150 if (config != null && !config.doesNotReply) {
157 * example : *A,0,2,2B;*Z,057;
159 private void dim(int percentage) {
160 logger.debug("dimming dimmer at address {} to {} %", getAddress(), percentage);
161 List<CommandExecutionSpecification> commands = new ArrayList<>(3);
162 if (percentage == 0) {
163 commands.add(new CommandExecutionSpecification(LuxomAction.CLEAR.getCommand() + ",0," + getAddress()));
165 commands.add(new CommandExecutionSpecification(LuxomAction.SET.getCommand() + ",0," + getAddress()));
167 commands.add(new CommandExecutionSpecification(LuxomAction.DATA.getCommand() + ",0," + getAddress()));
168 commands.add(new CommandExecutionSpecification(
169 LuxomAction.DATA_BYTE.getCommand() + ",0" + PercentageConverter.getHexRepresentation(percentage)));
171 sendCommands(commands);