Skip to content

Commit

Permalink
[insteon] added support for alternate heartbeat to motion sensor 2 (o…
Browse files Browse the repository at this point in the history
…penhab#8085)

Signed-off-by: Rob Nielsen <rob.nielsen@yahoo.com>
  • Loading branch information
robnielsen authored and andrewfg committed Aug 31, 2020
1 parent 62d9339 commit 1e276e4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ enum State {
*/
public boolean action(GroupMessage a, InsteonAddress address, int group, byte cmd1) {
publish = false;
long currentTime = System.currentTimeMillis();
switch (state) {
case EXPECT_BCAST:
switch (a) {
Expand All @@ -136,7 +137,16 @@ public boolean action(GroupMessage a, InsteonAddress address, int group, byte cm
switch (a) {
case BCAST:
if (lastCmd1 == cmd1) {
publish = false;
if (currentTime > lastUpdated + 30000) {
if (logger.isDebugEnabled()) {
logger.debug(
"{} group {} cmd1 {} is not a dup BCAST, received last message over 30000 ms ago",
address, group, Utils.getHexByte(cmd1));
}
publish = true;
} else {
publish = false;
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("{} group {} cmd1 {} is not a dup BCAST, last cmd1 {}", address, group,
Expand Down Expand Up @@ -181,7 +191,7 @@ public boolean action(GroupMessage a, InsteonAddress address, int group, byte cm
}

lastCmd1 = cmd1;
lastUpdated = System.currentTimeMillis();
lastUpdated = currentTime;
logger.debug("{} group {} state: {} --{}--> {}, publish: {}", address, group, oldState, a, state, publish);
return (publish);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,49 @@ public void handleMessage(int group, byte cmd1, Msg msg, DeviceFeature f) {
}
}

@NonNullByDefault
public static class MotionSensor2AlternateHeartbeatHandler extends MessageHandler {
MotionSensor2AlternateHeartbeatHandler(DeviceFeature p) {
super(p);
}

@Override
public void handleMessage(int group, byte cmd1, Msg msg, DeviceFeature f) {
InsteonDevice dev = f.getDevice();
try {
// group 0x0B (11) - alternate heartbeat group
InsteonAddress toAddr = msg.getAddr("toAddress");
int batteryLevel = toAddr.getHighByte() & 0xff;
int lightLevel = toAddr.getMiddleByte() & 0xff;
int temperatureLevel = msg.getByte("command2") & 0xff;

logger.debug("{}: {} got light level: {}, battery level: {}, temperature level: {}", nm(),
dev.getAddress(), lightLevel, batteryLevel, temperatureLevel);
feature.publish(new DecimalType(lightLevel), StateChangeType.CHANGED, InsteonDeviceHandler.FIELD,
InsteonDeviceHandler.FIELD_LIGHT_LEVEL);
feature.publish(new DecimalType(batteryLevel), StateChangeType.CHANGED, InsteonDeviceHandler.FIELD,
InsteonDeviceHandler.FIELD_BATTERY_LEVEL);
feature.publish(new DecimalType(temperatureLevel), StateChangeType.CHANGED, InsteonDeviceHandler.FIELD,
InsteonDeviceHandler.FIELD_TEMPERATURE_LEVEL);

// per 2844-222 dev doc: working battery level range is 0xd2 - 0x70
int batteryPercentage;
if (batteryLevel >= 0xd2) {
batteryPercentage = 100;
} else if (batteryLevel <= 0x70) {
batteryPercentage = 0;
} else {
batteryPercentage = (batteryLevel - 0x70) * 100 / (0xd2 - 0x70);
}
logger.debug("{}: {} battery percentage: {}", nm(), dev.getAddress(), batteryPercentage);
feature.publish(new QuantityType<>(batteryPercentage, SmartHomeUnits.PERCENT), StateChangeType.CHANGED,
InsteonDeviceHandler.FIELD, InsteonDeviceHandler.FIELD_BATTERY_PERCENTAGE);
} catch (FieldException e) {
logger.warn("error parsing {}: ", msg, e);
}
}
}

@NonNullByDefault
public static class HiddenDoorSensorDataReplyHandler extends MessageHandler {
HiddenDoorSensorDataReplyHandler(DeviceFeature p) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,20 @@
<command-handler command="OnOffType">NoOpCommandHandler</command-handler>
<poll-handler>NoPollHandler</poll-handler>
</feature>
<feature name="MotionSensor2Data">
<message-dispatcher>SimpleDispatcher</message-dispatcher>
<message-handler cmd="0x03" group="1">NoOpMsgHandler</message-handler>
<message-handler cmd="0x0C" group="1">NoOpMsgHandler</message-handler>
<message-handler cmd="0x11" group="1">NoOpMsgHandler</message-handler>
<message-handler cmd="0x13" group="1">NoOpMsgHandler</message-handler>
<message-handler cmd="0x03" group="11">NoOpMsgHandler</message-handler>
<message-handler cmd="0x0C" group="11">MotionSensor2AlternateHeartbeatHandler</message-handler>
<message-handler cmd="0x11" group="11">NoOpMsgHandler</message-handler>
<message-handler cmd="0x13" group="11">NoOpMsgHandler</message-handler>
<message-handler cmd="0x2e">MotionSensorDataReplyHandler</message-handler>
<command-handler command="OnOffType">NoOpCommandHandler</command-handler>
<poll-handler>NoPollHandler</poll-handler>
</feature>
<feature name="HiddenDoorSensorData">
<message-dispatcher>SimpleDispatcher</message-dispatcher>
<message-handler cmd="0x03" group="1">NoOpMsgHandler</message-handler>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@
<feature name="contact">WirelessMotionSensorContact</feature>
<feature name="lightlevelabovethreshold">WirelessMotionSensorLightLevelAboveThreshold</feature>
<feature name="lowbattery">WirelessMotionSensorLowBattery</feature>
<feature name="data">MotionSensorData</feature>
<feature name="data">MotionSensor2Data</feature>
<feature name="tamperswitch">WirelessMotionSensor2TamperSwitch</feature>
<feature name="lastheardfrom">GenericLastTime</feature>
</device>
Expand Down

0 comments on commit 1e276e4

Please sign in to comment.