2 * Copyright (c) 2010-2022 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.omnilink.internal.handler.units;
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.CHANNEL_UNIT_LEVEL;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.omnilink.internal.handler.UnitHandler;
20 import org.openhab.core.library.types.IncreaseDecreaseType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.types.Command;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
32 * The {@link DimmableUnitHandler} defines some methods that are used to
33 * interface with an OmniLink Dimmable Unit. This by extension also defines the
34 * Dimmable Unit things that openHAB will be able to pick up and interface with.
36 * @author Brian O'Connell - Initial contribution
37 * @author Ethan Dye - openHAB3 rewrite
40 public class DimmableUnitHandler extends UnitHandler {
41 private final Logger logger = LoggerFactory.getLogger(DimmableUnitHandler.class);
42 private final int thingID = getThingNumber();
43 public @Nullable String number;
45 public DimmableUnitHandler(Thing thing) {
50 public void handleCommand(ChannelUID channelUID, Command command) {
51 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
52 switch (channelUID.getId()) {
53 case CHANNEL_UNIT_LEVEL:
54 handleUnitLevel(channelUID, command);
57 logger.debug("Unknown channel for Dimmable Unit thing: {}", channelUID);
58 super.handleCommand(channelUID, command);
62 private void handleUnitLevel(ChannelUID channelUID, Command command) {
63 logger.debug("handleUnitLevel called for channel: {}, command: {}", channelUID, command);
64 if (command instanceof PercentType) {
65 handlePercent(channelUID, (PercentType) command);
66 } else if (command instanceof IncreaseDecreaseType) {
67 handleIncreaseDecrease(channelUID, (IncreaseDecreaseType) command);
69 // Only handle percent or increase/decrease.
70 super.handleCommand(channelUID, command);
74 private void handlePercent(ChannelUID channelUID, PercentType command) {
75 logger.debug("handlePercent called for channel: {}, command: {}", channelUID, command);
76 int lightLevel = command.intValue();
78 if (lightLevel == 0) {
79 super.handleOnOff(channelUID, OnOffType.OFF);
80 } else if (lightLevel == 100) {
81 super.handleOnOff(channelUID, OnOffType.ON);
83 sendOmnilinkCommand(CommandMessage.CMD_UNIT_PERCENT, lightLevel, thingID);
87 private void handleIncreaseDecrease(ChannelUID channelUID, IncreaseDecreaseType command) {
88 logger.debug("handleIncreaseDecrease called for channel: {}, command: {}", channelUID, command);
89 sendOmnilinkCommand(IncreaseDecreaseType.INCREASE.equals(command) ? CommandMessage.CMD_UNIT_UPB_BRIGHTEN_STEP_1
90 : CommandMessage.CMD_UNIT_UPB_DIM_STEP_1, 0, thingID);