]> git.basschouten.com Git - openhab-addons.git/blob
632197f0852fdb657a5fa0d56cfd8abc3822ed3c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.omnilink.internal.handler.units;
14
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.CHANNEL_UNIT_LEVEL;
16
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;
29
30 /**
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.
34  *
35  * @author Brian O'Connell - Initial contribution
36  * @author Ethan Dye - openHAB3 rewrite
37  */
38 @NonNullByDefault
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;
43
44     public DimmableUnitHandler(Thing thing) {
45         super(thing);
46     }
47
48     @Override
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);
54                 break;
55             default:
56                 logger.debug("Unknown channel for Dimmable Unit thing: {}", channelUID);
57                 super.handleCommand(channelUID, command);
58         }
59     }
60
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);
67         } else {
68             // Only handle percent or increase/decrease.
69             super.handleCommand(channelUID, command);
70         }
71     }
72
73     private void handlePercent(ChannelUID channelUID, PercentType command) {
74         logger.debug("handlePercent called for channel: {}, command: {}", channelUID, command);
75         int lightLevel = command.intValue();
76
77         if (lightLevel == 0) {
78             super.handleOnOff(channelUID, OnOffType.OFF);
79         } else if (lightLevel == 100) {
80             super.handleOnOff(channelUID, OnOffType.ON);
81         } else {
82             sendOmnilinkCommand(OmniLinkCmd.CMD_UNIT_PERCENT.getNumber(), lightLevel, thingID);
83         }
84     }
85
86     private void handleIncreaseDecrease(ChannelUID channelUID, IncreaseDecreaseType command) {
87         logger.debug("handleIncreaseDecrease called for channel: {}, command: {}", channelUID, command);
88         sendOmnilinkCommand(
89                 IncreaseDecreaseType.INCREASE.equals(command) ? OmniLinkCmd.CMD_UNIT_UNIT_BRIGHTEN_STEP_1.getNumber()
90                         : OmniLinkCmd.CMD_UNIT_UNIT_DIM_STEP_1.getNumber(),
91                 0, thingID);
92     }
93 }