2 * Copyright (c) 2010-2024 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.hdpowerview.internal.discovery;
15 import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewHubConfiguration;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.config.discovery.sddp.SddpDevice;
25 import org.openhab.core.config.discovery.sddp.SddpDiscoveryParticipant;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Component;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Discovers HD PowerView hubs by means of SDDP
35 * @author Andrew Fiddian-Green - Initial contribution
39 public class HDPowerViewHubDiscoveryParticipantSddp implements SddpDiscoveryParticipant {
41 private static final String LABEL_KEY_HUB = "discovery.hub.label";
42 private static final String LABEL_KEY_GATEWAY = "discovery.gateway.label";
44 private static final String HUNTER_DOUGLAS = "hunterdouglas:";
45 private static final String POWERVIEW_HUB_ID = "hub:powerview";
46 private static final String POWERVIEW_GEN3_ID = "powerview:gen3:gateway";
48 private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryParticipantSddp.class);
51 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
52 return Set.of(THING_TYPE_HUB, THING_TYPE_GATEWAY);
56 public @Nullable DiscoveryResult createResult(SddpDevice device) {
57 final ThingUID thingUID = getThingUID(device);
58 if (thingUID != null) {
60 DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
61 .withProperty(HDPowerViewHubConfiguration.HOST, device.ipAddress)
62 .withRepresentationProperty(HDPowerViewHubConfiguration.HOST)
63 .withLabel(String.format("@text/%s [\"%s\"]",
64 isGateway(device) ? LABEL_KEY_GATEWAY : LABEL_KEY_HUB, device.ipAddress))
66 logger.debug("SDDP discovered hub/gateway '{}' on host '{}'", thingUID, device.ipAddress);
68 } catch (IllegalArgumentException e) {
69 // error already logged, so fall through
76 public @Nullable ThingUID getThingUID(SddpDevice device) {
77 if (device.type.startsWith(HUNTER_DOUGLAS)) {
79 if (VALID_IP_V4_ADDRESS.matcher(device.ipAddress).matches()) {
80 return new ThingUID(isGateway(device) ? THING_TYPE_GATEWAY : THING_TYPE_HUB,
81 device.ipAddress.replace('.', '_'));
83 } catch (IllegalArgumentException e) {
84 // error already logged, so fall through
91 * Check if the device 'type' property represents a Gen 3 gateway or a Gen 1/2 hub.
93 * @return true if a Gen 3 gateway or false if a Gen 1/2 hub.
94 * @throws IllegalArgumentException if neither Gen 3, 2 or 1.
96 private boolean isGateway(SddpDevice device) throws IllegalArgumentException {
97 if (device.type.contains(POWERVIEW_GEN3_ID)) {
100 if (device.type.contains(POWERVIEW_HUB_ID)) {
103 final IllegalArgumentException e = new IllegalArgumentException("Device has unexpected 'type' property");
104 logger.debug("{}", e.getMessage());