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
14 package org.openhab.binding.lutron.internal.discovery;
16 import static org.openhab.binding.lutron.internal.LutronBindingConstants.*;
18 import java.net.InetAddress;
19 import java.util.Collections;
20 import java.util.HashMap;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
26 import javax.jmdns.ServiceInfo;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.openhab.core.config.discovery.DiscoveryResult;
31 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
32 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.osgi.service.component.annotations.Component;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link LutronMdnsBridgeDiscoveryService} discovers Lutron Caseta Smart Bridge Pro and eventually RA2 Select Main
42 * Repeater and other Lutron devices on the network using mDNS.
44 * @author Bob Adair - Initial contribution
48 public class LutronMdnsBridgeDiscoveryService implements MDNSDiscoveryParticipant {
50 // Lutron mDNS service <app>.<protocol>.<servicedomain>
51 private static final String LUTRON_MDNS_SERVICE_TYPE = "_lutron._tcp.local.";
53 private static final String PRODFAM_CASETA = "Caseta";
54 private static final String PRODTYP_CASETA_SBP2 = "Smart Bridge Pro 2";
55 private static final String DEVCLASS_CASETA_SBP2 = "08050100";
56 private static final String PRODFAM_RA2_SELECT = "RA2 Select";
57 private static final String PRODTYP_RA2_SELECT = "Main Repeater";
58 private static final String DEVCLASS_RA2_SELECT = "080E0401";
59 private static final String DEVCLASS_CONNECT_BRIDGE = "08090301";
60 private static final String DEFAULT_LABEL = "Unknown Lutron bridge";
62 private static final Pattern HOSTNAME_REGEX = Pattern.compile("lutron-([0-9a-f]+)\\."); // ex: lutron-01f1529a.local
64 private final Logger logger = LoggerFactory.getLogger(LutronMdnsBridgeDiscoveryService.class);
67 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
68 return Collections.singleton(THING_TYPE_IPBRIDGE);
72 public String getServiceType() {
73 return LUTRON_MDNS_SERVICE_TYPE;
77 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
78 if (!service.hasData()) {
82 String nice = service.getNiceTextString();
83 String qualifiedName = service.getQualifiedName();
85 InetAddress[] ipAddresses = service.getInetAddresses();
86 String devclass = service.getPropertyString("DEVCLASS");
87 String codever = service.getPropertyString("CODEVER");
88 String macaddr = service.getPropertyString("MACADDR");
90 logger.debug("Lutron mDNS bridge discovery notified of Lutron mDNS service: {}", nice);
91 logger.trace("Lutron mDNS service qualifiedName: {}", qualifiedName);
92 logger.trace("Lutron mDNS service ipAddresses: {} ({})", ipAddresses, ipAddresses.length);
93 logger.trace("Lutron mDNS service property DEVCLASS: {}", devclass);
94 logger.trace("Lutron mDNS service property CODEVER: {}", codever);
95 logger.trace("Lutron mDNS service property MACADDR: {}", macaddr);
97 Map<String, Object> properties = new HashMap<>();
98 String label = DEFAULT_LABEL;
100 if (ipAddresses.length < 1) {
103 if (ipAddresses.length > 1) {
104 logger.debug("Multiple addresses found for discovered Lutron device. Using only the first.");
106 properties.put(HOST, ipAddresses[0].getHostAddress());
108 String bridgeHostName = ipAddresses[0].getHostName();
109 logger.debug("Lutron mDNS bridge hostname: {}", bridgeHostName);
111 if (DEVCLASS_CASETA_SBP2.equals(devclass)) {
112 properties.put(PROPERTY_PRODFAM, PRODFAM_CASETA);
113 properties.put(PROPERTY_PRODTYP, PRODTYP_CASETA_SBP2);
114 label = PRODFAM_CASETA + " " + PRODTYP_CASETA_SBP2;
115 } else if (DEVCLASS_RA2_SELECT.equals(devclass)) {
116 properties.put(PROPERTY_PRODFAM, PRODFAM_RA2_SELECT);
117 properties.put(PROPERTY_PRODTYP, PRODTYP_RA2_SELECT);
118 label = PRODFAM_RA2_SELECT + " " + PRODTYP_RA2_SELECT;
119 } else if (DEVCLASS_CONNECT_BRIDGE.equals(devclass)) {
120 logger.debug("Lutron Connect Bridge discovered. Ignoring.");
123 logger.info("Lutron device with unknown DEVCLASS discovered via mDNS: {}. Configure device manually.",
125 return null; // For now, exit if service has unknown DEVCLASS
128 if (!bridgeHostName.equals(ipAddresses[0].getHostAddress())) {
129 label = label + " " + bridgeHostName;
132 if (codever != null) {
133 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, codever);
136 if (macaddr != null) {
137 properties.put(Thing.PROPERTY_MAC_ADDRESS, macaddr);
140 String sn = getSerial(service);
142 logger.trace("Lutron mDNS bridge serial number: {}", sn);
143 properties.put(SERIAL_NUMBER, sn);
145 logger.debug("Unable to determine serial number of discovered Lutron bridge device.");
149 ThingUID uid = getThingUID(service);
151 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withLabel(label).withProperties(properties)
152 .withRepresentationProperty(SERIAL_NUMBER).build();
153 logger.debug("Discovered Lutron bridge device via mDNS {}", uid);
156 logger.trace("Failed to create uid for discovered Lutron bridge device");
162 public @Nullable ThingUID getThingUID(ServiceInfo service) {
163 String serial = getSerial(service);
164 if (serial == null) {
167 return new ThingUID(THING_TYPE_IPBRIDGE, serial);
172 * Returns the device serial number for the mDNS service by extracting it from the hostname.
173 * Used as unique thing representation property.
175 * @param service Lutron mDNS service
176 * @return String containing serial number, or null if it cannot be determined
178 private @Nullable String getSerial(ServiceInfo service) {
179 InetAddress[] ipAddresses = service.getInetAddresses();
180 if (ipAddresses.length < 1) {
183 Matcher matcher = HOSTNAME_REGEX.matcher(ipAddresses[0].getHostName());
184 boolean matched = matcher.find();
185 String serialnum = null;
188 serialnum = matcher.group(1);
190 if (matched && serialnum != null && !serialnum.isEmpty()) {