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.bluetooth.enoceanble.internal;
15 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.bluetooth.BluetoothBindingConstants;
22 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
23 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * This discovery participant is able to recognize enoceanble devices and create discovery results for them.
36 * @author Patrick Fink - Initial contribution
41 public class EnoceanBleDiscoveryParticipant implements BluetoothDiscoveryParticipant {
42 private final Logger logger = LoggerFactory.getLogger(EnoceanBleDiscoveryParticipant.class);
44 private static final int ENOCEAN_COMPANY_ID = 986;
47 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48 return Set.of(EnoceanBleBindingConstants.THING_TYPE_PTM215B);
52 public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
53 Integer manufacturerId = device.getManufacturerId();
54 logger.debug("Discovered device {} with manufacturerId {} and name {}", device.getAddress(), manufacturerId,
56 if (manufacturerId != null && manufacturerId == ENOCEAN_COMPANY_ID) {
57 return new ThingUID(EnoceanBleBindingConstants.THING_TYPE_PTM215B, device.getAdapter().getUID(),
58 device.getAddress().toString().toLowerCase().replace(":", ""));
64 public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
65 ThingUID thingUID = getThingUID(device);
66 if (thingUID == null) {
69 String label = "Enocean PTM215B Rocker";
70 Map<String, Object> properties = new HashMap<>();
71 properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
72 properties.put(Thing.PROPERTY_VENDOR, "EnOcean GmbH");
73 Integer txPower = device.getTxPower();
74 if (txPower != null) {
75 properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
78 // Create the discovery result and add to the inbox
79 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
80 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
81 .withBridge(device.getAdapter().getUID()).withLabel(label).build();