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.Nullable;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.config.discovery.usbserial.UsbSerialDeviceInformation;
25 import org.openhab.core.config.discovery.usbserial.UsbSerialDiscoveryParticipant;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Component;
31 * Discovery for Enocean USB dongles, integrated in USB-serial discovery by implementing a component of type
32 * {@link UsbSerialDiscoveryParticipant}.
34 * Currently, this {@link UsbSerialDiscoveryParticipant} supports the Enocean USB300 dongles.
36 * @author Aitor Iturrioz - initial contribution
38 @Component(service = UsbSerialDiscoveryParticipant.class)
39 public class EnOceanUsbSerialDiscoveryParticipant implements UsbSerialDiscoveryParticipant {
41 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_BRIDGE));
43 public static final int ENOCEAN_USB300_DONGLE_VENDOR_ID = 0x0403;
44 public static final int ENOCEAN_USB300_DONGLE_PRODUCT_ID = 0x6001;
45 public static final String ENOCEAN_USB300_DONGLE_MANUFACTURER = "EnOcean GmbH";
46 public static final String ENOCEAN_USB300_DONGLE_PRODUCT = "usb 300";
47 public static final String ENOCEAN_USB300_DONGLE_DEFAULT_LABEL = "Enocean USB300 Dongle";
50 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51 return SUPPORTED_THING_TYPES;
55 public @Nullable DiscoveryResult createResult(UsbSerialDeviceInformation deviceInformation) {
56 if (isEnoceanUSB300Dongle(deviceInformation)) {
57 return DiscoveryResultBuilder.create(createBridgeThingType(deviceInformation))
58 .withLabel(createEnoceanUSB300DongleLabel(deviceInformation)).withRepresentationProperty(PATH)
59 .withProperty(PATH, deviceInformation.getSerialPort()).build();
66 public @Nullable ThingUID getThingUID(UsbSerialDeviceInformation deviceInformation) {
67 if (isEnoceanUSB300Dongle(deviceInformation)) {
68 return createBridgeThingType(deviceInformation);
74 private ThingUID createBridgeThingType(UsbSerialDeviceInformation deviceInformation) {
75 String serialNumber = deviceInformation.getSerialNumber();
76 if (serialNumber != null) {
77 return new ThingUID(THING_TYPE_BRIDGE, serialNumber);
79 return new ThingUID(THING_TYPE_BRIDGE, String.valueOf(deviceInformation.getProductId()));
83 private boolean isEnoceanUSB300Dongle(UsbSerialDeviceInformation deviceInformation) {
84 String manufacturer = deviceInformation.getManufacturer();
85 String product = deviceInformation.getProduct();
87 return deviceInformation.getVendorId() == ENOCEAN_USB300_DONGLE_VENDOR_ID
88 && deviceInformation.getProductId() == ENOCEAN_USB300_DONGLE_PRODUCT_ID && manufacturer != null
89 && manufacturer.equalsIgnoreCase(ENOCEAN_USB300_DONGLE_MANUFACTURER) && product != null
90 && product.toLowerCase().contains(ENOCEAN_USB300_DONGLE_PRODUCT);
93 private @Nullable String createEnoceanUSB300DongleLabel(UsbSerialDeviceInformation deviceInformation) {
94 String serialNumber = deviceInformation.getSerialNumber();
96 if (serialNumber != null && !serialNumber.isEmpty()) {
97 return String.format("%s (%s)", ENOCEAN_USB300_DONGLE_DEFAULT_LABEL, serialNumber);
99 return ENOCEAN_USB300_DONGLE_DEFAULT_LABEL;