[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 <stefan@roellin-baumann.ch>
This commit is contained in:
Stefan Roellin 2023-12-10 12:14:59 +01:00 committed by GitHub
parent 7da863aa49
commit 8c560a409c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 12 deletions

View File

@ -34,10 +34,11 @@ 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 |
| 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"]
}
}
```

View 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;
}
}

View 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

View 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;
}
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();
}
/**

View 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.

View File

@ -32,6 +32,12 @@
<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>