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.lutron.internal.discovery;
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.*;
17 import java.net.InetAddress;
18 import java.util.HashMap;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import javax.jmdns.ServiceInfo;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.osgi.service.component.annotations.Component;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link LutronMdnsBridgeDiscoveryService} discovers Lutron Caseta Smart Bridge, Caseta Smart Bridge Pro, RA2
40 * Select Main Repeater, and other Lutron devices on the network using mDNS.
42 * @author Bob Adair - Initial contribution
46 public class LutronMdnsBridgeDiscoveryService implements MDNSDiscoveryParticipant {
48 // Lutron mDNS service <app>.<protocol>.<servicedomain>
49 private static final String LUTRON_MDNS_SERVICE_TYPE = "_lutron._tcp.local.";
51 private static final String PRODFAM_CASETA = "Caseta";
52 private static final String PRODTYP_CASETA_SB = "Smart Bridge";
53 private static final String DEVCLASS_CASETA_SB = "08040100";
54 private static final String PRODTYP_CASETA_SBP2 = "Smart Bridge Pro 2";
55 private static final String DEVCLASS_CASETA_SBP2 = "08050100";
57 private static final String PRODFAM_RA2_SELECT = "RA2 Select";
58 private static final String PRODTYP_RA2_SELECT = "Main Repeater";
59 private static final String DEVCLASS_RA2_SELECT = "080E0401";
61 private static final String PRODFAM_RA3 = "RadioRA 3";
62 private static final String PRODTYP_RA3 = "Processor";
63 private static final String DEVCLASS_RA3 = "081B0101";
65 private static final String DEVCLASS_CONNECT_BRIDGE = "08090301";
66 private static final String DEFAULT_LABEL = "Unknown Lutron bridge";
68 private static final Pattern HOSTNAME_REGEX = Pattern.compile("lutron-([0-9a-f]+)\\."); // ex: lutron-01f1529a.local
70 private final Logger logger = LoggerFactory.getLogger(LutronMdnsBridgeDiscoveryService.class);
73 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
74 return Set.of(THING_TYPE_IPBRIDGE);
78 public String getServiceType() {
79 return LUTRON_MDNS_SERVICE_TYPE;
83 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
84 if (!service.hasData()) {
88 String nice = service.getNiceTextString();
89 String qualifiedName = service.getQualifiedName();
91 InetAddress[] ipAddresses = service.getInetAddresses();
92 String devclass = service.getPropertyString("DEVCLASS");
93 String codever = service.getPropertyString("CODEVER");
94 String macaddr = service.getPropertyString("MACADDR");
96 logger.debug("Lutron mDNS bridge discovery notified of Lutron mDNS service: {}", nice);
97 logger.trace("Lutron mDNS service qualifiedName: {}", qualifiedName);
98 logger.trace("Lutron mDNS service ipAddresses: {} ({})", ipAddresses, ipAddresses.length);
99 logger.trace("Lutron mDNS service property DEVCLASS: {}", devclass);
100 logger.trace("Lutron mDNS service property CODEVER: {}", codever);
101 logger.trace("Lutron mDNS service property MACADDR: {}", macaddr);
103 Map<String, Object> properties = new HashMap<>();
104 String label = DEFAULT_LABEL;
106 if (ipAddresses.length < 1) {
109 if (ipAddresses.length > 1) {
110 logger.debug("Multiple addresses found for discovered Lutron device. Using only the first.");
112 properties.put(HOST, ipAddresses[0].getHostAddress());
114 String bridgeHostName = ipAddresses[0].getHostName();
115 logger.debug("Lutron mDNS bridge hostname: {}", bridgeHostName);
117 if (DEVCLASS_CASETA_SB.equals(devclass)) {
118 properties.put(PROPERTY_PRODFAM, PRODFAM_CASETA);
119 properties.put(PROPERTY_PRODTYP, PRODTYP_CASETA_SB);
120 label = PRODFAM_CASETA + " " + PRODTYP_CASETA_SB;
121 } else if (DEVCLASS_CASETA_SBP2.equals(devclass)) {
122 properties.put(PROPERTY_PRODFAM, PRODFAM_CASETA);
123 properties.put(PROPERTY_PRODTYP, PRODTYP_CASETA_SBP2);
124 label = PRODFAM_CASETA + " " + PRODTYP_CASETA_SBP2;
125 } else if (DEVCLASS_RA2_SELECT.equals(devclass)) {
126 properties.put(PROPERTY_PRODFAM, PRODFAM_RA2_SELECT);
127 properties.put(PROPERTY_PRODTYP, PRODTYP_RA2_SELECT);
128 label = PRODFAM_RA2_SELECT + " " + PRODTYP_RA2_SELECT;
129 } else if (DEVCLASS_RA3.equals(devclass)) {
130 properties.put(PROPERTY_PRODFAM, PRODFAM_RA3);
131 properties.put(PROPERTY_PRODTYP, PRODTYP_RA3);
132 label = PRODFAM_RA3 + " " + PRODTYP_RA3;
133 } else if (DEVCLASS_CONNECT_BRIDGE.equals(devclass)) {
134 logger.debug("Lutron Connect Bridge discovered. Ignoring.");
137 logger.info("Lutron device with unknown DEVCLASS discovered via mDNS: {}. Configure device manually.",
139 return null; // Exit if service has unknown DEVCLASS
142 if (!bridgeHostName.equals(ipAddresses[0].getHostAddress())) {
143 label = label + " " + bridgeHostName;
146 if (codever != null) {
147 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, codever);
150 if (macaddr != null) {
151 properties.put(Thing.PROPERTY_MAC_ADDRESS, macaddr);
154 String sn = getSerial(service);
156 logger.trace("Lutron mDNS bridge serial number: {}", sn);
157 properties.put(SERIAL_NUMBER, sn);
159 logger.debug("Unable to determine serial number of discovered Lutron bridge device.");
163 ThingUID uid = getThingUID(service);
165 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withLabel(label).withProperties(properties)
166 .withRepresentationProperty(SERIAL_NUMBER).build();
167 logger.debug("Discovered Lutron bridge device via mDNS {}", uid);
170 logger.trace("Failed to create uid for discovered Lutron bridge device");
176 public @Nullable ThingUID getThingUID(ServiceInfo service) {
177 String serial = getSerial(service);
178 String devclass = service.getPropertyString("DEVCLASS");
179 if (serial == null) {
182 if (DEVCLASS_CASETA_SB.equals(devclass)) {
183 return new ThingUID(THING_TYPE_LEAPBRIDGE, serial);
185 return new ThingUID(THING_TYPE_IPBRIDGE, serial);
191 * Returns the device serial number for the mDNS service by extracting it from the hostname.
192 * Used as unique thing representation property.
194 * @param service Lutron mDNS service
195 * @return String containing serial number, or null if it cannot be determined
197 private @Nullable String getSerial(ServiceInfo service) {
198 InetAddress[] ipAddresses = service.getInetAddresses();
199 if (ipAddresses.length < 1) {
202 Matcher matcher = HOSTNAME_REGEX.matcher(ipAddresses[0].getHostName());
203 boolean matched = matcher.find();
204 String serialnum = null;
207 serialnum = matcher.group(1);
209 if (matched && serialnum != null && !serialnum.isEmpty()) {