From b66c8fb4548acdbaf98a5e956210d072107851a1 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Wed, 12 Jun 2024 22:21:23 +0200 Subject: [PATCH 1/3] Improve Gen 1/2 discovery label Signed-off-by: Jacob Laursen --- .../internal/GatewayWebTargets.java | 2 +- .../HDPowerViewHubDiscoveryParticipant.java | 72 +++++---------- ...DPowerViewHubMDNSDiscoveryParticipant.java | 89 +++++++++++++++++++ ...PowerViewHubSddpDiscoveryParticipant.java} | 28 ++++-- .../OH-INF/i18n/hdpowerview.properties | 2 +- 5 files changed, 135 insertions(+), 58 deletions(-) create mode 100644 bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubMDNSDiscoveryParticipant.java rename bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/{HDPowerViewHubDiscoveryParticipantSddp.java => HDPowerViewHubSddpDiscoveryParticipant.java} (77%) diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/GatewayWebTargets.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/GatewayWebTargets.java index 694f4f2ab876b..49290e9de7744 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/GatewayWebTargets.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/GatewayWebTargets.java @@ -173,7 +173,7 @@ public Map getInformation() throws HubProcessingException { Thing.PROPERTY_FIRMWARE_VERSION, result.getFwVersion(), // Thing.PROPERTY_SERIAL_NUMBER, result.getSerialNumber()); } catch (JsonParseException e) { - throw new HubProcessingException("getFirmwareVersions(): JsonParseException"); + throw new HubProcessingException("getInformation(): JsonParseException"); } } diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipant.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipant.java index 10f00c69d3b3b..933c0fac022d8 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipant.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipant.java @@ -12,70 +12,46 @@ */ package org.openhab.binding.hdpowerview.internal.discovery; -import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*; - -import java.util.Set; - -import javax.jmdns.ServiceInfo; - 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.mdns.MDNSDiscoveryParticipant; -import org.openhab.core.thing.ThingTypeUID; -import org.openhab.core.thing.ThingUID; -import org.osgi.service.component.annotations.Component; +import org.eclipse.jetty.client.HttpClient; +import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets; +import org.openhab.binding.hdpowerview.internal.dto.Firmware; +import org.openhab.binding.hdpowerview.internal.dto.HubFirmware; +import org.openhab.binding.hdpowerview.internal.exceptions.HubException; +import org.openhab.core.io.net.http.HttpClientFactory; +import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Discovers HD PowerView hubs by means of mDNS + * Abstract class for common discovery logic. * - * @author Andrew Fiddian-Green - Initial contribution + * @author Jacob Laursen - Initial contribution */ @NonNullByDefault -@Component -public class HDPowerViewHubDiscoveryParticipant implements MDNSDiscoveryParticipant { +public abstract class HDPowerViewHubDiscoveryParticipant { - private static final String LABEL_KEY = "discovery.hub.label"; + protected static final String LABEL_KEY_HUB = "discovery.hub.label"; private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryParticipant.class); - @Override - public Set getSupportedThingTypeUIDs() { - return Set.of(THING_TYPE_HUB); - } - - @Override - public String getServiceType() { - return "_powerview._tcp.local."; - } + private final HttpClient httpClient; - @Override - public @Nullable DiscoveryResult createResult(ServiceInfo service) { - for (String host : service.getHostAddresses()) { - if (VALID_IP_V4_ADDRESS.matcher(host).matches()) { - ThingUID thingUID = new ThingUID(THING_TYPE_HUB, host.replace('.', '_')); - DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID) - .withProperty(HDPowerViewHubConfiguration.HOST, host) - .withRepresentationProperty(HDPowerViewHubConfiguration.HOST) - .withLabel(String.format("@text/%s [\"%s\"]", LABEL_KEY, host)).build(); - logger.debug("mDNS discovered Gen 1/2 hub on host '{}'", host); - return hub; - } - } - return null; + protected HDPowerViewHubDiscoveryParticipant(@Reference HttpClientFactory httpClientFactory) { + httpClient = httpClientFactory.getCommonHttpClient(); } - @Override - public @Nullable ThingUID getThingUID(ServiceInfo service) { - for (String host : service.getHostAddresses()) { - if (VALID_IP_V4_ADDRESS.matcher(host).matches()) { - return new ThingUID(THING_TYPE_HUB, host.replace('.', '_')); + protected String getGeneration(String host) { + var webTargets = new HDPowerViewWebTargets(httpClient, host); + try { + HubFirmware firmware = webTargets.getFirmwareVersions(); + Firmware mainProcessor = firmware.mainProcessor; + if (mainProcessor != null) { + return String.valueOf(mainProcessor.revision); } + } catch (HubException e) { + logger.debug("Failed to discover hub firmware versions", e); } - return null; + return "1/2"; } } diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubMDNSDiscoveryParticipant.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubMDNSDiscoveryParticipant.java new file mode 100644 index 0000000000000..bb382a2e77dab --- /dev/null +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubMDNSDiscoveryParticipant.java @@ -0,0 +1,89 @@ +/** + * 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 javax.jmdns.ServiceInfo; + +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.mdns.MDNSDiscoveryParticipant; +import org.openhab.core.io.net.http.HttpClientFactory; +import org.openhab.core.thing.ThingTypeUID; +import org.openhab.core.thing.ThingUID; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Discovers HD PowerView hubs by means of mDNS + * + * @author Andrew Fiddian-Green - Initial contribution + */ +@NonNullByDefault +@Component +public class HDPowerViewHubMDNSDiscoveryParticipant extends HDPowerViewHubDiscoveryParticipant + implements MDNSDiscoveryParticipant { + + private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubMDNSDiscoveryParticipant.class); + + @Activate + public HDPowerViewHubMDNSDiscoveryParticipant(@Reference HttpClientFactory httpClientFactory) { + super(httpClientFactory); + } + + @Override + public Set getSupportedThingTypeUIDs() { + return Set.of(THING_TYPE_HUB); + } + + @Override + public String getServiceType() { + return "_powerview._tcp.local."; + } + + @Override + public @Nullable DiscoveryResult createResult(ServiceInfo service) { + for (String host : service.getHostAddresses()) { + if (VALID_IP_V4_ADDRESS.matcher(host).matches()) { + ThingUID thingUID = new ThingUID(THING_TYPE_HUB, host.replace('.', '_')); + String generation = this.getGeneration(host); + DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID) + .withProperty(HDPowerViewHubConfiguration.HOST, host) + .withRepresentationProperty(HDPowerViewHubConfiguration.HOST) + .withLabel(String.format("@text/%s [\"%s\", \"%s\"]", LABEL_KEY_HUB, generation, host)).build(); + logger.debug("mDNS discovered Gen {} hub on host '{}'", generation, host); + return hub; + } + } + return null; + } + + @Override + public @Nullable ThingUID getThingUID(ServiceInfo service) { + for (String host : service.getHostAddresses()) { + if (VALID_IP_V4_ADDRESS.matcher(host).matches()) { + return new ThingUID(THING_TYPE_HUB, host.replace('.', '_')); + } + } + return null; + } +} 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/HDPowerViewHubSddpDiscoveryParticipant.java similarity index 77% rename from bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipantSddp.java rename to bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubSddpDiscoveryParticipant.java index fd4e8d6a23af3..eefd596c92ce4 100644 --- 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/HDPowerViewHubSddpDiscoveryParticipant.java @@ -23,9 +23,12 @@ 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.io.net.http.HttpClientFactory; import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ThingUID; +import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,16 +39,21 @@ */ @NonNullByDefault @Component -public class HDPowerViewHubDiscoveryParticipantSddp implements SddpDiscoveryParticipant { +public class HDPowerViewHubSddpDiscoveryParticipant extends HDPowerViewHubDiscoveryParticipant + 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); + private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubSddpDiscoveryParticipant.class); + + @Activate + public HDPowerViewHubSddpDiscoveryParticipant(@Reference HttpClientFactory httpClientFactory) { + super(httpClientFactory); + } @Override public Set getSupportedThingTypeUIDs() { @@ -57,13 +65,17 @@ public Set getSupportedThingTypeUIDs() { final ThingUID thingUID = getThingUID(device); if (thingUID != null) { try { + boolean isGateway = isGateway(device); + String generation = isGateway ? "3" : getGeneration(device.ipAddress); + String label = isGateway // + ? String.format("@text/%s [\"%s\"]", LABEL_KEY_GATEWAY, device.ipAddress) + : String.format("@text/%s [\"%s\", \"%s\"]", LABEL_KEY_HUB, device.ipAddress, generation); + 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); + .withRepresentationProperty(HDPowerViewHubConfiguration.HOST).withLabel(label).build(); + logger.debug("SDDP discovered Gen {} hub/gateway '{}' on host '{}'", generation, thingUID, + device.ipAddress); return hub; } catch (IllegalArgumentException e) { // error already logged, so fall through diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties index 8785d2082b3fe..521f7a8379032 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties +++ b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/i18n/hdpowerview.properties @@ -86,7 +86,7 @@ offline.gone.shade-unknown-to-hub = Shade is unknown to Hub/Gateway # discovery -discovery.hub.label = PowerView Gen 1/2 Hub ({0}) +discovery.hub.label = PowerView Gen {0} Hub ({1}) discovery.gateway.label = PowerView Gen 3 Gateway ({0}) # dynamic channels From e8ddd15e68ac4fcc379dd0a9a3ccb4b47dcdb3f5 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Thu, 13 Jun 2024 23:19:32 +0200 Subject: [PATCH 2/3] Recognize Gen 2 Hub directly from SDDP metadata Signed-off-by: Jacob Laursen --- .../HDPowerViewHubDiscoveryParticipant.java | 57 ------------------- ...DPowerViewHubMDNSDiscoveryParticipant.java | 27 ++++++++- ... HDPowerViewSddpDiscoveryParticipant.java} | 20 ++----- 3 files changed, 30 insertions(+), 74 deletions(-) delete mode 100644 bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipant.java rename bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/{HDPowerViewHubSddpDiscoveryParticipant.java => HDPowerViewSddpDiscoveryParticipant.java} (85%) diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipant.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipant.java deleted file mode 100644 index 933c0fac022d8..0000000000000 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubDiscoveryParticipant.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * 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 org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jetty.client.HttpClient; -import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets; -import org.openhab.binding.hdpowerview.internal.dto.Firmware; -import org.openhab.binding.hdpowerview.internal.dto.HubFirmware; -import org.openhab.binding.hdpowerview.internal.exceptions.HubException; -import org.openhab.core.io.net.http.HttpClientFactory; -import org.osgi.service.component.annotations.Reference; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Abstract class for common discovery logic. - * - * @author Jacob Laursen - Initial contribution - */ -@NonNullByDefault -public abstract class HDPowerViewHubDiscoveryParticipant { - - protected static final String LABEL_KEY_HUB = "discovery.hub.label"; - - private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryParticipant.class); - - private final HttpClient httpClient; - - protected HDPowerViewHubDiscoveryParticipant(@Reference HttpClientFactory httpClientFactory) { - httpClient = httpClientFactory.getCommonHttpClient(); - } - - protected String getGeneration(String host) { - var webTargets = new HDPowerViewWebTargets(httpClient, host); - try { - HubFirmware firmware = webTargets.getFirmwareVersions(); - Firmware mainProcessor = firmware.mainProcessor; - if (mainProcessor != null) { - return String.valueOf(mainProcessor.revision); - } - } catch (HubException e) { - logger.debug("Failed to discover hub firmware versions", e); - } - return "1/2"; - } -} diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubMDNSDiscoveryParticipant.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubMDNSDiscoveryParticipant.java index bb382a2e77dab..d45817395765a 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubMDNSDiscoveryParticipant.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubMDNSDiscoveryParticipant.java @@ -20,7 +20,12 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.jetty.client.HttpClient; +import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets; import org.openhab.binding.hdpowerview.internal.config.HDPowerViewHubConfiguration; +import org.openhab.binding.hdpowerview.internal.dto.Firmware; +import org.openhab.binding.hdpowerview.internal.dto.HubFirmware; +import org.openhab.binding.hdpowerview.internal.exceptions.HubException; import org.openhab.core.config.discovery.DiscoveryResult; import org.openhab.core.config.discovery.DiscoveryResultBuilder; import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant; @@ -40,14 +45,16 @@ */ @NonNullByDefault @Component -public class HDPowerViewHubMDNSDiscoveryParticipant extends HDPowerViewHubDiscoveryParticipant - implements MDNSDiscoveryParticipant { +public class HDPowerViewHubMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant { + + public static final String LABEL_KEY_HUB = "discovery.hub.label"; private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubMDNSDiscoveryParticipant.class); + private final HttpClient httpClient; @Activate public HDPowerViewHubMDNSDiscoveryParticipant(@Reference HttpClientFactory httpClientFactory) { - super(httpClientFactory); + httpClient = httpClientFactory.getCommonHttpClient(); } @Override @@ -86,4 +93,18 @@ public String getServiceType() { } return null; } + + private String getGeneration(String host) { + var webTargets = new HDPowerViewWebTargets(httpClient, host); + try { + HubFirmware firmware = webTargets.getFirmwareVersions(); + Firmware mainProcessor = firmware.mainProcessor; + if (mainProcessor != null) { + return String.valueOf(mainProcessor.revision); + } + } catch (HubException e) { + logger.debug("Failed to discover hub firmware versions", e); + } + return "1/2"; + } } diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubSddpDiscoveryParticipant.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewSddpDiscoveryParticipant.java similarity index 85% rename from bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubSddpDiscoveryParticipant.java rename to bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewSddpDiscoveryParticipant.java index eefd596c92ce4..db3454a7d73c7 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewHubSddpDiscoveryParticipant.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewSddpDiscoveryParticipant.java @@ -23,24 +23,20 @@ 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.io.net.http.HttpClientFactory; import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ThingUID; -import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Discovers HD PowerView hubs by means of SDDP + * Discovers HD PowerView hubs/gateways by means of SDDP * * @author Andrew Fiddian-Green - Initial contribution */ @NonNullByDefault @Component -public class HDPowerViewHubSddpDiscoveryParticipant extends HDPowerViewHubDiscoveryParticipant - implements SddpDiscoveryParticipant { +public class HDPowerViewSddpDiscoveryParticipant implements SddpDiscoveryParticipant { private static final String LABEL_KEY_GATEWAY = "discovery.gateway.label"; @@ -48,12 +44,7 @@ public class HDPowerViewHubSddpDiscoveryParticipant extends HDPowerViewHubDiscov 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(HDPowerViewHubSddpDiscoveryParticipant.class); - - @Activate - public HDPowerViewHubSddpDiscoveryParticipant(@Reference HttpClientFactory httpClientFactory) { - super(httpClientFactory); - } + private final Logger logger = LoggerFactory.getLogger(HDPowerViewSddpDiscoveryParticipant.class); @Override public Set getSupportedThingTypeUIDs() { @@ -66,10 +57,11 @@ public Set getSupportedThingTypeUIDs() { if (thingUID != null) { try { boolean isGateway = isGateway(device); - String generation = isGateway ? "3" : getGeneration(device.ipAddress); + String generation = isGateway ? "3" : device.type.endsWith("v2") ? "2" : "1"; String label = isGateway // ? String.format("@text/%s [\"%s\"]", LABEL_KEY_GATEWAY, device.ipAddress) - : String.format("@text/%s [\"%s\", \"%s\"]", LABEL_KEY_HUB, device.ipAddress, generation); + : String.format("@text/%s [\"%s\", \"%s\"]", + HDPowerViewHubMDNSDiscoveryParticipant.LABEL_KEY_HUB, device.ipAddress, generation); DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID) .withProperty(HDPowerViewHubConfiguration.HOST, device.ipAddress) From 27e20045426a62a67d01a0701236fc92b81cf89d Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Thu, 13 Jun 2024 23:32:34 +0200 Subject: [PATCH 3/3] Merge gateway/generation checks Signed-off-by: Jacob Laursen --- .../HDPowerViewSddpDiscoveryParticipant.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewSddpDiscoveryParticipant.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewSddpDiscoveryParticipant.java index db3454a7d73c7..d0f484d41c1b1 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewSddpDiscoveryParticipant.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/discovery/HDPowerViewSddpDiscoveryParticipant.java @@ -56,9 +56,8 @@ public Set getSupportedThingTypeUIDs() { final ThingUID thingUID = getThingUID(device); if (thingUID != null) { try { - boolean isGateway = isGateway(device); - String generation = isGateway ? "3" : device.type.endsWith("v2") ? "2" : "1"; - String label = isGateway // + int generation = getGeneration(device); + String label = generation == 3 // ? String.format("@text/%s [\"%s\"]", LABEL_KEY_GATEWAY, device.ipAddress) : String.format("@text/%s [\"%s\", \"%s\"]", HDPowerViewHubMDNSDiscoveryParticipant.LABEL_KEY_HUB, device.ipAddress, generation); @@ -81,7 +80,7 @@ public Set getSupportedThingTypeUIDs() { 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, + return new ThingUID(getGeneration(device) == 3 ? THING_TYPE_GATEWAY : THING_TYPE_HUB, device.ipAddress.replace('.', '_')); } } catch (IllegalArgumentException e) { @@ -94,15 +93,15 @@ public Set getSupportedThingTypeUIDs() { /** * 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. + * @return 3 if a Gen 3 gateway, 2 if Gen 2 hub or 1 if Gen 1 hub. * @throws IllegalArgumentException if neither Gen 3, 2 or 1. */ - private boolean isGateway(SddpDevice device) throws IllegalArgumentException { + private int getGeneration(SddpDevice device) throws IllegalArgumentException { if (device.type.contains(POWERVIEW_GEN3_ID)) { - return true; + return 3; } if (device.type.contains(POWERVIEW_HUB_ID)) { - return false; + return device.type.endsWith("v2") ? 2 : 1; } final IllegalArgumentException e = new IllegalArgumentException("Device has unexpected 'type' property"); logger.debug("{}", e.getMessage());