]> git.basschouten.com Git - openhab-addons.git/blob
9800186cf1fddc684ab64d283136a1a039717135
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.bluetooth.govee.internal;
14
15 import static org.openhab.binding.bluetooth.govee.internal.GoveeBindingConstants.SUPPORTED_THING_TYPES_UIDS;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
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;
32
33 /**
34  * The {@link GoveeDiscoveryParticipant} handles discovery of Govee bluetooth devices
35  *
36  * @author Connor Petty - Initial contribution
37  */
38 @NonNullByDefault
39 @Component(service = BluetoothDiscoveryParticipant.class)
40 public class GoveeDiscoveryParticipant implements BluetoothDiscoveryParticipant {
41
42     @Override
43     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
44         return SUPPORTED_THING_TYPES_UIDS;
45     }
46
47     private ThingUID getThingUID(BluetoothDiscoveryDevice device, ThingTypeUID thingTypeUID) {
48         return new ThingUID(thingTypeUID, device.getAdapter().getUID(),
49                 device.getAddress().toString().toLowerCase().replace(":", ""));
50     }
51
52     @Override
53     public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
54         GoveeModel model = GoveeModel.getGoveeModel(device);
55         if (model != null) {
56             return getThingUID(device, model.getThingTypeUID());
57         }
58         return null;
59     }
60
61     @Override
62     public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
63         GoveeModel model = GoveeModel.getGoveeModel(device);
64         if (model != null) {
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));
72             }
73
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();
79         }
80         return null;
81     }
82 }