]> git.basschouten.com Git - openhab-addons.git/commitdiff
[deconz] Use ChannelBuilder created by ThingHandlerCallback (#10950)
authorChristoph Weitkamp <github@christophweitkamp.de>
Tue, 6 Jul 2021 18:04:06 +0000 (20:04 +0200)
committerGitHub <noreply@github.com>
Tue, 6 Jul 2021 18:04:06 +0000 (20:04 +0200)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.binding.deconz/src/main/java/org/openhab/binding/deconz/internal/BindingConstants.java
bundles/org.openhab.binding.deconz/src/main/java/org/openhab/binding/deconz/internal/handler/DeconzBaseThingHandler.java
bundles/org.openhab.binding.deconz/src/main/java/org/openhab/binding/deconz/internal/handler/LightThingHandler.java
bundles/org.openhab.binding.deconz/src/main/java/org/openhab/binding/deconz/internal/handler/SensorBaseThingHandler.java
bundles/org.openhab.binding.deconz/src/main/java/org/openhab/binding/deconz/internal/handler/SensorThermostatThingHandler.java

index 595f1716282f60fe371e7786e066fbffe9a033e0..0cf5c0c0d2cea7909a71ade9dac4485f7e0a1ca3 100644 (file)
@@ -15,7 +15,6 @@ package org.openhab.binding.deconz.internal;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.core.library.types.PercentType;
 import org.openhab.core.thing.ThingTypeUID;
-import org.openhab.core.thing.type.ChannelTypeUID;
 
 /**
  * The {@link BindingConstants} class defines common constants, which are
@@ -120,11 +119,6 @@ public class BindingConstants {
     public static final String CHANNEL_SCENE = "scene";
     public static final String CHANNEL_ONTIME = "ontime";
 
-    // channel uids
-    public static final ChannelTypeUID CHANNEL_EFFECT_TYPE_UID = new ChannelTypeUID(BINDING_ID, CHANNEL_EFFECT);
-    public static final ChannelTypeUID CHANNEL_EFFECT_SPEED_TYPE_UID = new ChannelTypeUID(BINDING_ID,
-            CHANNEL_EFFECT_SPEED);
-
     // Thing configuration
     public static final String CONFIG_HOST = "host";
     public static final String CONFIG_HTTP_PORT = "httpPort";
index 9f40f49022676a45c1cb0afb2049cabc6e8b3cfa..e869fd15cbb5bf9a953ca00236552f18f00b4039 100644 (file)
@@ -12,6 +12,8 @@
  */
 package org.openhab.binding.deconz.internal.handler;
 
+import static org.openhab.binding.deconz.internal.BindingConstants.*;
+
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Consumer;
@@ -25,12 +27,16 @@ import org.openhab.binding.deconz.internal.netutils.WebSocketConnection;
 import org.openhab.binding.deconz.internal.netutils.WebSocketMessageListener;
 import org.openhab.binding.deconz.internal.types.ResourceType;
 import org.openhab.core.thing.Bridge;
+import org.openhab.core.thing.Channel;
 import org.openhab.core.thing.ChannelUID;
 import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingStatus;
 import org.openhab.core.thing.ThingStatusDetail;
 import org.openhab.core.thing.ThingStatusInfo;
 import org.openhab.core.thing.binding.BaseThingHandler;
+import org.openhab.core.thing.binding.ThingHandlerCallback;
+import org.openhab.core.thing.type.ChannelKind;
+import org.openhab.core.thing.type.ChannelTypeUID;
 import org.openhab.core.types.Command;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -222,4 +228,30 @@ public abstract class DeconzBaseThingHandler extends BaseThingHandler implements
             bridgeStatusChanged(bridge.getStatusInfo());
         }
     }
+
+    protected void createChannel(String channelId, ChannelKind kind) {
+        if (thing.getChannel(channelId) != null) {
+            // channel already exists, no update necessary
+            return;
+        }
+
+        ThingHandlerCallback callback = getCallback();
+        if (callback != null) {
+            ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
+            ChannelTypeUID channelTypeUID;
+            switch (channelId) {
+                case CHANNEL_BATTERY_LEVEL:
+                    channelTypeUID = new ChannelTypeUID("system:battery-level");
+                    break;
+                case CHANNEL_BATTERY_LOW:
+                    channelTypeUID = new ChannelTypeUID("system:low-battery");
+                    break;
+                default:
+                    channelTypeUID = new ChannelTypeUID(BINDING_ID, channelId);
+                    break;
+            }
+            Channel channel = callback.createChannelBuilder(channelUID, channelTypeUID).withKind(kind).build();
+            updateThing(editThing().withChannel(channel).build());
+        }
+    }
 }
index dab69ea8100b2865ba3c5f1b81be7170894859da..4a6a81328b48165e9ab251792c043f1d0e03d7b4 100644 (file)
@@ -46,8 +46,7 @@ import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingStatus;
 import org.openhab.core.thing.ThingStatusDetail;
 import org.openhab.core.thing.ThingTypeUID;
-import org.openhab.core.thing.binding.builder.ChannelBuilder;
-import org.openhab.core.thing.binding.builder.ThingBuilder;
+import org.openhab.core.thing.type.ChannelKind;
 import org.openhab.core.types.Command;
 import org.openhab.core.types.CommandOption;
 import org.openhab.core.types.RefreshType;
@@ -346,22 +345,13 @@ public class LightThingHandler extends DeconzBaseThingHandler {
         }
 
         ChannelUID effectChannelUID = new ChannelUID(thing.getUID(), CHANNEL_EFFECT);
-        ChannelUID effectSpeedChannelUID = new ChannelUID(thing.getUID(), CHANNEL_EFFECT_SPEED);
-
-        if (thing.getChannel(CHANNEL_EFFECT) == null) {
-            ThingBuilder thingBuilder = editThing();
-            thingBuilder.withChannel(
-                    ChannelBuilder.create(effectChannelUID, "String").withType(CHANNEL_EFFECT_TYPE_UID).build());
-            if (model == EffectLightModel.LIDL_MELINARA) {
-                // additional channels
-                thingBuilder.withChannel(ChannelBuilder.create(effectSpeedChannelUID, "Number")
-                        .withType(CHANNEL_EFFECT_SPEED_TYPE_UID).build());
-            }
-            updateThing(thingBuilder.build());
-        }
+        createChannel(CHANNEL_EFFECT, ChannelKind.STATE);
 
         switch (model) {
             case LIDL_MELINARA:
+                // additional channels
+                createChannel(CHANNEL_EFFECT_SPEED, ChannelKind.STATE);
+
                 List<String> options = List.of("none", "steady", "snow", "rainbow", "snake", "tinkle", "fireworks",
                         "flag", "waves", "updown", "vintage", "fading", "collide", "strobe", "sparkles", "carnival",
                         "glow");
index 389d8c83f69c8ed5d5681892f7e8d2f56250a54b..f54998b58b84a3c0e97a6e639e0e8407f2e57aaa 100644 (file)
@@ -38,9 +38,7 @@ import org.openhab.core.thing.ChannelUID;
 import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingStatus;
 import org.openhab.core.thing.ThingStatusDetail;
-import org.openhab.core.thing.binding.ThingHandlerCallback;
 import org.openhab.core.thing.type.ChannelKind;
-import org.openhab.core.thing.type.ChannelTypeUID;
 import org.openhab.core.types.Command;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -172,32 +170,6 @@ public abstract class SensorBaseThingHandler extends DeconzBaseThingHandler {
         }
     }
 
-    protected void createChannel(String channelId, ChannelKind kind) {
-        if (thing.getChannel(channelId) != null) {
-            // channel already exists, no update necessary
-            return;
-        }
-
-        ThingHandlerCallback callback = getCallback();
-        if (callback != null) {
-            ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
-            ChannelTypeUID channelTypeUID;
-            switch (channelId) {
-                case CHANNEL_BATTERY_LEVEL:
-                    channelTypeUID = new ChannelTypeUID("system:battery-level");
-                    break;
-                case CHANNEL_BATTERY_LOW:
-                    channelTypeUID = new ChannelTypeUID("system:low-battery");
-                    break;
-                default:
-                    channelTypeUID = new ChannelTypeUID(BINDING_ID, channelId);
-                    break;
-            }
-            Channel channel = callback.createChannelBuilder(channelUID, channelTypeUID).withKind(kind).build();
-            updateThing(editThing().withChannel(channel).build());
-        }
-    }
-
     /**
      * Update channel value from {@link SensorConfig} object - override to include further channels
      *
index a3df6fb063245337bd8a6875b8cfc8e16d269b72..f6e83fbb23a87a8b7b52ea18c46350959e8789df 100644 (file)
@@ -39,9 +39,7 @@ import org.openhab.core.library.types.StringType;
 import org.openhab.core.thing.ChannelUID;
 import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingTypeUID;
-import org.openhab.core.thing.binding.builder.ChannelBuilder;
-import org.openhab.core.thing.binding.builder.ThingBuilder;
-import org.openhab.core.thing.type.ChannelTypeUID;
+import org.openhab.core.thing.type.ChannelKind;
 import org.openhab.core.types.Command;
 import org.openhab.core.types.RefreshType;
 import org.slf4j.Logger;
@@ -201,11 +199,8 @@ public class SensorThermostatThingHandler extends SensorBaseThingHandler {
 
         SensorMessage sensorMessage = (SensorMessage) stateResponse;
         SensorState sensorState = sensorMessage.state;
-        if (sensorState != null && sensorState.windowopen != null && thing.getChannel(CHANNEL_WINDOWOPEN) == null) {
-            ThingBuilder thingBuilder = editThing();
-            thingBuilder.withChannel(ChannelBuilder.create(new ChannelUID(thing.getUID(), CHANNEL_WINDOWOPEN), "String")
-                    .withType(new ChannelTypeUID(BINDING_ID, "open")).build());
-            updateThing(thingBuilder.build());
+        if (sensorState != null && sensorState.windowopen != null) {
+            createChannel(CHANNEL_WINDOWOPEN, ChannelKind.STATE);
         }
 
         super.processStateResponse(stateResponse);