]> git.basschouten.com Git - openhab-addons.git/blob
7eb0d26996d349432c2574bdff623e8f19bfdcb8
[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.enoceanble.internal;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.bluetooth.BluetoothBindingConstants;
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;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This discovery participant is able to recognize enoceanble devices and create discovery results for them.
36  *
37  * @author Patrick Fink - Initial contribution
38  *
39  */
40 @NonNullByDefault
41 @Component
42 public class EnoceanBleDiscoveryParticipant implements BluetoothDiscoveryParticipant {
43     private final Logger logger = LoggerFactory.getLogger(EnoceanBleDiscoveryParticipant.class);
44
45     private static final int ENOCEAN_COMPANY_ID = 986;
46
47     @Override
48     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49         return Collections.singleton(EnoceanBleBindingConstants.THING_TYPE_PTM215B);
50     }
51
52     @Override
53     public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
54         Integer manufacturerId = device.getManufacturerId();
55         logger.debug("Discovered device {} with manufacturerId {} and name {}", device.getAddress(), manufacturerId,
56                 device.getName());
57         if (manufacturerId != null && manufacturerId == ENOCEAN_COMPANY_ID) {
58             return new ThingUID(EnoceanBleBindingConstants.THING_TYPE_PTM215B, device.getAdapter().getUID(),
59                     device.getAddress().toString().toLowerCase().replace(":", ""));
60         }
61         return null;
62     }
63
64     @Override
65     public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
66         ThingUID thingUID = getThingUID(device);
67         if (thingUID == null) {
68             return null;
69         }
70         String label = "Enocean PTM215B Rocker";
71         Map<String, Object> properties = new HashMap<>();
72         properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
73         properties.put(Thing.PROPERTY_VENDOR, "EnOcean GmbH");
74         Integer txPower = device.getTxPower();
75         if (txPower != null) {
76             properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
77         }
78
79         // Create the discovery result and add to the inbox
80         return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
81                 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
82                 .withBridge(device.getAdapter().getUID()).withLabel(label).build();
83     }
84 }