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.govee.internal;
15 import static org.openhab.binding.bluetooth.govee.internal.GoveeBindingConstants.SUPPORTED_THING_TYPES_UIDS;
17 import java.util.HashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.bluetooth.BluetoothBindingConstants;
24 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
25 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
34 * The {@link GoveeDiscoveryParticipant} handles discovery of Govee bluetooth devices
36 * @author Connor Petty - Initial contribution
39 @Component(service = BluetoothDiscoveryParticipant.class)
40 public class GoveeDiscoveryParticipant implements BluetoothDiscoveryParticipant {
43 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
44 return SUPPORTED_THING_TYPES_UIDS;
47 private ThingUID getThingUID(BluetoothDiscoveryDevice device, ThingTypeUID thingTypeUID) {
48 return new ThingUID(thingTypeUID, device.getAdapter().getUID(),
49 device.getAddress().toString().toLowerCase().replace(":", ""));
53 public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
54 GoveeModel model = GoveeModel.getGoveeModel(device);
56 return getThingUID(device, model.getThingTypeUID());
62 public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
63 GoveeModel model = GoveeModel.getGoveeModel(device);
65 Map<String, Object> properties = new HashMap<>();
66 properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
67 properties.put(Thing.PROPERTY_VENDOR, "Govee");
68 properties.put(Thing.PROPERTY_MODEL_ID, model.name());
69 Integer txPower = device.getTxPower();
70 if (txPower != null) {
71 properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
74 // Create the discovery result and add to the inbox
75 return DiscoveryResultBuilder.create(getThingUID(device, model.getThingTypeUID()))
76 .withProperties(properties)
77 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
78 .withBridge(device.getAdapter().getUID()).withLabel(model.getLabel()).build();