]> git.basschouten.com Git - openhab-addons.git/commitdiff
[draytonwiser] Expose Smart Plug Power Metering, Improve null handling (#9706)
authorJames Melville <jamesmelville@gmail.com>
Wed, 6 Jan 2021 23:09:11 +0000 (23:09 +0000)
committerGitHub <noreply@github.com>
Wed, 6 Jan 2021 23:09:11 +0000 (15:09 -0800)
* [draytonwiser] Expose Smart Plug Power Metering
* [draytonwiser] Handle null values from offline devices

Signed-off-by: James Melville <jamesmelville@gmail.com>
bundles/org.openhab.binding.draytonwiser/README.md
bundles/org.openhab.binding.draytonwiser/src/main/java/org/openhab/binding/draytonwiser/internal/DraytonWiserBindingConstants.java
bundles/org.openhab.binding.draytonwiser/src/main/java/org/openhab/binding/draytonwiser/internal/handler/RoomStatHandler.java
bundles/org.openhab.binding.draytonwiser/src/main/java/org/openhab/binding/draytonwiser/internal/handler/SmartPlugHandler.java
bundles/org.openhab.binding.draytonwiser/src/main/java/org/openhab/binding/draytonwiser/internal/handler/TRVHandler.java
bundles/org.openhab.binding.draytonwiser/src/main/java/org/openhab/binding/draytonwiser/internal/model/DeviceDTO.java
bundles/org.openhab.binding.draytonwiser/src/main/java/org/openhab/binding/draytonwiser/internal/model/SmartPlugDTO.java
bundles/org.openhab.binding.draytonwiser/src/main/resources/OH-INF/thing/thing-types.xml

index 6a05d3b5c0d23b49fa976470193b9feea8bb972b..57c63b90a3fe5d366b5324262bd475b357936798 100644 (file)
@@ -111,11 +111,13 @@ The `awaySetPoint` defines the temperature in degrees Celsius that will be sent
 
 #### Smart Plug
 
-| Channel             | Item Type | Description                        |
-|---------------------|-----------|------------------------------------|
-| `currentSignalRSSI` | Number    | Relative Signal Strength Indicator |
-| `currentSignalLQI`  | Number    | Link Quality Indicator             |
-| `zigbeeConnected`   | Switch    | Is the TRV joined to network       |
+| Channel                  | Item Type     | Description                                |
+|--------------------------|---------------|--------------------------------------------|
+| `currentSignalRSSI`      | Number        | Relative Signal Strength Indicator         |
+| `currentSignalLQI`       | Number        | Link Quality Indicator                     |
+| `zigbeeConnected`        | Switch        | Is the TRV joined to network               |
+| `plugInstantaneousPower` | Number:Power  | Current Power being drawn through the plug |
+| `plugEnergyDelivered`    | Number:Energy | Cumulative energy drawn through the plug   |
 
 ### Command Channels
 
index 340498b68844fe7d21831033e329f871b604a932..aecc1d0ac95cef09961e39aaa4b019c2c95595a6 100644 (file)
@@ -18,6 +18,7 @@ import java.util.HashSet;
 import java.util.Set;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.core.library.types.DecimalType;
 import org.openhab.core.thing.ThingTypeUID;
 import org.openhab.core.types.State;
@@ -105,6 +106,8 @@ public class DraytonWiserBindingConstants {
     public static final String CHANNEL_SMARTPLUG_OUTPUT_STATE = "plugOutputState";
     public static final String CHANNEL_SMARTPLUG_AWAY_ACTION = "plugAwayAction";
     public static final String CHANNEL_COMFORT_MODE_STATE = "comfortModeState";
+    public static final String CHANNEL_SMARTPLUG_INSTANTANEOUS_POWER = "plugInstantaneousPower";
+    public static final String CHANNEL_SMARTPLUG_ENERGY_DELIVERED = "plugEnergyDelivered";
 
     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
             .unmodifiableSet(new HashSet<>(Arrays.asList(THING_TYPE_CONTROLLER, THING_TYPE_ROOM, THING_TYPE_ROOMSTAT,
@@ -149,11 +152,15 @@ public class DraytonWiserBindingConstants {
             this.batteryLevel = batteryLevel;
         }
 
-        public static State toBatteryLevel(final String level) {
-            try {
-                return new DecimalType(BatteryLevel.valueOf(level.toUpperCase()).batteryLevel);
-            } catch (final IllegalArgumentException e) {
-                // Catch unrecognized values.
+        public static State toBatteryLevel(final @Nullable String level) {
+            if (level != null) {
+                try {
+                    return new DecimalType(BatteryLevel.valueOf(level.toUpperCase()).batteryLevel);
+                } catch (final IllegalArgumentException e) {
+                    // Catch unrecognized values.
+                    return UnDefType.UNDEF;
+                }
+            } else {
                 return UnDefType.UNDEF;
             }
         }
index 05abfffd8c13636bef3995d65466f49e009cc26b..e636300aa42d8d9f804984572fa3d1e016a36a8c 100644 (file)
@@ -104,11 +104,13 @@ public class RoomStatHandler extends DraytonWiserThingHandler<RoomStatData> {
     }
 
     private State getSignalRSSI() {
-        return new DecimalType(getData().device.getRssi());
+        final Integer rssi = getData().device.getRssi();
+        return rssi == null ? UnDefType.UNDEF : new QuantityType<>(rssi, Units.DECIBEL_MILLIWATTS);
     }
 
     private State getSignalLQI() {
-        return new DecimalType(getData().device.getLqi());
+        final Integer lqi = getData().device.getLqi();
+        return lqi == null ? UnDefType.UNDEF : new DecimalType(lqi);
     }
 
     private State getWiserSignalStrength() {
@@ -120,7 +122,8 @@ public class RoomStatHandler extends DraytonWiserThingHandler<RoomStatData> {
     }
 
     private State getBatteryVoltage() {
-        return new QuantityType<>(getData().device.getBatteryVoltage() / 10.0, Units.VOLT);
+        final Integer voltage = getData().device.getBatteryVoltage();
+        return voltage == null ? UnDefType.UNDEF : new QuantityType<>(voltage / 10.0, Units.VOLT);
     }
 
     private State getWiserBatteryLevel() {
index fc7ea58fae78e92618d7395ed45f7d9b87e018fe..674c4dabf8577994862d0abaf29d2b1d300a15b7 100644 (file)
@@ -23,6 +23,8 @@ import org.openhab.binding.draytonwiser.internal.model.DraytonWiserDTO;
 import org.openhab.binding.draytonwiser.internal.model.SmartPlugDTO;
 import org.openhab.core.library.types.DecimalType;
 import org.openhab.core.library.types.OnOffType;
+import org.openhab.core.library.types.QuantityType;
+import org.openhab.core.library.unit.Units;
 import org.openhab.core.thing.Thing;
 import org.openhab.core.types.Command;
 import org.openhab.core.types.State;
@@ -79,6 +81,8 @@ public class SmartPlugHandler extends DraytonWiserThingHandler<SmartPlugData> {
         updateState(CHANNEL_ZIGBEE_CONNECTED, this::getZigbeeConnected);
         updateState(CHANNEL_DEVICE_LOCKED, this::getDeviceLocked);
         updateState(CHANNEL_MANUAL_MODE_STATE, this::getManualModeState);
+        updateState(CHANNEL_SMARTPLUG_INSTANTANEOUS_POWER, this::getInstantaneousDemand);
+        updateState(CHANNEL_SMARTPLUG_ENERGY_DELIVERED, this::getCurrentSummationDelivered);
     }
 
     @Override
@@ -100,11 +104,13 @@ public class SmartPlugHandler extends DraytonWiserThingHandler<SmartPlugData> {
     }
 
     private State getSignalRSSI() {
-        return new DecimalType(getData().device.getRssi());
+        final Integer rssi = getData().device.getRssi();
+        return rssi == null ? UnDefType.UNDEF : new QuantityType<>(rssi, Units.DECIBEL_MILLIWATTS);
     }
 
     private State getSignalLQI() {
-        return new DecimalType(getData().device.getLqi());
+        final Integer lqi = getData().device.getLqi();
+        return lqi == null ? UnDefType.UNDEF : new DecimalType(lqi);
     }
 
     private State getZigbeeConnected() {
@@ -136,6 +142,16 @@ public class SmartPlugHandler extends DraytonWiserThingHandler<SmartPlugData> {
         getApi().setSmartPlugAwayAction(getData().smartPlug.getId(), awayAction);
     }
 
+    private State getInstantaneousDemand() {
+        final Integer demand = getData().smartPlug.getInstantaneousDemand();
+        return demand == null ? UnDefType.UNDEF : new QuantityType<>(demand, Units.WATT);
+    }
+
+    private State getCurrentSummationDelivered() {
+        final Integer delivered = getData().smartPlug.getCurrentSummationDelivered();
+        return delivered == null ? UnDefType.UNDEF : new QuantityType<>(delivered, Units.WATT_HOUR);
+    }
+
     static class SmartPlugData {
         public final SmartPlugDTO smartPlug;
         public final DeviceDTO device;
index b2c2bd37e6eab07106c82f0048df83871489069b..a4fb2754868727a2e835abafca290e42b8734c95 100644 (file)
@@ -93,7 +93,8 @@ public class TRVHandler extends DraytonWiserThingHandler<SmartValveData> {
     }
 
     private State getDemand() {
-        return new QuantityType<>(getData().smartValve.getPercentageDemand(), Units.PERCENT);
+        final Integer demand = getData().smartValve.getPercentageDemand();
+        return demand == null ? UnDefType.UNDEF : new QuantityType<>(demand, Units.PERCENT);
     }
 
     private State getTemperature() {
@@ -104,11 +105,13 @@ public class TRVHandler extends DraytonWiserThingHandler<SmartValveData> {
     }
 
     private State getSignalRSSI() {
-        return new DecimalType(getData().device.getRssi());
+        final Integer rssi = getData().device.getRssi();
+        return rssi == null ? UnDefType.UNDEF : new QuantityType<>(rssi, Units.DECIBEL_MILLIWATTS);
     }
 
     private State getSignalLQI() {
-        return new DecimalType(getData().device.getLqi());
+        final Integer lqi = getData().device.getLqi();
+        return lqi == null ? UnDefType.UNDEF : new DecimalType(lqi);
     }
 
     private State getWiserSignalStrength() {
@@ -120,7 +123,8 @@ public class TRVHandler extends DraytonWiserThingHandler<SmartValveData> {
     }
 
     private State getBatteryVoltage() {
-        return new QuantityType<>(getData().device.getBatteryVoltage() / 10.0, Units.VOLT);
+        final Integer voltage = getData().device.getBatteryVoltage();
+        return voltage == null ? UnDefType.UNDEF : new QuantityType<>(voltage / 10.0, Units.VOLT);
     }
 
     private State getWiserBatteryLevel() {
index b6b216362f94e2aed0ee0dcbc37a72d5ca8c8141..c7b81ff442d2185479ca80b2046746e7d5bfd3ac 100644 (file)
@@ -88,8 +88,8 @@ public class DeviceDTO {
         return displayedSignalStrength;
     }
 
-    public int getBatteryVoltage() {
-        return batteryVoltage == null ? Integer.MIN_VALUE : batteryVoltage;
+    public Integer getBatteryVoltage() {
+        return batteryVoltage;
     }
 
     public String getBatteryLevel() {
index 419409303fc667ddce7a9dd3615b8bac5e17ab86..9e4f7261455dc0636dac6cbd32c434066dc56d71 100644 (file)
@@ -31,6 +31,8 @@ public class SmartPlugDTO {
     private String targetState;
     private Integer debounceCount;
     private String overrideState;
+    private Integer currentSummationDelivered;
+    private Integer instantaneousDemand;
 
     public Integer getId() {
         return id;
@@ -75,4 +77,12 @@ public class SmartPlugDTO {
     public String getMode() {
         return mode;
     }
+
+    public Integer getCurrentSummationDelivered() {
+        return currentSummationDelivered;
+    }
+
+    public Integer getInstantaneousDemand() {
+        return instantaneousDemand;
+    }
 }
index 6fd046ace695bba34e413f1c61fe556b41f3f4a9..e87d7a6f85cdbd52d9d9e8ffc0ffd4f34fb2013f 100644 (file)
                        <channel id="zigbeeConnected" typeId="zigbeeConnected-channel"/>
                        <channel id="deviceLocked" typeId="deviceLocked-channel"/>
                        <channel id="manualModeState" typeId="manualModeState-channel"/>
+                       <channel id="plugInstantaneousPower" typeId="plugInstantaneousPower-channel"/>
+                       <channel id="plugEnergyDelivered" typeId="plugEnergyDelivered-channel"/>
                </channels>
 
                <representation-property>serialNumber</representation-property>
                <description>Should the room pre-heat to achieve the desired temperature</description>
        </channel-type>
 
+       <channel-type id="plugInstantaneousPower-channel">
+               <item-type>Number:Power</item-type>
+               <label>Plug Instantaneous Power</label>
+               <description>Current Power being drawn through the plug</description>
+               <state readOnly="true" pattern="%d %unit%"/>
+       </channel-type>
+
+       <channel-type id="plugEnergyDelivered-channel">
+               <item-type>Number:Energy</item-type>
+               <label>Plug Energy Delivered</label>
+               <description>Cumulative energy drawn through the plug</description>
+               <state readOnly="true" pattern="%d %unit%"/>
+       </channel-type>
+
 </thing:thing-descriptions>