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.am43.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.BluetoothDevice.ConnectionState;
23 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
24 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
33 * This discovery participant is able to recognize AM43 devices and create discovery results for them.
35 * @author Connor Petty - Initial contribution
40 public class AM43DiscoveryParticipant implements BluetoothDiscoveryParticipant {
43 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
44 return Set.of(AM43BindingConstants.THING_TYPE_AM43);
48 public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
49 ThingUID thingUID = getThingUID(device);
50 if (thingUID == null) {
53 String label = "AM43 Blind Drive Motor";
54 Map<String, Object> properties = new HashMap<>();
55 properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
56 properties.put(Thing.PROPERTY_MODEL_ID, "AM43-0.45/40-ES-EB");
57 properties.put(Thing.PROPERTY_VENDOR, "A-OK Precision motor Ltd.");
58 Integer txPower = device.getTxPower();
59 if (txPower != null) {
60 properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
63 // Create the discovery result and add to the inbox
64 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
65 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
66 .withBridge(device.getAdapter().getUID()).withLabel(label).build();
70 public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
71 if (device.getConnectionState() == ConnectionState.CONNECTED
72 && device.supportsService(AM43BindingConstants.SERVICE_UUID)) {
73 return new ThingUID(AM43BindingConstants.THING_TYPE_AM43, device.getAdapter().getUID(),
74 device.getAddress().toString().toLowerCase().replace(":", ""));
80 public boolean requiresConnection(BluetoothDiscoveryDevice device) {
81 return device.getManufacturerId() == null && device.getName() != null;