Skip to content

Commit

Permalink
[deconz] Added properties for sensors (openhab#5899)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <[email protected]>
Signed-off-by: Tim Roberts <[email protected]>
  • Loading branch information
cweitkamp authored and tmrobert8 committed Jan 21, 2020
1 parent 04604b5 commit 510c21e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public class BindingConstants {
public static final String CONFIG_HOST = "host";
public static final String CONFIG_APIKEY = "apikey";

public static final String UNIQUE_ID = "uid";

public static String url(String host, @Nullable String apikey, @Nullable String endpointType,
@Nullable String endpointID) {
StringBuilder url = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void addDevice(String sensorID, SensorMessage sensor) {

DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(uid).withBridge(handler.getThing().getUID())
.withLabel(sensor.name + " (" + sensor.manufacturername + ")").withProperty("id", sensorID)
.withProperty("uid", sensor.uniqueid).withRepresentationProperty("uid").build();
.withProperty(UNIQUE_ID, sensor.uniqueid).withRepresentationProperty(UNIQUE_ID).build();
thingDiscovered(discoveryResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
*/
@NonNullByDefault
public class SensorThingHandler extends BaseThingHandler implements ValueUpdateListener {

private final Logger logger = LoggerFactory.getLogger(SensorThingHandler.class);
private SensorThingConfig config = new SensorThingConfig();
private DeconzBridgeConfig bridgeConfig = new DeconzBridgeConfig();
Expand Down Expand Up @@ -185,17 +186,22 @@ public void bridgeStatusChanged(@NonNull ThingStatusInfo bridgeStatusInfo) {
return;
}

Map<String, String> editProperties = editProperties();
editProperties.put(Thing.PROPERTY_FIRMWARE_VERSION, newState.swversion);
editProperties.put(Thing.PROPERTY_MODEL_ID, newState.modelid);
editProperties.put(UNIQUE_ID, newState.uniqueid);
ignoreConfigurationUpdate = true;
updateProperties(editProperties);

// Some sensors support optional channels
// (see https://github.com/dresden-elektronik/deconz-rest-plugin/wiki/Supported-Devices#sensors)
// any battery-powered sensor
Integer batteryLevel = newState.config.battery;
if (batteryLevel != null) {
ignoreConfigurationUpdate = true;
createAndUpdateChannelIfExists(CHANNEL_BATTERY_LEVEL,
new DecimalType(batteryLevel.longValue()));
createAndUpdateChannelIfExists(CHANNEL_BATTERY_LOW,
batteryLevel <= 10 ? OnOffType.ON : OnOffType.OFF);
ignoreConfigurationUpdate = false;
}

// some Xiaomi sensors
Expand Down Expand Up @@ -227,6 +233,7 @@ public void bridgeStatusChanged(@NonNull ThingStatusInfo bridgeStatusInfo) {
if (newState.state.tampered != null) {
createChannel(CHANNEL_TAMPERED);
}
ignoreConfigurationUpdate = false;

// Initial data
for (Channel channel : thing.getChannels()) {
Expand All @@ -237,6 +244,7 @@ public void bridgeStatusChanged(@NonNull ThingStatusInfo bridgeStatusInfo) {
webSocketConnection.registerValueListener(config.id, this);
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
});

}

private void createAndUpdateChannelIfExists(String channelId, State state) {
Expand Down Expand Up @@ -314,16 +322,16 @@ public void valueUpdated(ChannelUID channelUID, SensorState state, boolean initi
if (state.dark != null) {
boolean dark = state.dark;
if (dark) { // if it's dark, it's dark ;)
updateState(CHANNEL_LIGHT, new StringType("Dark"));
updateState(channelUID, new StringType("Dark"));
} else if (state.daylight != null) { // if its not dark, it might be between darkness and daylight
boolean daylight = state.daylight;
if (daylight) {
updateState(CHANNEL_LIGHT, new StringType("Daylight"));
updateState(channelUID, new StringType("Daylight"));
} else if (!daylight) {
updateState(CHANNEL_LIGHT, new StringType("Sunset"));
updateState(channelUID, new StringType("Sunset"));
}
} else { // if no daylight value is known, we assume !dark means daylight
updateState(CHANNEL_LIGHT, new StringType("Daylight"));
updateState(channelUID, new StringType("Daylight"));
}
}
break;
Expand Down Expand Up @@ -412,7 +420,7 @@ public void valueUpdated(ChannelUID channelUID, SensorState state, boolean initi
break;
case CHANNEL_BUTTONEVENT:
if (buttonevent != null && !initializing) {
triggerChannel(CHANNEL_BUTTONEVENT, String.valueOf(buttonevent));
triggerChannel(channelUID, String.valueOf(buttonevent));
}
break;
case CHANNEL_LAST_UPDATED:
Expand Down

0 comments on commit 510c21e

Please sign in to comment.