]> git.basschouten.com Git - openhab-addons.git/blob
cefdd8367bb0d4836b5c302b8e363225f2613e84
[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.grundfosalpha.internal;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Set;
18
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;
32
33 /**
34  * This discovery participant is able to recognize Grundfos Alpha devices and create discovery results for them.
35  *
36  * @author Markus Heberling - Initial contribution
37  *
38  */
39 @NonNullByDefault
40 @Component
41 public class GrundfosAlphaDiscoveryParticipant implements BluetoothDiscoveryParticipant {
42     private final Logger logger = LoggerFactory.getLogger(GrundfosAlphaDiscoveryParticipant.class);
43
44     @Override
45     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
46         return Set.of(GrundfosAlphaBindingConstants.THING_TYPE_MI401);
47     }
48
49     @Override
50     public boolean requiresConnection(BluetoothDiscoveryDevice device) {
51         return false;
52     }
53
54     @Override
55     public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
56         Integer manufacturerId = device.getManufacturerId();
57         @Nullable
58         String name = device.getName();
59         logger.debug("Discovered device {} with manufacturerId {} and name {}", device.getAddress(), manufacturerId,
60                 name);
61         if ("MI401".equals(name)) {
62             return new ThingUID(GrundfosAlphaBindingConstants.THING_TYPE_MI401, device.getAdapter().getUID(),
63                     device.getAddress().toString().toLowerCase().replace(":", ""));
64         }
65         return null;
66     }
67
68     @Override
69     public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
70         ThingUID thingUID = getThingUID(device);
71         if (thingUID == null) {
72             return null;
73         }
74         String label = "Grundfos Alpha Reader MI401";
75         Map<String, Object> properties = new HashMap<>();
76         properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
77         properties.put(Thing.PROPERTY_VENDOR, "Grundfos");
78         Integer txPower = device.getTxPower();
79         if (txPower != null) {
80             properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
81         }
82
83         // Create the discovery result and add to the inbox
84         return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
85                 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
86                 .withBridge(device.getAdapter().getUID()).withLabel(label).build();
87     }
88 }