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.lirc.internal.discovery;
15 import java.util.HashMap;
18 import org.openhab.binding.lirc.internal.LIRCBindingConstants;
19 import org.openhab.binding.lirc.internal.LIRCMessageListener;
20 import org.openhab.binding.lirc.internal.handler.LIRCBridgeHandler;
21 import org.openhab.binding.lirc.internal.messages.LIRCButtonEvent;
22 import org.openhab.binding.lirc.internal.messages.LIRCResponse;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
33 * @author Andrew Nagle - Initial contribution
35 public class LIRCRemoteDiscoveryService extends AbstractDiscoveryService implements LIRCMessageListener {
37 private final Logger logger = LoggerFactory.getLogger(LIRCRemoteDiscoveryService.class);
39 private LIRCBridgeHandler bridgeHandler;
41 public LIRCRemoteDiscoveryService(LIRCBridgeHandler lircBridgeHandler) {
42 super(LIRCBindingConstants.SUPPORTED_DEVICE_TYPES, LIRCBindingConstants.DISCOVERY_TIMOUT, true);
43 this.bridgeHandler = lircBridgeHandler;
44 bridgeHandler.registerMessageListener(this);
48 protected void startScan() {
49 logger.debug("Discovery service scan started");
50 bridgeHandler.startDeviceDiscovery();
54 public void onButtonPressed(ThingUID bridge, LIRCButtonEvent buttonEvent) {
55 addRemote(bridge, buttonEvent.getRemote());
59 public void onMessageReceived(ThingUID bridge, LIRCResponse message) {
60 LIRCResponse response = message;
61 String command = response.getCommand();
62 if ("LIST".equals(command) && response.isSuccess()) {
63 for (String remoteID : response.getData()) {
64 addRemote(bridge, remoteID);
69 private void addRemote(ThingUID bridge, String remote) {
70 ThingTypeUID uid = LIRCBindingConstants.THING_TYPE_REMOTE;
71 ThingUID thingUID = new ThingUID(uid, bridge, remote);
73 logger.trace("Remote {}: Discovered new remote.", remote);
74 Map<String, Object> properties = new HashMap<>(1);
75 properties.put(LIRCBindingConstants.PROPERTY_REMOTE, remote);
76 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withLabel(remote).withBridge(bridge)
77 .withProperties(properties).build();
78 thingDiscovered(discoveryResult);