From 8c90265719afef9356d3ccff71c0f55fca5bd4c2 Mon Sep 17 00:00:00 2001 From: jlaur Date: Wed, 8 Sep 2021 20:36:59 +0200 Subject: [PATCH] [hdpowerview] Add new battery level channel (#11206) * Introduce new batteryLevel channel. Fixes #11205 Signed-off-by: Jacob Laursen --- .../org.openhab.binding.hdpowerview/README.md | 3 ++- .../internal/HDPowerViewBindingConstants.java | 1 + .../handler/HDPowerViewShadeHandler.java | 22 ++++++++++++++++++- .../resources/OH-INF/thing/thing-types.xml | 1 + .../hdpowerview/HDPowerViewJUnitTests.java | 2 ++ .../src/test/resources/duette.json | 4 ++-- 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.hdpowerview/README.md b/bundles/org.openhab.binding.hdpowerview/README.md index ab9fe8f76f07e..7f043983b74f6 100644 --- a/bundles/org.openhab.binding.hdpowerview/README.md +++ b/bundles/org.openhab.binding.hdpowerview/README.md @@ -74,11 +74,12 @@ If it is a dual action (top-down plus bottom-up) shade, there is also a roller s All of these channels appear in the binding, but only those which have a physical implementation in the shade, will have any physical effect. | Channel | Item Type | Description | -|----------------|--------------------------|------------| +|----------------|--------------------------|-------------| | position | Rollershutter | The vertical position of the shade's rail -- see [next chapter](#Roller-Shutter-Up/Down-Position-vs.-Open/Close-State). Up/Down commands will move the rail completely up or completely down. Percentage commands will move the rail to an intermediate position. Stop commands will halt any current movement of the rail. | | secondary | Rollershutter | The vertical position of the secondary rail (if any). Its function is basically identical to the `position` channel above -- but see [next chapter](#Roller-Shutter-Up/Down-Position-vs.-Open/Close-State). | | vane | Dimmer | The degree of opening of the slats or vanes. Setting this to a non-zero value will first move the shade `position` fully down, since the slats or vanes can only have a defined state if the shade is in its down position -- see [Interdependency between Channel positions](#Interdependency-between-Channel-positions). | | lowBattery | Switch | Indicates ON when the battery level of the shade is low, as determined by the hub's internal rules. | +| batteryLevel | Number | Battery level (10% = low, 50% = medium, 100% = high) | batteryVoltage | Number:ElectricPotential | Battery voltage reported by the shade. | | signalStrength | Number | Signal strength (0 for no or unknown signal, 1 for weak, 2 for average, 3 for good or 4 for excellent) | diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewBindingConstants.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewBindingConstants.java index 1b710cb277a71..da235dd22c8da 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewBindingConstants.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/HDPowerViewBindingConstants.java @@ -41,6 +41,7 @@ public class HDPowerViewBindingConstants { public static final String CHANNEL_SHADE_SECONDARY_POSITION = "secondary"; public static final String CHANNEL_SHADE_VANE = "vane"; public static final String CHANNEL_SHADE_LOW_BATTERY = "lowBattery"; + public static final String CHANNEL_SHADE_BATTERY_LEVEL = "batteryLevel"; public static final String CHANNEL_SHADE_BATTERY_VOLTAGE = "batteryVoltage"; public static final String CHANNEL_SHADE_SIGNAL_STRENGTH = "signalStrength"; diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewShadeHandler.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewShadeHandler.java index 960d182fddd7e..70bf65bdd75ed 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewShadeHandler.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/handler/HDPowerViewShadeHandler.java @@ -137,7 +137,7 @@ protected void onReceiveUpdate(@Nullable ShadeData shadeData) { if (shadeData != null) { updateStatus(ThingStatus.ONLINE); updateBindingStates(shadeData.positions); - updateState(CHANNEL_SHADE_LOW_BATTERY, shadeData.batteryStatus == 1 ? OnOffType.ON : OnOffType.OFF); + updateBatteryLevel(shadeData.batteryStatus); updateState(CHANNEL_SHADE_BATTERY_VOLTAGE, new QuantityType<>(shadeData.batteryStrength / 10, Units.VOLT)); updateState(CHANNEL_SHADE_SIGNAL_STRENGTH, new DecimalType(shadeData.signalStrength)); } else { @@ -157,6 +157,26 @@ private void updateBindingStates(@Nullable ShadePosition shadePos) { } } + private void updateBatteryLevel(int batteryStatus) { + int mappedValue; + switch (batteryStatus) { + case 1: // Low + mappedValue = 10; + break; + case 2: // Medium + mappedValue = 50; + break; + case 3: // High + case 4: // Plugged in + mappedValue = 100; + break; + default: // No status available (0) or invalid + return; + } + updateState(CHANNEL_SHADE_LOW_BATTERY, batteryStatus == 1 ? OnOffType.ON : OnOffType.OFF); + updateState(CHANNEL_SHADE_BATTERY_LEVEL, new DecimalType(mappedValue)); + } + private void moveShade(ActuatorClass actuatorClass, CoordinateSystem coordSys, int newPercent) { try { HDPowerViewHubHandler bridge; diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml index 19e8e9a8a18f7..9794e8368aa5f 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.hdpowerview/src/main/resources/OH-INF/thing/thing-types.xml @@ -48,6 +48,7 @@ + diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/HDPowerViewJUnitTests.java b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/HDPowerViewJUnitTests.java index 75a56d6102958..8fd7e844c565d 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/HDPowerViewJUnitTests.java +++ b/bundles/org.openhab.binding.hdpowerview/src/test/java/org/openhab/binding/hdpowerview/HDPowerViewJUnitTests.java @@ -356,6 +356,8 @@ public void testOfflineJsonParsing() { pos = shadePos.getState(PRIMARY_ACTUATOR, VANE_COORDS); assertEquals(UnDefType.class, pos.getClass()); + assertEquals(3, shadeData.batteryStatus); + assertEquals(4, shadeData.signalStrength); } catch (JsonParseException e) { fail(e.getMessage()); diff --git a/bundles/org.openhab.binding.hdpowerview/src/test/resources/duette.json b/bundles/org.openhab.binding.hdpowerview/src/test/resources/duette.json index 4e0e8a7d5291d..ff4e03e01c00a 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/test/resources/duette.json +++ b/bundles/org.openhab.binding.hdpowerview/src/test/resources/duette.json @@ -6,8 +6,8 @@ { "id": 63778, "type": 8, - "batteryStatus": 0, - "batteryStrength": 0, + "batteryStatus": 3, + "batteryStrength": 168, "roomId": 891, "firmware": { "revision": 1,