2 * Copyright (c) 2010-2021 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.airthings.internal;
15 import java.util.HashMap;
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;
32 * This discovery participant is able to recognize Airthings devices and create discovery results for them.
34 * @author Pauli Anttila - Initial contribution
35 * @author Kai Kreuzer - Added Airthings Wave Mini support
40 public class AirthingsDiscoveryParticipant implements BluetoothDiscoveryParticipant {
42 private static final int AIRTHINGS_COMPANY_ID = 820; // Formerly Corentium AS
44 private static final String WAVE_PLUS_MODEL = "2930";
45 private static final String WAVE_MINI_MODEL = "2920";
48 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49 return AirthingsBindingConstants.SUPPORTED_THING_TYPES_UIDS;
53 public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
54 if (isAirthingsDevice(device)) {
55 if (WAVE_PLUS_MODEL.equals(device.getModel())) {
56 return new ThingUID(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_PLUS,
57 device.getAdapter().getUID(), device.getAddress().toString().toLowerCase().replace(":", ""));
59 if (WAVE_MINI_MODEL.equals(device.getModel())) {
60 return new ThingUID(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_MINI,
61 device.getAdapter().getUID(), device.getAddress().toString().toLowerCase().replace(":", ""));
68 public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
69 if (!isAirthingsDevice(device)) {
72 ThingUID thingUID = getThingUID(device);
73 if (thingUID == null) {
76 if (WAVE_PLUS_MODEL.equals(device.getModel())) {
77 return createResult(device, thingUID, "Airthings Wave Plus");
79 if (WAVE_MINI_MODEL.equals(device.getModel())) {
80 return createResult(device, thingUID, "Airthings Wave Mini");
86 public boolean requiresConnection(BluetoothDiscoveryDevice device) {
87 return isAirthingsDevice(device);
90 private boolean isAirthingsDevice(BluetoothDiscoveryDevice device) {
91 Integer manufacturerId = device.getManufacturerId();
92 if (manufacturerId != null && manufacturerId == AIRTHINGS_COMPANY_ID) {
98 private DiscoveryResult createResult(BluetoothDiscoveryDevice device, ThingUID thingUID, String label) {
99 Map<String, Object> properties = new HashMap<>();
100 properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
101 properties.put(Thing.PROPERTY_VENDOR, "Airthings AS");
102 String serialNumber = device.getSerialNumber();
103 String firmwareRevision = device.getFirmwareRevision();
104 String model = device.getModel();
105 String hardwareRevision = device.getHardwareRevision();
106 Integer txPower = device.getTxPower();
107 if (serialNumber != null) {
108 properties.put(Thing.PROPERTY_SERIAL_NUMBER, serialNumber);
110 if (firmwareRevision != null) {
111 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareRevision);
114 properties.put(Thing.PROPERTY_MODEL_ID, model);
116 if (hardwareRevision != null) {
117 properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareRevision);
119 if (txPower != null) {
120 properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
122 properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getAddress().toString());
124 // Create the discovery result and add to the inbox
125 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
126 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
127 .withBridge(device.getAdapter().getUID()).withLabel(label).build();