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.enocean.internal.discovery;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
17 import java.util.Arrays;
18 import java.util.HashSet;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.config.discovery.usbserial.UsbSerialDeviceInformation;
26 import org.openhab.core.config.discovery.usbserial.UsbSerialDiscoveryParticipant;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
32 * Discovery for Enocean USB dongles, integrated in USB-serial discovery by implementing a component of type
33 * {@link UsbSerialDiscoveryParticipant}.
35 * Currently, this {@link UsbSerialDiscoveryParticipant} supports the Enocean USB300 dongles.
37 * @author Aitor Iturrioz - initial contribution
40 @Component(service = UsbSerialDiscoveryParticipant.class)
41 public class EnOceanUsbSerialDiscoveryParticipant implements UsbSerialDiscoveryParticipant {
43 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_BRIDGE));
45 public static final int ENOCEAN_USB300_DONGLE_VENDOR_ID = 0x0403;
46 public static final int ENOCEAN_USB300_DONGLE_PRODUCT_ID = 0x6001;
47 public static final String ENOCEAN_USB300_DONGLE_MANUFACTURER = "EnOcean GmbH";
48 public static final String ENOCEAN_USB300_DONGLE_PRODUCT = "usb 300";
49 public static final String ENOCEAN_USB300_DONGLE_DEFAULT_LABEL = "Enocean USB300 Dongle";
52 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53 return SUPPORTED_THING_TYPES;
57 public @Nullable DiscoveryResult createResult(UsbSerialDeviceInformation deviceInformation) {
58 if (isEnoceanUSB300Dongle(deviceInformation)) {
59 return DiscoveryResultBuilder.create(createBridgeThingType(deviceInformation))
60 .withLabel(createEnoceanUSB300DongleLabel(deviceInformation)).withRepresentationProperty(PATH)
61 .withProperty(PATH, deviceInformation.getSerialPort()).build();
68 public @Nullable ThingUID getThingUID(UsbSerialDeviceInformation deviceInformation) {
69 if (isEnoceanUSB300Dongle(deviceInformation)) {
70 return createBridgeThingType(deviceInformation);
76 private ThingUID createBridgeThingType(UsbSerialDeviceInformation deviceInformation) {
77 String serialNumber = deviceInformation.getSerialNumber();
78 if (serialNumber != null) {
79 return new ThingUID(THING_TYPE_BRIDGE, serialNumber);
81 return new ThingUID(THING_TYPE_BRIDGE, String.valueOf(deviceInformation.getProductId()));
85 private boolean isEnoceanUSB300Dongle(UsbSerialDeviceInformation deviceInformation) {
86 String manufacturer = deviceInformation.getManufacturer();
87 String product = deviceInformation.getProduct();
89 return deviceInformation.getVendorId() == ENOCEAN_USB300_DONGLE_VENDOR_ID
90 && deviceInformation.getProductId() == ENOCEAN_USB300_DONGLE_PRODUCT_ID && manufacturer != null
91 && manufacturer.equalsIgnoreCase(ENOCEAN_USB300_DONGLE_MANUFACTURER) && product != null
92 && product.toLowerCase().contains(ENOCEAN_USB300_DONGLE_PRODUCT);
95 private @Nullable String createEnoceanUSB300DongleLabel(UsbSerialDeviceInformation deviceInformation) {
96 String serialNumber = deviceInformation.getSerialNumber();
98 if (serialNumber != null && !serialNumber.isEmpty()) {
99 return String.format("%s (%s)", ENOCEAN_USB300_DONGLE_DEFAULT_LABEL, serialNumber);
101 return ENOCEAN_USB300_DONGLE_DEFAULT_LABEL;