From df9c07098850bc5666b1a761d291b77581b3f691 Mon Sep 17 00:00:00 2001 From: Michael Lobstein Date: Thu, 29 Dec 2022 00:05:28 -0600 Subject: [PATCH 1/4] Fix fatal Null Pointer errors Signed-off-by: Michael Lobstein --- .../bondhome/internal/api/BondDeviceType.java | 1 + .../binding/bondhome/internal/api/BondHttpApi.java | 2 +- .../internal/discovery/BondDiscoveryService.java | 2 +- .../bondhome/internal/handler/BondDeviceHandler.java | 12 +++++++----- .../main/resources/OH-INF/i18n/bondhome.properties | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceType.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceType.java index e22517437c783..e8003e8332827 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceType.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondDeviceType.java @@ -35,6 +35,7 @@ public enum BondDeviceType { FIREPLACE(THING_TYPE_BOND_FIREPLACE), @SerializedName("GX") GENERIC_DEVICE(THING_TYPE_BOND_GENERIC); + // TODO: add Light ("LT") type private ThingTypeUID deviceTypeUid; diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java index c9fd6cd22e857..8437dc375528a 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java @@ -250,7 +250,7 @@ private synchronized String request(String uri) throws BondException { logger.debug("Repeated Bond API calls to {} failed.", uri); bridgeHandler.setBridgeOffline(ThingStatusDetail.COMMUNICATION_ERROR, "@text/offline.comm-error.api-call-failed"); - throw new BondException("@text/offline.conf-error.api-call-failed", true); + throw new BondException("@text/offline.comm-error.api-call-failed", true); } } } while (true); diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/discovery/BondDiscoveryService.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/discovery/BondDiscoveryService.java index 46d6dd05469f3..b839898a0db84 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/discovery/BondDiscoveryService.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/discovery/BondDiscoveryService.java @@ -96,7 +96,7 @@ protected synchronized void startScan() { for (final String deviceId : deviceList) { BondDevice thisDevice = api.getDevice(deviceId); String deviceName; - if ((deviceName = thisDevice.name) != null) { + if (thisDevice.type != null && (deviceName = thisDevice.name) != null) { final ThingUID deviceUid = new ThingUID(thisDevice.type.getThingTypeUID(), bridgeUid, deviceId); final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(deviceUid) .withBridge(bridgeUid).withLabel(thisDevice.name) diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java index 6f4e88cefd6dc..37d4c790f7bf6 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java @@ -554,10 +554,12 @@ private synchronized void deleteExtraChannels(List currentActi Set availableChannelIds = new HashSet<>(); for (BondDeviceAction action : currentActions) { - String actionType = action.getChannelTypeId(); - if (actionType != null) { - availableChannelIds.add(actionType); - logger.trace(" Action: {}, Relevant Channel Type Id: {}", action.getActionId(), actionType); + if (action != null) { + String actionType = action.getChannelTypeId(); + if (actionType != null) { + availableChannelIds.add(actionType); + logger.trace(" Action: {}, Relevant Channel Type Id: {}", action.getActionId(), actionType); + } } } // Remove power channels if we have a dimmer channel for them; @@ -625,7 +627,7 @@ public synchronized void updateChannelsFromState(@Nullable BondDeviceState updat BondDeviceProperties devProperties = this.deviceProperties; if (devProperties != null) { double maxSpeed = devProperties.maxSpeed; - value = (int) (((double) updateState.speed / maxSpeed) * 100); + value = (int) ((updateState.speed / maxSpeed) * 100); logger.trace("Raw fan speed: {}, Percent: {}", updateState.speed, value); } else if (updateState.speed != 0 && this.getThing().getThingTypeUID().equals(THING_TYPE_BOND_FAN)) { logger.info("Unable to convert fan speed to a percent for {}!", this.getThing().getLabel()); diff --git a/bundles/org.openhab.binding.bondhome/src/main/resources/OH-INF/i18n/bondhome.properties b/bundles/org.openhab.binding.bondhome/src/main/resources/OH-INF/i18n/bondhome.properties index b102047a2c75a..3c0e779f91aea 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/resources/OH-INF/i18n/bondhome.properties +++ b/bundles/org.openhab.binding.bondhome/src/main/resources/OH-INF/i18n/bondhome.properties @@ -82,7 +82,7 @@ channel-type.bondhome.timerChannelType.description = Starts a timer for s second # thing status descriptions -offline.comm-error.api-call-failed = Bond API call to {} failed: {} +offline.comm-error.api-call-failed = Bond API call failed. offline.comm-error.device-not-found = No Bond device found with the given device id. offline.comm-error.no-api = Bond Bridge API not available. offline.comm-error.no-response = No response received! From 6622b0a159e98c148826c4a1d1e62095b3a2f00d Mon Sep 17 00:00:00 2001 From: Michael Lobstein Date: Thu, 29 Dec 2022 00:11:32 -0600 Subject: [PATCH 2/4] revert autoformatted code Signed-off-by: Michael Lobstein --- .../binding/bondhome/internal/handler/BondDeviceHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java index 37d4c790f7bf6..a98a59cb7dc20 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java @@ -627,7 +627,7 @@ public synchronized void updateChannelsFromState(@Nullable BondDeviceState updat BondDeviceProperties devProperties = this.deviceProperties; if (devProperties != null) { double maxSpeed = devProperties.maxSpeed; - value = (int) ((updateState.speed / maxSpeed) * 100); + value = (int) (((double) updateState.speed / maxSpeed) * 100); logger.trace("Raw fan speed: {}, Percent: {}", updateState.speed, value); } else if (updateState.speed != 0 && this.getThing().getThingTypeUID().equals(THING_TYPE_BOND_FAN)) { logger.info("Unable to convert fan speed to a percent for {}!", this.getThing().getLabel()); From 0bb1fa57d60450ff854c926153ed326d3e1b18d7 Mon Sep 17 00:00:00 2001 From: Michael Lobstein Date: Thu, 29 Dec 2022 17:42:34 -0600 Subject: [PATCH 3/4] review changes Signed-off-by: Michael Lobstein --- .../binding/bondhome/internal/api/BondHttpApi.java | 4 +++- .../bondhome/internal/handler/BondDeviceHandler.java | 12 +++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java index 8437dc375528a..7338d2c3211ba 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/api/BondHttpApi.java @@ -120,7 +120,9 @@ public BondDevice getDevice(String deviceId) throws BondException { String json = request("/v2/devices/" + deviceId); logger.trace("BondHome device info : {}", json); try { - return Objects.requireNonNull(gson.fromJson(json, BondDevice.class)); + BondDevice device = Objects.requireNonNull(gson.fromJson(json, BondDevice.class)); + device.actions.removeIf(Objects::isNull); + return device; } catch (JsonParseException e) { logger.debug("Could not parse device {}'s JSON '{}'", deviceId, json, e); throw new BondException("@text/offline.comm-error.unparseable-response"); diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java index a98a59cb7dc20..f6a8a53073b0c 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java @@ -554,12 +554,10 @@ private synchronized void deleteExtraChannels(List currentActi Set availableChannelIds = new HashSet<>(); for (BondDeviceAction action : currentActions) { - if (action != null) { - String actionType = action.getChannelTypeId(); - if (actionType != null) { - availableChannelIds.add(actionType); - logger.trace(" Action: {}, Relevant Channel Type Id: {}", action.getActionId(), actionType); - } + String actionType = action.getChannelTypeId(); + if (actionType != null) { + availableChannelIds.add(actionType); + logger.trace(" Action: {}, Relevant Channel Type Id: {}", action.getActionId(), actionType); } } // Remove power channels if we have a dimmer channel for them; @@ -627,7 +625,7 @@ public synchronized void updateChannelsFromState(@Nullable BondDeviceState updat BondDeviceProperties devProperties = this.deviceProperties; if (devProperties != null) { double maxSpeed = devProperties.maxSpeed; - value = (int) (((double) updateState.speed / maxSpeed) * 100); + value = (int) ((updateState.speed / maxSpeed) * 100); logger.trace("Raw fan speed: {}, Percent: {}", updateState.speed, value); } else if (updateState.speed != 0 && this.getThing().getThingTypeUID().equals(THING_TYPE_BOND_FAN)) { logger.info("Unable to convert fan speed to a percent for {}!", this.getThing().getLabel()); From a835e09fe751ec482f34f4cc4bb653a3899de99d Mon Sep 17 00:00:00 2001 From: Michael Lobstein Date: Thu, 29 Dec 2022 17:48:23 -0600 Subject: [PATCH 4/4] review changes Signed-off-by: Michael Lobstein --- .../binding/bondhome/internal/handler/BondDeviceHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java index f6a8a53073b0c..6f4e88cefd6dc 100644 --- a/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java +++ b/bundles/org.openhab.binding.bondhome/src/main/java/org/openhab/binding/bondhome/internal/handler/BondDeviceHandler.java @@ -625,7 +625,7 @@ public synchronized void updateChannelsFromState(@Nullable BondDeviceState updat BondDeviceProperties devProperties = this.deviceProperties; if (devProperties != null) { double maxSpeed = devProperties.maxSpeed; - value = (int) ((updateState.speed / maxSpeed) * 100); + value = (int) (((double) updateState.speed / maxSpeed) * 100); logger.trace("Raw fan speed: {}, Percent: {}", updateState.speed, value); } else if (updateState.speed != 0 && this.getThing().getThingTypeUID().equals(THING_TYPE_BOND_FAN)) { logger.info("Unable to convert fan speed to a percent for {}!", this.getThing().getLabel());