2 * Copyright (c) 2010-2023 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
36 * @author Davy Wong - Added Airthings Wave Gen 1 support
41 public class AirthingsDiscoveryParticipant implements BluetoothDiscoveryParticipant {
43 private static final int AIRTHINGS_COMPANY_ID = 820; // Formerly Corentium AS
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
50 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51 return AirthingsBindingConstants.SUPPORTED_THING_TYPES_UIDS;
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(":", ""));
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(":", ""));
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(":", ""));
74 public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
75 if (!isAirthingsDevice(device)) {
78 ThingUID thingUID = getThingUID(device);
79 if (thingUID == null) {
82 if (WAVE_PLUS_MODEL.equals(device.getModel())) {
83 return createResult(device, thingUID, "Airthings Wave Plus");
85 if (WAVE_MINI_MODEL.equals(device.getModel())) {
86 return createResult(device, thingUID, "Airthings Wave Mini");
88 if (WAVE_GEN1_MODEL.equals(device.getModel())) {
89 return createResult(device, thingUID, "Airthings Wave Gen 1");
95 public boolean requiresConnection(BluetoothDiscoveryDevice device) {
96 return isAirthingsDevice(device);
99 private boolean isAirthingsDevice(BluetoothDiscoveryDevice device) {
100 Integer manufacturerId = device.getManufacturerId();
101 return manufacturerId != null && manufacturerId == AIRTHINGS_COMPANY_ID;
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);
116 if (firmwareRevision != null) {
117 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareRevision);
120 properties.put(Thing.PROPERTY_MODEL_ID, model);
122 if (hardwareRevision != null) {
123 properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareRevision);
125 if (txPower != null) {
126 properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
128 properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getAddress().toString());
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();