diff --git a/bundles/org.openhab.binding.harmonyhub/pom.xml b/bundles/org.openhab.binding.harmonyhub/pom.xml index 2a9a52858..a70253660 100644 --- a/bundles/org.openhab.binding.harmonyhub/pom.xml +++ b/bundles/org.openhab.binding.harmonyhub/pom.xml @@ -22,7 +22,7 @@ com.github.digitaldan harmony-client - 1.1.5 + 1.1.6 compile diff --git a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubBindingConstants.java b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubBindingConstants.java index d3757c29c..a1b29acbc 100644 --- a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubBindingConstants.java +++ b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/HarmonyHubBindingConstants.java @@ -42,7 +42,7 @@ public class HarmonyHubBindingConstants { public static final String DEVICE_PROPERTY_ID = "id"; public static final String DEVICE_PROPERTY_NAME = "name"; - public static final String HUB_PROPERTY_ID = "id"; + public static final String HUB_PROPERTY_ID = "uuid"; public static final String HUB_PROPERTY_HOST = "host"; public static final String HUB_PROPERTY_NAME = "name"; } diff --git a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/discovery/HarmonyHubDiscoveryService.java b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/discovery/HarmonyHubDiscoveryService.java index 4ca2663a4..a23f79d0a 100644 --- a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/discovery/HarmonyHubDiscoveryService.java +++ b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/discovery/HarmonyHubDiscoveryService.java @@ -255,10 +255,12 @@ public class HarmonyHubDiscoveryService extends AbstractDiscoveryService { String friendlyName = properties.get("friendlyName"); String hostName = properties.get("host_name"); String ip = properties.get("ip"); + String uuid = properties.get("uuid"); if (friendlyName != null && !friendlyName.isBlank() && hostName != null && !hostName.isBlank() - && ip != null && !ip.isBlank() && !responses.contains(hostName)) { + && ip != null && !ip.isBlank() && uuid != null && !uuid.isBlank() + && !responses.contains(hostName)) { responses.add(hostName); - hubDiscovered(ip, friendlyName, hostName); + hubDiscovered(ip, friendlyName, hostName, uuid); } } } catch (IOException | IndexOutOfBoundsException e) { @@ -270,7 +272,7 @@ public class HarmonyHubDiscoveryService extends AbstractDiscoveryService { } } - private void hubDiscovered(String ip, String friendlyName, String hostName) { + private void hubDiscovered(String ip, String friendlyName, String hostName, String uuid) { String thingId = hostName.replaceAll("[^A-Za-z0-9\\-_]", ""); logger.trace("Adding HarmonyHub {} ({}) at host {}", friendlyName, thingId, ip); ThingUID uid = new ThingUID(HARMONY_HUB_THING_TYPE, thingId); @@ -279,6 +281,8 @@ public class HarmonyHubDiscoveryService extends AbstractDiscoveryService { .withLabel("HarmonyHub " + friendlyName) .withProperty(HUB_PROPERTY_HOST, ip) .withProperty(HUB_PROPERTY_NAME, friendlyName) + .withProperty(HUB_PROPERTY_ID, uuid) + .withRepresentationProperty(HUB_PROPERTY_ID) .build()); // @formatter:on } diff --git a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyHubHandler.java b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyHubHandler.java index eb974d0f7..8a421e87e 100644 --- a/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyHubHandler.java +++ b/bundles/org.openhab.binding.harmonyhub/src/main/java/org/openhab/binding/harmonyhub/internal/handler/HarmonyHubHandler.java @@ -19,6 +19,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CopyOnWriteArrayList; @@ -59,6 +60,7 @@ import com.digitaldan.harmony.HarmonyClientListener; import com.digitaldan.harmony.config.Activity; import com.digitaldan.harmony.config.Activity.Status; import com.digitaldan.harmony.config.HarmonyConfig; +import com.digitaldan.harmony.config.Ping; /** * The {@link HarmonyHubHandler} is responsible for handling commands for Harmony Hubs, which are @@ -88,6 +90,7 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien private final HarmonyClient client; private @Nullable ScheduledFuture retryJob; private @Nullable ScheduledFuture heartBeatJob; + private boolean propertiesUpdated; private int heartBeatInterval; @@ -183,7 +186,7 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien config = getConfigAs(HarmonyHubConfig.class); cancelRetry(); updateStatus(ThingStatus.UNKNOWN); - retryJob = scheduler.schedule(this::connect, 0, TimeUnit.SECONDS); + scheduleRetry(0); } @Override @@ -221,12 +224,18 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien public void hubConnected() { heartBeatJob = scheduler.scheduleWithFixedDelay(() -> { try { - client.sendPing(); + Ping ping = client.sendPing().get(); + if (!propertiesUpdated) { + Map properties = editProperties(); + properties.put(HUB_PROPERTY_ID, ping.getUuid()); + updateProperties(properties); + propertiesUpdated = true; + } } catch (Exception e) { logger.debug("heartbeat failed", e); setOfflineAndReconnect("Hearbeat failed"); } - }, heartBeatInterval, heartBeatInterval, TimeUnit.SECONDS); + }, 5, heartBeatInterval, TimeUnit.SECONDS); updateStatus(ThingStatus.ONLINE); getConfigFuture().thenAcceptAsync(harmonyConfig -> updateCurrentActivityChannel(harmonyConfig), scheduler) .exceptionally(e -> { @@ -294,7 +303,7 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien private void setOfflineAndReconnect(String error) { disconnectFromHub(); - retryJob = scheduler.schedule(this::connect, RETRY_TIME, TimeUnit.SECONDS); + scheduleRetry(RETRY_TIME); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, error); } @@ -305,6 +314,11 @@ public class HarmonyHubHandler extends BaseBridgeHandler implements HarmonyClien } } + private synchronized void scheduleRetry(int retrySeconds) { + cancelRetry(); + retryJob = scheduler.schedule(this::connect, retrySeconds, TimeUnit.SECONDS); + } + private void updateState(@Nullable Activity activity) { if (activity != null) { logger.debug("Updating current activity to {}", activity.getLabel());