2 * Copyright (c) 2010-2020 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.Collections;
16 import java.util.HashMap;
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;
33 * This discovery participant is able to recognize Airthings devices and create discovery results for them.
35 * @author Pauli Anttila - Initial contribution
39 @Component(immediate = true)
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";
47 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48 return Collections.singleton(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_PLUS);
52 public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
53 if (isAirthingsDevice(device)) {
54 if (WAVE_PLUS_MODEL.equals(device.getModel())) {
55 return new ThingUID(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_PLUS,
56 device.getAdapter().getUID(), device.getAddress().toString().toLowerCase().replace(":", ""));
63 public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
64 if (!isAirthingsDevice(device)) {
67 ThingUID thingUID = getThingUID(device);
68 if (thingUID == null) {
71 if (WAVE_PLUS_MODEL.equals(device.getModel())) {
72 return createWavePlus(device, thingUID);
78 public boolean requiresConnection(BluetoothDiscoveryDevice device) {
79 return isAirthingsDevice(device);
82 private boolean isAirthingsDevice(BluetoothDiscoveryDevice device) {
83 Integer manufacturerId = device.getManufacturerId();
84 if (manufacturerId != null && manufacturerId == AIRTHINGS_COMPANY_ID) {
90 private DiscoveryResult createWavePlus(BluetoothDiscoveryDevice device, ThingUID thingUID) {
91 Map<String, Object> properties = new HashMap<>();
92 properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
93 properties.put(Thing.PROPERTY_VENDOR, "Airthings AS");
94 String serialNumber = device.getSerialNumber();
95 String firmwareRevision = device.getFirmwareRevision();
96 String model = device.getModel();
97 String hardwareRevision = device.getHardwareRevision();
98 Integer txPower = device.getTxPower();
99 if (serialNumber != null) {
100 properties.put(Thing.PROPERTY_SERIAL_NUMBER, serialNumber);
102 if (firmwareRevision != null) {
103 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareRevision);
106 properties.put(Thing.PROPERTY_MODEL_ID, model);
108 if (hardwareRevision != null) {
109 properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareRevision);
111 if (txPower != null) {
112 properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
114 properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getAddress().toString());
116 // Create the discovery result and add to the inbox
117 return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
118 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
119 .withBridge(device.getAdapter().getUID()).withLabel("Airthings Wave+").build();