Skip to content

Commit

Permalink
[deconz] Use ChannelBuilder created by ThingHandlerCallback (openhab#…
Browse files Browse the repository at this point in the history
…10950)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp authored and computergeek1507 committed Jul 13, 2021
1 parent 19ea4d7 commit 1d15493
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,12 +27,16 @@
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;
Expand Down Expand Up @@ -222,4 +228,30 @@ public void initialize() {
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
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;
Expand Down Expand Up @@ -346,22 +345,13 @@ private void checkAndUpdateEffectChannels(LightMessage lightMessage) {
}

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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
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;
Expand Down Expand Up @@ -172,32 +170,6 @@ private void processLastSeen(DeconzBaseMessage stateResponse) {
}
}

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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
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;
Expand Down Expand Up @@ -201,11 +199,8 @@ protected void processStateResponse(DeconzBaseMessage stateResponse) {

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);
Expand Down

0 comments on commit 1d15493

Please sign in to comment.