]> git.basschouten.com Git - openhab-addons.git/blob
6eeccbd122a0c9e886998b5bd0303e2eb551c660
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.airthings.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
31 /**
32  * This discovery participant is able to recognize Airthings devices and create discovery results for them.
33  *
34  * @author Pauli Anttila - Initial contribution
35  * @author Kai Kreuzer - Added Airthings Wave Mini support
36  * @author Davy Wong - Added Airthings Wave Gen 1 support
37  *
38  */
39 @NonNullByDefault
40 @Component
41 public class AirthingsDiscoveryParticipant implements BluetoothDiscoveryParticipant {
42
43     private static final int AIRTHINGS_COMPANY_ID = 820; // Formerly Corentium AS
44
45     private static final String WAVE_PLUS_MODEL = "2930";
46     private static final String WAVE_MINI_MODEL = "2920";
47     private static final String WAVE_GEN1_MODEL = "2900"; // Wave 1st Gen SN 2900xxxxxx
48
49     @Override
50     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51         return AirthingsBindingConstants.SUPPORTED_THING_TYPES_UIDS;
52     }
53
54     @Override
55     public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
56         if (isAirthingsDevice(device)) {
57             if (WAVE_PLUS_MODEL.equals(device.getModel())) {
58                 return new ThingUID(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_PLUS,
59                         device.getAdapter().getUID(), device.getAddress().toString().toLowerCase().replace(":", ""));
60             }
61             if (WAVE_MINI_MODEL.equals(device.getModel())) {
62                 return new ThingUID(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_MINI,
63                         device.getAdapter().getUID(), device.getAddress().toString().toLowerCase().replace(":", ""));
64             }
65             if (WAVE_GEN1_MODEL.equals(device.getModel())) {
66                 return new ThingUID(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_GEN1,
67                         device.getAdapter().getUID(), device.getAddress().toString().toLowerCase().replace(":", ""));
68             }
69         }
70         return null;
71     }
72
73     @Override
74     public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
75         if (!isAirthingsDevice(device)) {
76             return null;
77         }
78         ThingUID thingUID = getThingUID(device);
79         if (thingUID == null) {
80             return null;
81         }
82         if (WAVE_PLUS_MODEL.equals(device.getModel())) {
83             return createResult(device, thingUID, "Airthings Wave Plus");
84         }
85         if (WAVE_MINI_MODEL.equals(device.getModel())) {
86             return createResult(device, thingUID, "Airthings Wave Mini");
87         }
88         if (WAVE_GEN1_MODEL.equals(device.getModel())) {
89             return createResult(device, thingUID, "Airthings Wave Gen 1");
90         }
91         return null;
92     }
93
94     @Override
95     public boolean requiresConnection(BluetoothDiscoveryDevice device) {
96         return isAirthingsDevice(device);
97     }
98
99     private boolean isAirthingsDevice(BluetoothDiscoveryDevice device) {
100         Integer manufacturerId = device.getManufacturerId();
101         return manufacturerId != null && manufacturerId == AIRTHINGS_COMPANY_ID;
102     }
103
104     private DiscoveryResult createResult(BluetoothDiscoveryDevice device, ThingUID thingUID, String label) {
105         Map<String, Object> properties = new HashMap<>();
106         properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
107         properties.put(Thing.PROPERTY_VENDOR, "Airthings AS");
108         String serialNumber = device.getSerialNumber();
109         String firmwareRevision = device.getFirmwareRevision();
110         String model = device.getModel();
111         String hardwareRevision = device.getHardwareRevision();
112         Integer txPower = device.getTxPower();
113         if (serialNumber != null) {
114             properties.put(Thing.PROPERTY_SERIAL_NUMBER, serialNumber);
115         }
116         if (firmwareRevision != null) {
117             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareRevision);
118         }
119         if (model != null) {
120             properties.put(Thing.PROPERTY_MODEL_ID, model);
121         }
122         if (hardwareRevision != null) {
123             properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareRevision);
124         }
125         if (txPower != null) {
126             properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
127         }
128         properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getAddress().toString());
129
130         // Create the discovery result and add to the inbox
131         return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
132                 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
133                 .withBridge(device.getAdapter().getUID()).withLabel(label).build();
134     }
135 }