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;
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.CommandMessage;
35 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
36 import com.digitaldan.jomnilinkII.MessageTypes.properties.ButtonProperties;
39 * The {@link ButtonHandler} defines some methods that are used to
40 * interface with an OmniLink Button. This by extension also defines the
41 * Button thing that openHAB will be able to pick up and interface with.
43 * @author Craig Hamilton - Initial contribution
44 * @author Ethan Dye - openHAB3 rewrite
47 public class ButtonHandler extends AbstractOmnilinkHandler {
48 private final Logger logger = LoggerFactory.getLogger(ButtonHandler.class);
49 private final int thingID = getThingNumber();
50 public @Nullable String number;
52 public ButtonHandler(Thing thing) {
57 public void initialize() {
58 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
59 if (bridgeHandler != null) {
60 updateStatus(ThingStatus.ONLINE);
62 updateButtonProperties(bridgeHandler);
64 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
65 "Received null bridge while initializing Button!");
69 private void updateButtonProperties(OmnilinkBridgeHandler bridgeHandler) {
70 final List<AreaProperties> areas = getAreaProperties();
72 for (AreaProperties areaProperties : areas) {
73 int areaFilter = bitFilterForArea(areaProperties);
75 ObjectPropertyRequest<ButtonProperties> objectPropertyRequest = ObjectPropertyRequest
76 .builder(bridgeHandler, ObjectPropertyRequests.BUTTONS, thingID, 0).selectNamed()
77 .areaFilter(areaFilter).build();
79 for (ButtonProperties buttonProperties : objectPropertyRequest) {
80 Map<String, String> properties = editProperties();
81 properties.put(THING_PROPERTIES_NAME, buttonProperties.getName());
82 properties.put(THING_PROPERTIES_AREA, Integer.toString(areaProperties.getNumber()));
83 updateProperties(properties);
90 public void handleCommand(ChannelUID channelUID, Command command) {
91 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
93 if (command instanceof RefreshType) {
98 switch (channelUID.getId()) {
99 case CHANNEL_BUTTON_PRESS:
100 if (command instanceof OnOffType) {
101 sendOmnilinkCommand(CommandMessage.CMD_BUTTON, 0, thingID);
104 logger.debug("Invalid command: {}, must be OnOffType", command);
108 logger.warn("Unknown channel for Button thing: {}", channelUID);
112 public void buttonActivated() {
113 ChannelUID activateChannel = new ChannelUID(getThing().getUID(), TRIGGER_CHANNEL_BUTTON_ACTIVATED_EVENT);
114 triggerChannel(activateChannel);
117 public void updateChannels() {
118 updateState(CHANNEL_BUTTON_PRESS, OnOffType.OFF);