From 8c560a409c5a79c6f935b14b9acb64600fccd0cb Mon Sep 17 00:00:00 2001 From: Stefan Roellin Date: Sun, 10 Dec 2023 12:14:59 +0100 Subject: [PATCH] [pilight] Add option to disable background discovery for a given pilight bridge thing (#15986) * [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 --- bundles/org.openhab.binding.pilight/README.md | 17 ++++++++------- .../internal/PilightBridgeConfiguration.java | 9 ++++++++ .../PilightDeviceDiscoveryService.java | 7 ++++++- .../handler/PilightBridgeHandler.java | 21 ++++++++++++++++--- .../resources/OH-INF/i18n/pilight.properties | 3 +++ .../main/resources/OH-INF/thing/bridge.xml | 6 ++++++ 6 files changed, 51 insertions(+), 12 deletions(-) diff --git a/bundles/org.openhab.binding.pilight/README.md b/bundles/org.openhab.binding.pilight/README.md index 7be4827d1..eca1d0632 100644 --- a/bundles/org.openhab.binding.pilight/README.md +++ b/bundles/org.openhab.binding.pilight/README.md @@ -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"] } } ``` diff --git a/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/PilightBridgeConfiguration.java b/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/PilightBridgeConfiguration.java index 130cc22d1..678323921 100644 --- a/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/PilightBridgeConfiguration.java +++ b/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/PilightBridgeConfiguration.java @@ -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; + } } diff --git a/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/discovery/PilightDeviceDiscoveryService.java b/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/discovery/PilightDeviceDiscoveryService.java index fb14f9551..ca71350ca 100644 --- a/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/discovery/PilightDeviceDiscoveryService.java +++ b/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/discovery/PilightDeviceDiscoveryService.java @@ -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 diff --git a/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/handler/PilightBridgeHandler.java b/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/handler/PilightBridgeHandler.java index d29d4f448..c7d94e03a 100644 --- a/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/handler/PilightBridgeHandler.java +++ b/bundles/org.openhab.binding.pilight/src/main/java/org/openhab/binding/pilight/internal/handler/PilightBridgeHandler.java @@ -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(); } /** diff --git a/bundles/org.openhab.binding.pilight/src/main/resources/OH-INF/i18n/pilight.properties b/bundles/org.openhab.binding.pilight/src/main/resources/OH-INF/i18n/pilight.properties index c7c899338..9f8727a18 100644 --- a/bundles/org.openhab.binding.pilight/src/main/resources/OH-INF/i18n/pilight.properties +++ b/bundles/org.openhab.binding.pilight/src/main/resources/OH-INF/i18n/pilight.properties @@ -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. diff --git a/bundles/org.openhab.binding.pilight/src/main/resources/OH-INF/thing/bridge.xml b/bundles/org.openhab.binding.pilight/src/main/resources/OH-INF/thing/bridge.xml index e52c70a60..62e0e4bcd 100644 --- a/bundles/org.openhab.binding.pilight/src/main/resources/OH-INF/thing/bridge.xml +++ b/bundles/org.openhab.binding.pilight/src/main/resources/OH-INF/thing/bridge.xml @@ -32,6 +32,12 @@ 500 true + + + Whether Pilight devices for this bridge should automatically be discovered. + true + true +