]> git.basschouten.com Git - openhab-addons.git/commitdiff
[hdpowerview] Improvements to SDDP discovery (#16853)
authorAndrew Fiddian-Green <software@whitebear.ch>
Sun, 9 Jun 2024 08:39:50 +0000 (09:39 +0100)
committerGitHub <noreply@github.com>
Sun, 9 Jun 2024 08:39:50 +0000 (10:39 +0200)
* [hdpowerview] add SDDP thing discovery

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
bundles/org.openhab.binding.hdpowerview/src/main/feature/feature.xml
bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipantSddp.java [new file with mode: 0644]
bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/addon/addon.xml

index 71ed90ed0ecf44e2274f4e539454a6dac84b3d7b..bef52ebdade80ff9170e626f27f9f8cb42c3c7aa 100644 (file)
@@ -4,6 +4,7 @@
 
        <feature name="openhab-binding-hdpowerview" description="HD PowerView Binding" version="${project.version}">
                <feature>openhab-runtime-base</feature>
+               <feature>openhab-core-config-discovery-sddp</feature>
                <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.hdpowerview/${project.version}</bundle>
        </feature>
 </features>
diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipantSddp.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipantSddp.java
new file mode 100644 (file)
index 0000000..fd4e8d6
--- /dev/null
@@ -0,0 +1,107 @@
+/**
+ * Copyright (c) 2010-2024 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.hdpowerview.internal.discovery;
+
+import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*;
+
+import java.util.Set;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.openhab.binding.hdpowerview.internal.config.HDPowerViewHubConfiguration;
+import org.openhab.core.config.discovery.DiscoveryResult;
+import org.openhab.core.config.discovery.DiscoveryResultBuilder;
+import org.openhab.core.config.discovery.sddp.SddpDevice;
+import org.openhab.core.config.discovery.sddp.SddpDiscoveryParticipant;
+import org.openhab.core.thing.ThingTypeUID;
+import org.openhab.core.thing.ThingUID;
+import org.osgi.service.component.annotations.Component;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Discovers HD PowerView hubs by means of SDDP
+ *
+ * @author Andrew Fiddian-Green - Initial contribution
+ */
+@NonNullByDefault
+@Component
+public class HDPowerViewHubDiscoveryParticipantSddp implements SddpDiscoveryParticipant {
+
+    private static final String LABEL_KEY_HUB = "discovery.hub.label";
+    private static final String LABEL_KEY_GATEWAY = "discovery.gateway.label";
+
+    private static final String HUNTER_DOUGLAS = "hunterdouglas:";
+    private static final String POWERVIEW_HUB_ID = "hub:powerview";
+    private static final String POWERVIEW_GEN3_ID = "powerview:gen3:gateway";
+
+    private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryParticipantSddp.class);
+
+    @Override
+    public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
+        return Set.of(THING_TYPE_HUB, THING_TYPE_GATEWAY);
+    }
+
+    @Override
+    public @Nullable DiscoveryResult createResult(SddpDevice device) {
+        final ThingUID thingUID = getThingUID(device);
+        if (thingUID != null) {
+            try {
+                DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
+                        .withProperty(HDPowerViewHubConfiguration.HOST, device.ipAddress)
+                        .withRepresentationProperty(HDPowerViewHubConfiguration.HOST)
+                        .withLabel(String.format("@text/%s [\"%s\"]",
+                                isGateway(device) ? LABEL_KEY_GATEWAY : LABEL_KEY_HUB, device.ipAddress))
+                        .build();
+                logger.debug("SDDP discovered hub/gateway '{}' on host '{}'", thingUID, device.ipAddress);
+                return hub;
+            } catch (IllegalArgumentException e) {
+                // error already logged, so fall through
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public @Nullable ThingUID getThingUID(SddpDevice device) {
+        if (device.type.startsWith(HUNTER_DOUGLAS)) {
+            try {
+                if (VALID_IP_V4_ADDRESS.matcher(device.ipAddress).matches()) {
+                    return new ThingUID(isGateway(device) ? THING_TYPE_GATEWAY : THING_TYPE_HUB,
+                            device.ipAddress.replace('.', '_'));
+                }
+            } catch (IllegalArgumentException e) {
+                // error already logged, so fall through
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Check if the device 'type' property represents a Gen 3 gateway or a Gen 1/2 hub.
+     *
+     * @return true if a Gen 3 gateway or false if a Gen 1/2 hub.
+     * @throws IllegalArgumentException if neither Gen 3, 2 or 1.
+     */
+    private boolean isGateway(SddpDevice device) throws IllegalArgumentException {
+        if (device.type.contains(POWERVIEW_GEN3_ID)) {
+            return true;
+        }
+        if (device.type.contains(POWERVIEW_HUB_ID)) {
+            return false;
+        }
+        final IllegalArgumentException e = new IllegalArgumentException("Device has unexpected 'type' property");
+        logger.debug("{}", e.getMessage());
+        throw e;
+    }
+}
index 26fa974af64b2471b3517896f57ed51fbf265476..44125c0ad2dea7c4d914fcfcc60c101d55bd21de 100644 (file)
@@ -32,7 +32,7 @@
                        <match-properties>
                                <match-property>
                                        <name>type</name>
-                                       <regex>hunterdouglas:hub:powerview.*</regex>
+                                       <regex>hunterdouglas.*</regex>
                                </match-property>
                        </match-properties>
                </discovery-method>