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;
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
17 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequest;
23 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequests;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
35 import com.digitaldan.jomnilinkII.MessageTypes.properties.ButtonProperties;
38 * The {@link ButtonHandler} defines some methods that are used to
39 * interface with an OmniLink Button. This by extension also defines the
40 * Button thing that openHAB will be able to pick up and interface with.
42 * @author Craig Hamilton - Initial contribution
43 * @author Ethan Dye - openHAB3 rewrite
46 public class ButtonHandler extends AbstractOmnilinkHandler {
47 private final Logger logger = LoggerFactory.getLogger(ButtonHandler.class);
48 private final int thingID = getThingNumber();
49 public @Nullable String number;
51 public ButtonHandler(Thing thing) {
56 public void initialize() {
57 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
58 if (bridgeHandler != null) {
59 updateStatus(ThingStatus.ONLINE);
61 updateButtonProperties(bridgeHandler);
63 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
64 "Received null bridge while initializing Button!");
68 private void updateButtonProperties(OmnilinkBridgeHandler bridgeHandler) {
69 final List<AreaProperties> areas = getAreaProperties();
71 for (AreaProperties areaProperties : areas) {
72 int areaFilter = bitFilterForArea(areaProperties);
74 ObjectPropertyRequest<ButtonProperties> objectPropertyRequest = ObjectPropertyRequest
75 .builder(bridgeHandler, ObjectPropertyRequests.BUTTONS, thingID, 0).selectNamed()
76 .areaFilter(areaFilter).build();
78 for (ButtonProperties buttonProperties : objectPropertyRequest) {
79 Map<String, String> properties = editProperties();
80 properties.put(THING_PROPERTIES_NAME, buttonProperties.getName());
81 properties.put(THING_PROPERTIES_AREA, Integer.toString(areaProperties.getNumber()));
82 updateProperties(properties);
89 public void handleCommand(ChannelUID channelUID, Command command) {
90 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
92 if (command instanceof RefreshType) {
97 switch (channelUID.getId()) {
98 case CHANNEL_BUTTON_PRESS:
99 if (command instanceof OnOffType) {
100 sendOmnilinkCommand(OmniLinkCmd.CMD_BUTTON.getNumber(), 0, thingID);
103 logger.debug("Invalid command: {}, must be OnOffType", command);
107 logger.warn("Unknown channel for Button thing: {}", channelUID);
111 public void buttonActivated() {
112 ChannelUID activateChannel = new ChannelUID(getThing().getUID(), TRIGGER_CHANNEL_BUTTON_ACTIVATED_EVENT);
113 triggerChannel(activateChannel);
116 public void updateChannels() {
117 updateState(CHANNEL_BUTTON_PRESS, OnOffType.OFF);