Skip to content

Commit

Permalink
[Deconz] add support for "last_seen" channel (openhab#7992)
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Lafois <[email protected]>
  • Loading branch information
blafois authored and markus7017 committed Sep 18, 2020
1 parent 1734b79 commit 05ee3da
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
6 changes: 6 additions & 0 deletions bundles/org.openhab.binding.deconz/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
1 change: 1 addition & 0 deletions bundles/org.openhab.binding.deconz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ The sensor devices support some of the following channels:
|-----------------|--------------------------|:-----------:|-------------------------------------------------------------------------------------------|----------------------------------------------|
| presence | Switch | R | Status of presence: `ON` = presence; `OFF` = no-presence | presencesensor |
| last_updated | DateTime | R | Timestamp when the sensor was last updated | all, except daylightsensor |
| last_seen | DateTime | R | Timestamp when the sensor was last seen | all, except daylightsensor |
| power | Number:Power | R | Current power usage in Watts | powersensor, sometimes for consumptionsensor |
| consumption | Number:Energy | R | Current power usage in Watts/Hour | consumptionsensor |
| voltage | Number:ElectricPotential | R | Current voltage in V | some powersensors |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class BindingConstants {
// List of all Channel ids
public static final String CHANNEL_PRESENCE = "presence";
public static final String CHANNEL_LAST_UPDATED = "last_updated";
public static final String CHANNEL_LAST_SEEN = "last_seen";
public static final String CHANNEL_POWER = "power";
public static final String CHANNEL_CONSUMPTION = "consumption";
public static final String CHANNEL_VOLTAGE = "voltage";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.deconz.internal.dto;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* The REST interface and websocket connection are using the same fields.
Expand All @@ -37,6 +38,9 @@ public class DeconzBaseMessage {
/** the API endpoint **/
public String ep = "";

/** device last seen */
public @Nullable String lastseen;

// websocket and rest api
public String uniqueid = ""; // "00:0b:57:ff:fe:94:6b:dd-01-1000"
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public abstract class DeconzBaseThingHandler<T extends DeconzBaseMessage> extend
protected ThingConfig config = new ThingConfig();
protected DeconzBridgeConfig bridgeConfig = new DeconzBridgeConfig();
protected final Gson gson;
private @Nullable ScheduledFuture<?> scheduledFuture;
@Nullable
protected ScheduledFuture<?> scheduledFuture;
protected @Nullable WebSocketConnection connection;
protected @Nullable AsyncHttpClient http;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.measure.Unit;

Expand Down Expand Up @@ -172,6 +173,19 @@ protected void processStateResponse(@Nullable SensorMessage stateResponse) {
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.
if (stateResponse.lastseen != null) {
updateState(CHANNEL_LAST_SEEN,
new DateTimeType(ZonedDateTime.ofInstant(
LocalDateTime.parse(stateResponse.lastseen, DateTimeFormatter.ISO_LOCAL_DATE_TIME),
ZoneOffset.UTC, ZoneId.systemDefault())));
// Because "last seen" is never updated by the WebSocket API - if this is supported, then we have to
// manually poll it time to time (every 5 minutes by default)
super.scheduledFuture = scheduler.schedule((Runnable) this::requestState, 5, TimeUnit.MINUTES);
}

updateStatus(ThingStatus.ONLINE);
}

Expand All @@ -198,7 +212,7 @@ protected void createChannel(String channelId, ChannelKind kind) {

/**
* Update channel value from {@link SensorConfig} object - override to include further channels
*
*
* @param channelUID
* @param newConfig
*/
Expand All @@ -222,7 +236,7 @@ protected void valueUpdated(ChannelUID channelUID, SensorConfig newConfig) {

/**
* Update channel value from {@link SensorState} object - override to include further channels
*
*
* @param channelID
* @param newState
* @param initializing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<channels>
<channel typeId="presence" id="presence"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand All @@ -35,6 +36,14 @@
<state readOnly="true" pattern="%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS"/>
</channel-type>

<channel-type id="last_seen">
<item-type>DateTime</item-type>
<label>Last Seen</label>
<description>The date and time when the sensor was last seen.</description>
<category>Time</category>
<state readOnly="true" pattern="%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS"/>
</channel-type>

<thing-type id="powersensor">
<supported-bridge-type-refs>
<bridge-type-ref id="deconz"/>
Expand All @@ -44,6 +53,7 @@
<channels>
<channel typeId="power" id="power"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand Down Expand Up @@ -84,6 +94,7 @@
<channels>
<channel typeId="consumption" id="consumption"></channel>
<channel typeId="last_updated" id="last_updated"></channel>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand All @@ -108,6 +119,7 @@
<channel typeId="buttonevent" id="buttonevent"/>
<channel typeId="button" id="button"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand Down Expand Up @@ -168,6 +180,7 @@
<channel typeId="dark" id="dark"/>
<channel typeId="daylight" id="daylight"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand Down Expand Up @@ -212,6 +225,7 @@
<channels>
<channel typeId="temperature" id="temperature"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand All @@ -235,6 +249,7 @@
<channels>
<channel typeId="humidity" id="humidity"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand All @@ -258,6 +273,7 @@
<channels>
<channel typeId="pressure" id="pressure"></channel>
<channel typeId="last_updated" id="last_updated"></channel>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand Down Expand Up @@ -317,6 +333,7 @@
<channels>
<channel typeId="open" id="open"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand All @@ -340,6 +357,7 @@
<channels>
<channel typeId="waterleakage" id="waterleakage"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand All @@ -363,6 +381,7 @@
<channels>
<channel typeId="fire" id="fire"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand All @@ -386,6 +405,7 @@
<channels>
<channel typeId="alarm" id="alarm"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand Down Expand Up @@ -416,6 +436,7 @@
<channels>
<channel typeId="vibration" id="vibration"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand All @@ -439,6 +460,7 @@
<channels>
<channel typeId="battery" id="battery_level"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand All @@ -461,6 +483,7 @@
<channels>
<channel typeId="carbonmonoxide" id="carbonmonoxide"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>

<representation-property>uid</representation-property>
Expand Down Expand Up @@ -488,6 +511,7 @@
<channel typeId="offset" id="offset"/>
<channel typeId="valve" id="valve"/>
<channel typeId="last_updated" id="last_updated"/>
<channel typeId="last_seen" id="last_seen"/>
</channels>
<representation-property>uid</representation-property>
<config-description-ref uri="thing-type:deconz:sensor"/>
Expand Down

0 comments on commit 05ee3da

Please sign in to comment.