[homematic] Fix NullPointerException in discovery (#17390)

* #17372: Fixed NPE

Signed-off-by: Sönke Küper <soenkekueper@gmx.de>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Sönke Küper 2024-09-16 20:13:52 +02:00 committed by Ciprian Pascu
parent 37d732d76c
commit 40bd429619
2 changed files with 15 additions and 7 deletions

View File

@ -78,7 +78,7 @@ public class HomematicDeviceDiscoveryService
Thing bridge = thingHandler.getThing();
ThingStatus bridgeStatus = bridge.getStatus();
if (ThingStatus.ONLINE == bridgeStatus) {
if (ThingStatus.ONLINE == bridgeStatus && gateway != null) {
gateway.setInstallMode(true, getInstallModeDuration());
int remaining = gateway.getInstallMode();
@ -109,7 +109,10 @@ public class HomematicDeviceDiscoveryService
public synchronized void stopScan() {
logger.debug("Stopping Homematic discovery scan");
disableInstallMode();
thingHandler.getGateway().cancelLoadAllDeviceMetadata();
final HomematicGateway gateway = thingHandler.getGateway();
if (gateway != null) {
gateway.cancelLoadAllDeviceMetadata();
}
waitForScanFinishing();
super.stopScan();
}
@ -117,10 +120,11 @@ public class HomematicDeviceDiscoveryService
private void disableInstallMode() {
try {
synchronized (installModeSync) {
if (isInInstallMode) {
final HomematicGateway gateway = thingHandler.getGateway();
if (isInInstallMode && gateway != null) {
isInInstallMode = false;
installModeSync.notify();
thingHandler.getGateway().setInstallMode(false, 0);
gateway.setInstallMode(false, 0);
}
}
} catch (Exception ex) {
@ -172,10 +176,10 @@ public class HomematicDeviceDiscoveryService
* Starts a thread which loads all Homematic devices connected to the gateway.
*/
public void loadDevices() {
if (loadDevicesFuture == null && thingHandler.getGateway() != null) {
final HomematicGateway gateway = thingHandler.getGateway();
if (loadDevicesFuture == null && gateway != null) {
loadDevicesFuture = scheduler.submit(() -> {
try {
final HomematicGateway gateway = thingHandler.getGateway();
gateway.loadAllDeviceMetadata();
thingHandler.getTypeGenerator().validateFirmwares();
} catch (Throwable ex) {

View File

@ -24,6 +24,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.homematic.internal.common.HomematicConfig;
import org.openhab.binding.homematic.internal.communicator.HomematicGateway;
@ -267,8 +268,11 @@ public class HomematicBridgeHandler extends BaseBridgeHandler implements Homemat
}
/**
* Returns the HomematicGateway.
* Returns the {@link HomematicGateway}.
*
* @return The gateway or null if gateway has not yet been initialized.
*/
@Nullable
public HomematicGateway getGateway() {
return gateway;
}