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.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.OmniLinkCmd;
20 import org.openhab.binding.omnilink.internal.handler.UnitHandler;
21 import org.openhab.core.library.types.IncreaseDecreaseType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.PercentType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.types.Command;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link DimmableUnitHandler} defines some methods that are used to
32 * interface with an OmniLink Dimmable Unit. This by extension also defines the
33 * Dimmable Unit things that openHAB will be able to pick up and interface with.
35 * @author Brian O'Connell - Initial contribution
36 * @author Ethan Dye - openHAB3 rewrite
39 public class DimmableUnitHandler extends UnitHandler {
40 private final Logger logger = LoggerFactory.getLogger(DimmableUnitHandler.class);
41 private final int thingID = getThingNumber();
42 public @Nullable String number;
44 public DimmableUnitHandler(Thing thing) {
49 public void handleCommand(ChannelUID channelUID, Command command) {
50 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
51 switch (channelUID.getId()) {
52 case CHANNEL_UNIT_LEVEL:
53 handleUnitLevel(channelUID, command);
56 logger.debug("Unknown channel for Dimmable Unit thing: {}", channelUID);
57 super.handleCommand(channelUID, command);
61 private void handleUnitLevel(ChannelUID channelUID, Command command) {
62 logger.debug("handleUnitLevel called for channel: {}, command: {}", channelUID, command);
63 if (command instanceof PercentType) {
64 handlePercent(channelUID, (PercentType) command);
65 } else if (command instanceof IncreaseDecreaseType) {
66 handleIncreaseDecrease(channelUID, (IncreaseDecreaseType) command);
68 // Only handle percent or increase/decrease.
69 super.handleCommand(channelUID, command);
73 private void handlePercent(ChannelUID channelUID, PercentType command) {
74 logger.debug("handlePercent called for channel: {}, command: {}", channelUID, command);
75 int lightLevel = command.intValue();
77 if (lightLevel == 0) {
78 super.handleOnOff(channelUID, OnOffType.OFF);
79 } else if (lightLevel == 100) {
80 super.handleOnOff(channelUID, OnOffType.ON);
82 sendOmnilinkCommand(OmniLinkCmd.CMD_UNIT_PERCENT.getNumber(), lightLevel, thingID);
86 private void handleIncreaseDecrease(ChannelUID channelUID, IncreaseDecreaseType command) {
87 logger.debug("handleIncreaseDecrease called for channel: {}, command: {}", channelUID, command);
89 IncreaseDecreaseType.INCREASE.equals(command) ? OmniLinkCmd.CMD_UNIT_UNIT_BRIGHTEN_STEP_1.getNumber()
90 : OmniLinkCmd.CMD_UNIT_UNIT_DIM_STEP_1.getNumber(),