]> git.basschouten.com Git - openhab-addons.git/commitdiff
[pilight] Add option to disable background discovery for a given pilight bridge thing...
authorStefan Roellin <stefanroellin@users.noreply.github.com>
Sun, 10 Dec 2023 11:14:59 +0000 (12:14 +0100)
committerGitHub <noreply@github.com>
Sun, 10 Dec 2023 11:14:59 +0000 (12:14 +0100)
* [pilight] Add option to disable background discovery for a given pilight bridge thing

Previously the background discovery updated periodically all channels of all
devices regardless whether they have sent an update or not.  This behavior
makes it impossible to decide whether a device is still alive by observing
channel updates sent by a device itself.  Especially for devices running on
battery, it is important to know, if it still sends updates.

Create ExecutorService during initialize

---------

Signed-off-by: Stefan Roellin <stefan@roellin-baumann.ch>
bundles/org.openhab.binding.pilight/README.md
bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/PilightBridgeConfiguration.java
bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/discovery/PilightDeviceDiscoveryService.java
bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/handler/PilightBridgeHandler.java
bundles/org.openhab.binding.pilight/src/main/resources/OH-INF/i18n/pilight.properties
bundles/org.openhab.binding.pilight/src/main/resources/OH-INF/thing/bridge.xml

index 7be4827d1eb1e3dcf2c35e6dfc0f8e8092718027..eca1d06320860514bc0858421ea1cf6dbf50379b 100644 (file)
@@ -33,11 +33,12 @@ different pilight `bridge` things.
 
 The `bridge` requires the following configuration parameters:
 
-| Parameter Label | Parameter ID | Description                                                                                                                                                                              | Required |
-|-----------------|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|
-| IP Address      | ipAddress    | Host name or IP address of the pilight daemon                                                                                                                                            | yes      |
-| Port            | port         | Port number on which the pilight daemon is listening. Default: 5000                                                                                                                      | yes      |
-| Delay           | delay        | Delay (in millisecond) between consecutive commands. Recommended value without band pass filter: 1000. Recommended value with band pass filter: somewhere between 200-500. Default: 500 | no       |
+| Parameter Label      | Parameter ID        | Description                                                                                                                                                                             | Required |
+|----------------------|---------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|
+| IP Address           | ipAddress           | Host name or IP address of the pilight daemon                                                                                                                                           | yes      |
+| Port                 | port                | Port number on which the pilight daemon is listening. Default: 5000                                                                                                                     | yes      |
+| Delay                | delay               | Delay (in millisecond) between consecutive commands. Recommended value without band pass filter: 1000. Recommended value with band pass filter: somewhere between 200-500. Default: 500 | no       |
+| Background Discovery | backgroundDiscovery | Whether pilight devices for this Bridge should automatically be discovered. Default: true                                                                                               | no       |
 
 Important: you must explicitly configure the port in the pilight daemon config or otherwise a random port will be used
 and the binding will not be able to connect.
@@ -87,13 +88,13 @@ things from them.
 ### pilight.things
 
 ```java
-Bridge pilight:bridge:raspi "Pilight Daemon raspi" [ ipAddress="192.168.1.1", port=5000 ] {
+Bridge pilight:bridge:raspi "Pilight Daemon raspi" [ ipAddress="192.168.1.1", port=5000, backgroundDiscovery=false ] {
         Thing switch office "Office" [ name="office" ]
         Thing dimmer piano "Piano"  [ name="piano" ]
         Thing generic weather "Weather"  [ name="weather" ] {
             Channels:
-              State Number : temperature [ property="temperature"]
-              State Number : humidity [ property="humidity"]
+              Type number : temperature "Temperature" [ property="temperature"]
+              Type number : humidity "Humidity" [ property="humidity"]
         }
 }
 ```
index 130cc22d10f87184af90ac37ca376d2db76eb599..67832392133328cc2822b8a3c67b8e283c99c9b2 100644 (file)
@@ -26,6 +26,7 @@ public class PilightBridgeConfiguration {
     private String ipAddress = "";
     private int port = 0;
     private int delay = 500;
+    private boolean backgroundDiscovery = true;
 
     public String getIpAddress() {
         return ipAddress;
@@ -50,4 +51,12 @@ public class PilightBridgeConfiguration {
     public void setDelay(Integer delay) {
         this.delay = delay;
     }
+
+    public boolean getBackgroundDiscovery() {
+        return backgroundDiscovery;
+    }
+
+    public void setBackgroundDiscovery(boolean flag) {
+        backgroundDiscovery = flag;
+    }
 }
index fb14f9551801d40756710b045e26c4635b4a805e..ca71350ca7a4800a8e0456cc579c09a805ca7ba9 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.binding.pilight.internal.discovery;
 
 import static org.openhab.binding.pilight.internal.PilightBindingConstants.*;
 
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -33,6 +34,7 @@ import org.openhab.binding.pilight.internal.handler.PilightBridgeHandler;
 import org.openhab.core.config.discovery.AbstractDiscoveryService;
 import org.openhab.core.config.discovery.DiscoveryResult;
 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
+import org.openhab.core.config.discovery.DiscoveryService;
 import org.openhab.core.thing.ThingTypeUID;
 import org.openhab.core.thing.ThingUID;
 import org.openhab.core.thing.binding.ThingHandler;
@@ -178,11 +180,14 @@ public class PilightDeviceDiscoveryService extends AbstractDiscoveryService impl
 
     @Override
     public void activate() {
-        super.activate(null);
         final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
+        boolean discoveryEnabled = false;
         if (pilightBridgeHandler != null) {
+            removeOlderResults(new Date().getTime(), pilightBridgeHandler.getThing().getUID());
+            discoveryEnabled = pilightBridgeHandler.isBackgroundDiscoveryEnabled();
             pilightBridgeHandler.registerDiscoveryListener(this);
         }
+        super.activate(Map.of(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY, discoveryEnabled));
     }
 
     @Override
index d29d4f448bff703b55b13c427d9f200b381afa16..c7d94e03ac9e27693adf030be81571c9d2431fac 100644 (file)
@@ -65,8 +65,7 @@ public class PilightBridgeHandler extends BaseBridgeHandler {
 
     private @Nullable PilightDeviceDiscoveryService discoveryService = null;
 
-    private final ExecutorService connectorExecutor = Executors
-            .newSingleThreadExecutor(new NamedThreadFactory(getThing().getUID().getAsString(), true));
+    private @Nullable ExecutorService connectorExecutor = null;
 
     public PilightBridgeHandler(Bridge bridge) {
         super(bridge);
@@ -116,7 +115,10 @@ public class PilightBridgeHandler extends BaseBridgeHandler {
 
         updateStatus(ThingStatus.UNKNOWN);
 
+        ExecutorService connectorExecutor = Executors
+                .newSingleThreadExecutor(new NamedThreadFactory(getThing().getUID().getAsString(), true));
         connectorExecutor.execute(connector);
+        this.connectorExecutor = connectorExecutor;
         this.connector = connector;
     }
 
@@ -133,7 +135,20 @@ public class PilightBridgeHandler extends BaseBridgeHandler {
             this.connector = null;
         }
 
-        connectorExecutor.shutdown();
+        final @Nullable ExecutorService connectorExecutor = this.connectorExecutor;
+        if (connectorExecutor != null) {
+            connectorExecutor.shutdown();
+            this.connectorExecutor = null;
+        }
+    }
+
+    /**
+     * Is background discovery for this bridge enabled?
+     *
+     * @return background discovery
+     */
+    public boolean isBackgroundDiscoveryEnabled() {
+        return getConfigAs(PilightBridgeConfiguration.class).getBackgroundDiscovery();
     }
 
     /**
index c7c8993389edabf0c199717bb93ce1879cdebf51..9f8727a18005561c4d178eb989b0f5d616f01985 100644 (file)
@@ -18,12 +18,15 @@ thing-type.pilight.switch.description = Pilight Switch
 
 # thing types config
 
+thing-type.config.pilight.bridge.backgroundDiscovery.label = Background Discovery
+thing-type.config.pilight.bridge.backgroundDiscovery.description = Whether Pilight devices for this Bridge should automatically be discovered.
 thing-type.config.pilight.bridge.delay.label = Delay between Commands
 thing-type.config.pilight.bridge.delay.description = Delay (in millisecond) between consecutive commands. Recommended value without band pass filter: 1000. Recommended value with band pass filter: somewhere between 200-500.
 thing-type.config.pilight.bridge.ipAddress.label = Network Address
 thing-type.config.pilight.bridge.ipAddress.description = The IP or host name of the Pilight instance.
 thing-type.config.pilight.bridge.port.label = Port
 thing-type.config.pilight.bridge.port.description = Port of the Pilight daemon. You must explicitly configure the port in the Pilight daemon config or otherwise a random port will be used and the binding will not be able to connect.
+
 thing-type.config.pilight.device.name.label = Name of Device
 thing-type.config.pilight.device.name.description = The name of the pilight device.
 
index e52c70a6021bb46d98706919f8bbf0b47340be3c..62e0e4bcdb4a545b095c74288d71215654d3549f 100644 (file)
                                <default>500</default>
                                <advanced>true</advanced>
                        </parameter>
+                       <parameter name="backgroundDiscovery" type="boolean">
+                               <label>Background Discovery</label>
+                               <description>Whether Pilight devices for this bridge should automatically be discovered.</description>
+                               <advanced>true</advanced>
+                               <default>true</default>
+                       </parameter>
                </config-description>
        </bridge-type>