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.nanoleaf.internal.discovery;
15 import static org.openhab.binding.nanoleaf.internal.NanoleafBindingConstants.*;
17 import java.util.HashMap;
21 import javax.jmdns.ServiceInfo;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.nanoleaf.internal.NanoleafHandlerFactory;
26 import org.openhab.binding.nanoleaf.internal.OpenAPIUtils;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.osgi.service.component.annotations.Component;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link NanoleafMDNSDiscoveryParticipant} is responsible for discovering new Nanoleaf controllers (bridges).
40 * @author Martin Raepple - Initial contribution
41 * @author Stefan Höhn - further improvements for static defined things
42 * @see <a href="https://openhab.org/documentation/development/bindings/discovery-services.html">MSDN
45 @Component(configurationPid = "discovery.nanoleaf")
47 public class NanoleafMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
49 private final Logger logger = LoggerFactory.getLogger(NanoleafMDNSDiscoveryParticipant.class);
52 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53 return NanoleafHandlerFactory.SUPPORTED_THING_TYPES_UIDS;
57 public String getServiceType() {
62 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
63 final ThingUID uid = getThingUID(service);
67 final Map<String, Object> properties = new HashMap<>(2);
68 String host = service.getHostAddresses()[0];
69 properties.put(CONFIG_ADDRESS, host);
70 int port = service.getPort();
71 properties.put(CONFIG_PORT, port);
72 String firmwareVersion = service.getPropertyString("srcvers");
73 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareVersion);
74 String modelId = service.getPropertyString("md");
75 properties.put(Thing.PROPERTY_MODEL_ID, modelId);
76 properties.put(Thing.PROPERTY_VENDOR, "Nanoleaf");
77 String qualifiedName = service.getQualifiedName();
78 logger.debug("Device found: {}", qualifiedName);
80 logger.trace("Discovered nanoleaf host: {} port: {} firmWare: {} modelId: {} qualifiedName: {}", host, port,
81 firmwareVersion, modelId, qualifiedName);
82 logger.debug("Adding Nanoleaf controller {} with FW version {} found at {}:{} to inbox", qualifiedName,
83 firmwareVersion, host, port);
84 if (!OpenAPIUtils.checkRequiredFirmware(service.getPropertyString("md"), firmwareVersion)) {
85 logger.debug("Nanoleaf controller firmware is too old. Must be {} or higher",
86 MODEL_ID_LIGHTPANELS.equals(modelId) ? API_MIN_FW_VER_LIGHTPANELS : API_MIN_FW_VER_CANVAS);
89 final DiscoveryResult result = DiscoveryResultBuilder.create(uid).withThingType(getThingType(service))
90 .withProperties(properties).withLabel(service.getName()).withRepresentationProperty(CONFIG_ADDRESS)
96 public @Nullable ThingUID getThingUID(ServiceInfo service) {
97 ThingTypeUID thingTypeUID = getThingType(service);
98 if (thingTypeUID != null) {
99 String id = service.getPropertyString("id").replace(":", "");
100 return new ThingUID(thingTypeUID, id);
106 private @Nullable ThingTypeUID getThingType(final ServiceInfo service) {
107 String model = service.getPropertyString("md"); // model
108 logger.debug("Nanoleaf Type: {}", model);
112 return THING_TYPE_CONTROLLER;