]> git.basschouten.com Git - openhab-addons.git/commitdiff
[hue] Implementing "[discovery.upnp] Devices may apply a grace period" (#9985)
authorAndrew Fiddian-Green <software@whitebear.ch>
Tue, 9 Feb 2021 20:52:11 +0000 (20:52 +0000)
committerGitHub <noreply@github.com>
Tue, 9 Feb 2021 20:52:11 +0000 (12:52 -0800)
* [hue] implement PR #2144 in openhab.core
* [hue] add binding configuration parameter; return long

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
bundles/org.openhab.binding.hue/README.md
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/HueBindingConstants.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/discovery/HueBridgeDiscoveryParticipant.java
bundles/org.openhab.binding.hue/src/main/resources/OH-INF/binding/binding.xml

index 9c2d07e512beb7ab094c5ea4a3ee25baaa5a25f3..f809c21f4f3a433c0f997b42cdffc88fe08594c3 100644 (file)
@@ -367,3 +367,11 @@ if (receivedEvent == "1000.0")) {
     //do stuff
 }       
 ```
+
+### UPnP Discovery: Inbox 'Grace Period'
+
+The Hue Bridge can sometimes be late in sending its UPnP 'ssdp:alive' notifications even though it has not really gone offline.
+This means that the Hue Bridge could be repeatedly removed from, and (re)added to, the InBox.
+Which would lead to confusion in the UI, and repeated logger messages.
+To prevent this, the binding tells the OpenHAB core to wait for a further period of time ('grace period') before actually removing the Bridge from the Inbox.
+The 'grace period' has a default value of 50 seconds, but it can be fine tuned in the main UI via Settings | Bindings | Hue | Configure.
index 8f38f52b9ee291b8caf59eb6fb58d4d1dfb3ca32..90949968cb4ed5a95d5819f6380001b92d293514 100644 (file)
@@ -84,6 +84,9 @@ public class HueBindingConstants {
     public static final String EVENT_DIMMER_SWITCH = "dimmer_switch_event";
     public static final String EVENT_TAP_SWITCH = "tap_switch_event";
 
+    // Binding configuration properties
+    public static final String REMOVAL_GRACE_PERIOD = "removalGracePeriod";
+
     // Bridge config properties
     public static final String HOST = "ipAddress";
     public static final String PORT = "port";
index 278b717ac71867d751054c0c992930cb48798fd1..d5bb4ce498354452b3be9e09dcc47ea86c979ec7 100644 (file)
@@ -15,7 +15,9 @@ package org.openhab.binding.hue.internal.discovery;
 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
 import static org.openhab.core.thing.Thing.PROPERTY_SERIAL_NUMBER;
 
+import java.io.IOException;
 import java.util.Collections;
+import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
@@ -25,13 +27,20 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.jupnp.model.meta.DeviceDetails;
 import org.jupnp.model.meta.ModelDetails;
 import org.jupnp.model.meta.RemoteDevice;
+import org.openhab.binding.hue.internal.HueBindingConstants;
 import org.openhab.core.config.discovery.DiscoveryResult;
 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
 import org.openhab.core.config.discovery.upnp.internal.UpnpDiscoveryService;
 import org.openhab.core.thing.ThingTypeUID;
 import org.openhab.core.thing.ThingUID;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+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;
 
 /**
  * The {@link HueBridgeDiscoveryParticipant} is responsible for discovering new and
@@ -44,6 +53,18 @@ import org.osgi.service.component.annotations.Component;
 @Component(service = UpnpDiscoveryParticipant.class)
 public class HueBridgeDiscoveryParticipant implements UpnpDiscoveryParticipant {
 
+    private final Logger logger = LoggerFactory.getLogger(HueBridgeDiscoveryParticipant.class);
+
+    // Hue bridges have maxAge 100 seconds, so set the default grace period to half of that
+    private long removalGracePeriodSeconds = 50;
+
+    private final ConfigurationAdmin configAdmin;
+
+    @Activate
+    public HueBridgeDiscoveryParticipant(final @Reference ConfigurationAdmin configAdmin) {
+        this.configAdmin = configAdmin;
+    }
+
     @Override
     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
         return Collections.singleton(THING_TYPE_BRIDGE);
@@ -92,4 +113,20 @@ public class HueBridgeDiscoveryParticipant implements UpnpDiscoveryParticipant {
         }
         return null;
     }
+
+    @Override
+    public long getRemovalGracePeriodSeconds(RemoteDevice device) {
+        try {
+            Configuration conf = configAdmin.getConfiguration("binding.hue");
+            Dictionary<String, @Nullable Object> properties = conf.getProperties();
+            Object property = properties.get(HueBindingConstants.REMOVAL_GRACE_PERIOD);
+            if (property != null) {
+                removalGracePeriodSeconds = Long.parseLong(property.toString());
+            }
+        } catch (IOException | IllegalStateException | NumberFormatException e) {
+            // fall through to pre-initialised (default) value
+        }
+        logger.trace("getRemovalGracePeriodSeconds={}", removalGracePeriodSeconds);
+        return removalGracePeriodSeconds;
+    }
 }
index 90daae7d404269413aa3d9e1ee6a054a93142688..87ae19a6aa0b83eea25a3a2bccc2d9c517c593da 100644 (file)
@@ -3,8 +3,16 @@
        xmlns:binding="https://openhab.org/schemas/binding/v1.0.0"
        xsi:schemaLocation="https://openhab.org/schemas/binding/v1.0.0 https://openhab.org/schemas/binding-1.0.0.xsd">
 
-       <name>hue Binding</name>
-       <description>The hue Binding integrates the Philips hue system. It
-               allows to control hue bulbs.</description>
+       <name>Hue Binding</name>
+       <description>The Hue Binding integrates the Philips Hue system. It allows to control Hue bulbs.</description>
+
+       <config-description>
+               <parameter name="removalGracePeriod" type="integer" min="0" step="1" unit="s">
+                       <label>Removal Grace Period</label>
+                       <description>Extra grace period (seconds) that UPnP discovery shall wait before removing a lost Bridge from the
+                               Inbox. Default is 50 seconds.</description>
+                       <default>50</default>
+               </parameter>
+       </config-description>
 
 </binding:binding>