[deconz] improve sensor thing initialization (#9453)

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
This commit is contained in:
J-N-K
2020-12-23 01:18:58 +01:00
committed by GitHub
parent 9fb476f24e
commit 74aef9beb8
3 changed files with 62 additions and 9 deletions

View File

@@ -16,6 +16,7 @@ import static org.openhab.binding.deconz.internal.BindingConstants.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@@ -111,10 +112,8 @@ public abstract class SensorBaseThingHandler extends DeconzBaseThingHandler {
}
SensorMessage sensorMessage = (SensorMessage) stateResponse;
SensorConfig newSensorConfig = sensorMessage.config;
sensorConfig = newSensorConfig != null ? newSensorConfig : new SensorConfig();
SensorState newSensorState = sensorMessage.state;
sensorState = newSensorState != null ? newSensorState : new SensorState();
sensorConfig = Objects.requireNonNullElse(sensorMessage.config, new SensorConfig());
sensorState = Objects.requireNonNullElse(sensorMessage.state, new SensorState());
// Add some information about the sensor
if (!sensorConfig.reachable) {
@@ -146,10 +145,6 @@ public abstract class SensorBaseThingHandler extends DeconzBaseThingHandler {
ignoreConfigurationUpdate = false;
// Initial data
updateChannels(sensorConfig);
updateChannels(sensorState, true);
// "Last seen" is the last "ping" from the device, whereas "last update" is the last status changed.
// For example, for a fire sensor, the device pings regularly, without necessarily updating channels.
// So to monitor a sensor is still alive, the "last seen" is necessary.
@@ -165,6 +160,10 @@ public abstract class SensorBaseThingHandler extends DeconzBaseThingHandler {
config.lastSeenPolling);
}
// Initial data
updateChannels(sensorConfig);
updateChannels(sensorState, true);
updateStatus(ThingStatus.ONLINE);
}
@@ -176,6 +175,11 @@ 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);
@@ -192,7 +196,7 @@ public abstract class SensorBaseThingHandler extends DeconzBaseThingHandler {
break;
}
Channel channel = callback.createChannelBuilder(channelUID, channelTypeUID).withKind(kind).build();
updateThing(editThing().withoutChannel(channelUID).withChannel(channel).build());
updateThing(editThing().withChannel(channel).build());
}
}