]> git.basschouten.com Git - openhab-addons.git/blob
182a3a4f8d0b0f2313ea7d62ea03aa3291bc5ddb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lutron.internal.discovery;
14
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.*;
16
17 import java.net.InetAddress;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import javax.jmdns.ServiceInfo;
25
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;
37
38 /**
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.
41  *
42  * @author Bob Adair - Initial contribution
43  */
44 @Component
45 @NonNullByDefault
46 public class LutronMdnsBridgeDiscoveryService implements MDNSDiscoveryParticipant {
47
48     // Lutron mDNS service <app>.<protocol>.<servicedomain>
49     private static final String LUTRON_MDNS_SERVICE_TYPE = "_lutron._tcp.local.";
50
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";
56
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";
60
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";
64
65     private static final String DEVCLASS_CONNECT_BRIDGE = "08090301";
66     private static final String DEFAULT_LABEL = "Unknown Lutron bridge";
67
68     private static final Pattern HOSTNAME_REGEX = Pattern.compile("lutron-([0-9a-f]+)\\."); // ex: lutron-01f1529a.local
69
70     private final Logger logger = LoggerFactory.getLogger(LutronMdnsBridgeDiscoveryService.class);
71
72     @Override
73     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
74         return Set.of(THING_TYPE_IPBRIDGE);
75     }
76
77     @Override
78     public String getServiceType() {
79         return LUTRON_MDNS_SERVICE_TYPE;
80     }
81
82     @Override
83     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
84         if (!service.hasData()) {
85             return null;
86         }
87
88         String nice = service.getNiceTextString();
89         String qualifiedName = service.getQualifiedName();
90
91         InetAddress[] ipAddresses = service.getInetAddresses();
92         String devclass = service.getPropertyString("DEVCLASS");
93         String codever = service.getPropertyString("CODEVER");
94         String macaddr = service.getPropertyString("MACADDR");
95
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);
102
103         Map<String, Object> properties = new HashMap<>();
104         String label = DEFAULT_LABEL;
105
106         if (ipAddresses.length < 1) {
107             return null;
108         }
109         if (ipAddresses.length > 1) {
110             logger.debug("Multiple addresses found for discovered Lutron device. Using only the first.");
111         }
112         properties.put(HOST, ipAddresses[0].getHostAddress());
113
114         String bridgeHostName = ipAddresses[0].getHostName();
115         logger.debug("Lutron mDNS bridge hostname: {}", bridgeHostName);
116
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.");
135             return null;
136         } else {
137             logger.info("Lutron device with unknown DEVCLASS discovered via mDNS: {}. Configure device manually.",
138                     devclass);
139             return null; // Exit if service has unknown DEVCLASS
140         }
141
142         if (!bridgeHostName.equals(ipAddresses[0].getHostAddress())) {
143             label = label + " " + bridgeHostName;
144         }
145
146         if (codever != null) {
147             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, codever);
148         }
149
150         if (macaddr != null) {
151             properties.put(Thing.PROPERTY_MAC_ADDRESS, macaddr);
152         }
153
154         String sn = getSerial(service);
155         if (sn != null) {
156             logger.trace("Lutron mDNS bridge serial number: {}", sn);
157             properties.put(SERIAL_NUMBER, sn);
158         } else {
159             logger.debug("Unable to determine serial number of discovered Lutron bridge device.");
160             return null;
161         }
162
163         ThingUID uid = getThingUID(service);
164         if (uid != null) {
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);
168             return result;
169         } else {
170             logger.trace("Failed to create uid for discovered Lutron bridge device");
171             return null;
172         }
173     }
174
175     @Override
176     public @Nullable ThingUID getThingUID(ServiceInfo service) {
177         String serial = getSerial(service);
178         String devclass = service.getPropertyString("DEVCLASS");
179         if (serial == null) {
180             return null;
181         } else {
182             if (DEVCLASS_CASETA_SB.equals(devclass)) {
183                 return new ThingUID(THING_TYPE_LEAPBRIDGE, serial);
184             } else {
185                 return new ThingUID(THING_TYPE_IPBRIDGE, serial);
186             }
187         }
188     }
189
190     /**
191      * Returns the device serial number for the mDNS service by extracting it from the hostname.
192      * Used as unique thing representation property.
193      *
194      * @param service Lutron mDNS service
195      * @return String containing serial number, or null if it cannot be determined
196      */
197     private @Nullable String getSerial(ServiceInfo service) {
198         InetAddress[] ipAddresses = service.getInetAddresses();
199         if (ipAddresses.length < 1) {
200             return null;
201         }
202         Matcher matcher = HOSTNAME_REGEX.matcher(ipAddresses[0].getHostName());
203         boolean matched = matcher.find();
204         String serialnum = null;
205
206         if (matched) {
207             serialnum = matcher.group(1);
208         }
209         if (matched && serialnum != null && !serialnum.isEmpty()) {
210             return serialnum;
211         } else {
212             return null;
213         }
214     }
215 }