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.omnilink.internal.handler.units;
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
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.DecimalType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.RefreshType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
32 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedUnitStatus;
35 * The {@link UpbRoomHandler} defines some methods that are used to
36 * interface with an OmniLink UPB Room. This by extension also defines the
37 * OmniPro UPB Room thing that openHAB will be able to pick up and interface with.
39 * @author Craig Hamilton - Initial contribution
40 * @author Ethan Dye - openHAB3 rewrite
43 public class UpbRoomHandler extends UnitHandler {
44 private final Logger logger = LoggerFactory.getLogger(UpbRoomHandler.class);
45 private final int thingID = getThingNumber();
46 public @Nullable String number;
48 public UpbRoomHandler(Thing thing) {
53 public void handleCommand(ChannelUID channelUID, Command command) {
54 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
56 if (command instanceof RefreshType) {
57 retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
58 ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
62 switch (channelUID.getId()) {
63 case CHANNEL_ROOM_SCENE_A:
64 case CHANNEL_ROOM_SCENE_B:
65 case CHANNEL_ROOM_SCENE_C:
66 case CHANNEL_ROOM_SCENE_D:
67 if (command instanceof OnOffType) {
68 handleRoomScene(channelUID, (OnOffType) command);
70 logger.debug("Invalid command: {}, must be OnOffType", command);
73 case CHANNEL_ROOM_SWITCH:
74 if (command instanceof OnOffType) {
75 super.handleOnOff(channelUID, (OnOffType) command);
77 logger.debug("Invalid command: {}, must be OnOffType", command);
80 case CHANNEL_ROOM_STATE:
81 if (command instanceof DecimalType) {
82 handleRoomState(channelUID, (DecimalType) command);
84 logger.debug("Invalid command: {}, must be DecimalType", command);
88 logger.debug("Unknown channel for UPB Room thing: {}", channelUID);
89 super.handleCommand(channelUID, command);
93 private void handleRoomScene(ChannelUID channelUID, OnOffType command) {
94 logger.debug("handleRoomScene called for channel: {}, command: {}", channelUID, command);
97 switch (channelUID.getId()) {
111 logger.warn("Unexpected UPB Room scene: {}", channelUID);
114 int roomNum = (thingID + 7) / 8;
115 int param2 = ((roomNum * 6) - 3) + linkNum;
116 sendOmnilinkCommand(OnOffType.ON.equals(command) ? CommandMessage.CMD_UNIT_UPB_LINK_ON
117 : CommandMessage.CMD_UNIT_UPB_LINK_OFF, 0, param2);
120 private void handleRoomState(ChannelUID channelUID, DecimalType command) {
121 logger.debug("handleRoomState called for channel: {}, command: {}", channelUID, command);
122 final int cmdValue = command.intValue();
128 cmd = CommandMessage.CMD_UNIT_OFF;
132 cmd = CommandMessage.CMD_UNIT_ON;
139 cmd = CommandMessage.CMD_UNIT_UPB_LINK_ON;
141 * A little magic with the link #'s: 0 and 1 are off and on, respectively.
142 * So A ends up being 2, but OmniLink Protocol expects an offset of 0. That
143 * is why we subtract the 2.
145 int roomNum = (thingID + 7) / 8;
146 param2 = ((roomNum * 6) - 3) + cmdValue - 2;
149 logger.warn("Unexpected UPB Room state: {}", Integer.toString(cmdValue));
153 sendOmnilinkCommand(cmd, 0, param2);
157 public void updateChannels(ExtendedUnitStatus status) {
158 logger.debug("updateChannels called for UPB Room status: {}", status);
159 int unitStatus = status.getStatus();
161 updateState(CHANNEL_ROOM_STATE, new DecimalType(unitStatus));
162 updateState(CHANNEL_ROOM_SWITCH, OnOffType.from(unitStatus == 1));
163 updateState(CHANNEL_ROOM_SCENE_A, OnOffType.from(unitStatus == 2));
164 updateState(CHANNEL_ROOM_SCENE_B, OnOffType.from(unitStatus == 3));
165 updateState(CHANNEL_ROOM_SCENE_C, OnOffType.from(unitStatus == 4));
166 updateState(CHANNEL_ROOM_SCENE_D, OnOffType.from(unitStatus == 5));