[deconz] Use ChannelBuilder created by ThingHandlerCallback (#10950)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2021-07-06 20:04:06 +02:00 committed by GitHub
parent 26258e8ef5
commit 28c580bf5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 57 deletions

View File

@ -15,7 +15,6 @@ package org.openhab.binding.deconz.internal;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.library.types.PercentType; import org.openhab.core.library.types.PercentType;
import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.type.ChannelTypeUID;
/** /**
* The {@link BindingConstants} class defines common constants, which are * 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_SCENE = "scene";
public static final String CHANNEL_ONTIME = "ontime"; 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 // Thing configuration
public static final String CONFIG_HOST = "host"; public static final String CONFIG_HOST = "host";
public static final String CONFIG_HTTP_PORT = "httpPort"; public static final String CONFIG_HTTP_PORT = "httpPort";

View File

@ -12,6 +12,8 @@
*/ */
package org.openhab.binding.deconz.internal.handler; package org.openhab.binding.deconz.internal.handler;
import static org.openhab.binding.deconz.internal.BindingConstants.*;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Consumer; 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.netutils.WebSocketMessageListener;
import org.openhab.binding.deconz.internal.types.ResourceType; import org.openhab.binding.deconz.internal.types.ResourceType;
import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.ThingStatusInfo; import org.openhab.core.thing.ThingStatusInfo;
import org.openhab.core.thing.binding.BaseThingHandler; 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.openhab.core.types.Command;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -222,4 +228,30 @@ public abstract class DeconzBaseThingHandler extends BaseThingHandler implements
bridgeStatusChanged(bridge.getStatusInfo()); 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());
}
}
} }

View File

@ -46,8 +46,7 @@ import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.builder.ChannelBuilder; import org.openhab.core.thing.type.ChannelKind;
import org.openhab.core.thing.binding.builder.ThingBuilder;
import org.openhab.core.types.Command; import org.openhab.core.types.Command;
import org.openhab.core.types.CommandOption; import org.openhab.core.types.CommandOption;
import org.openhab.core.types.RefreshType; import org.openhab.core.types.RefreshType;
@ -346,22 +345,13 @@ public class LightThingHandler extends DeconzBaseThingHandler {
} }
ChannelUID effectChannelUID = new ChannelUID(thing.getUID(), CHANNEL_EFFECT); ChannelUID effectChannelUID = new ChannelUID(thing.getUID(), CHANNEL_EFFECT);
ChannelUID effectSpeedChannelUID = new ChannelUID(thing.getUID(), CHANNEL_EFFECT_SPEED); createChannel(CHANNEL_EFFECT, ChannelKind.STATE);
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());
}
switch (model) { switch (model) {
case LIDL_MELINARA: case LIDL_MELINARA:
// additional channels
createChannel(CHANNEL_EFFECT_SPEED, ChannelKind.STATE);
List<String> options = List.of("none", "steady", "snow", "rainbow", "snake", "tinkle", "fireworks", List<String> options = List.of("none", "steady", "snow", "rainbow", "snake", "tinkle", "fireworks",
"flag", "waves", "updown", "vintage", "fading", "collide", "strobe", "sparkles", "carnival", "flag", "waves", "updown", "vintage", "fading", "collide", "strobe", "sparkles", "carnival",
"glow"); "glow");

View File

@ -38,9 +38,7 @@ import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail; 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.ChannelKind;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.Command; import org.openhab.core.types.Command;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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 * Update channel value from {@link SensorConfig} object - override to include further channels
* *

View File

@ -39,9 +39,7 @@ import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.builder.ChannelBuilder; import org.openhab.core.thing.type.ChannelKind;
import org.openhab.core.thing.binding.builder.ThingBuilder;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.Command; import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType; import org.openhab.core.types.RefreshType;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -201,11 +199,8 @@ public class SensorThermostatThingHandler extends SensorBaseThingHandler {
SensorMessage sensorMessage = (SensorMessage) stateResponse; SensorMessage sensorMessage = (SensorMessage) stateResponse;
SensorState sensorState = sensorMessage.state; SensorState sensorState = sensorMessage.state;
if (sensorState != null && sensorState.windowopen != null && thing.getChannel(CHANNEL_WINDOWOPEN) == null) { if (sensorState != null && sensorState.windowopen != null) {
ThingBuilder thingBuilder = editThing(); createChannel(CHANNEL_WINDOWOPEN, ChannelKind.STATE);
thingBuilder.withChannel(ChannelBuilder.create(new ChannelUID(thing.getUID(), CHANNEL_WINDOWOPEN), "String")
.withType(new ChannelTypeUID(BINDING_ID, "open")).build());
updateThing(thingBuilder.build());
} }
super.processStateResponse(stateResponse); super.processStateResponse(stateResponse);