@Override
public void onDeviceStateChanged(final DeviceDTO device) {
synchronized (this.lock) {
- updateChannels(device);
+ updateChannels(device, false);
}
}
if (capability.hasState()) {
boolean deviceChanged = updateDevice(event, capability);
if (deviceChanged) {
- updateChannels(device);
+ updateChannels(device, true);
}
} else {
logger.debug("Capability {} has no state (yet?) - refreshing device.", capability.getName());
Optional<DeviceDTO> deviceOptional = refreshDevice(linkedCapabilityId);
- deviceOptional.ifPresent(this::updateChannels);
+ deviceOptional.ifPresent((d) -> updateChannels(d, true));
}
}
} else if (event.isLinkedtoDevice()) {
if (device.hasDeviceState()) {
- updateChannels(device);
+ updateChannels(device, true);
} else {
logger.debug("Device {}/{} has no state.", device.getConfig().getName(), device.getId());
}
return true;
}
- private void updateChannels(DeviceDTO device) {
+ private void updateChannels(DeviceDTO device, boolean isChangedByEvent) {
// DEVICE STATES
final boolean isReachable = updateStatus(device);
logger.debug("->capability:{} ({}/{})", capability.getId(), capability.getType(), capability.getName());
if (capability.hasState()) {
- updateCapabilityChannels(device, capability);
+ updateCapabilityChannels(device, capability, isChangedByEvent);
} else {
logger.debug("Capability not available for device {} ({})", device.getConfig().getName(),
device.getType());
}
}
- private void updateCapabilityChannels(DeviceDTO device, CapabilityDTO capability) {
+ private void updateCapabilityChannels(DeviceDTO device, CapabilityDTO capability, boolean isChangedByEvent) {
switch (capability.getType()) {
case CapabilityDTO.TYPE_VARIABLEACTUATOR:
updateVariableActuatorChannels(capability);
updateLuminanceSensorChannels(capability);
break;
case CapabilityDTO.TYPE_PUSHBUTTONSENSOR:
- updatePushButtonSensorChannels(capability);
+ updatePushButtonSensorChannels(capability, isChangedByEvent);
break;
case CapabilityDTO.TYPE_ENERGYCONSUMPTIONSENSOR:
updateEnergyConsumptionSensorChannels(capability);
}
}
- private void updatePushButtonSensorChannels(CapabilityDTO capability) {
+ private void updatePushButtonSensorChannels(CapabilityDTO capability, boolean isChangedByEvent) {
final Integer pushCount = capability.getCapabilityState().getPushButtonSensorCounterState();
final Integer buttonIndex = capability.getCapabilityState().getPushButtonSensorButtonIndexState();
final String type = capability.getCapabilityState().getPushButtonSensorButtonIndexType();
if (buttonIndex != null && pushCount != null) {
if (buttonIndex >= 0 && buttonIndex <= 7) {
final int channelIndex = buttonIndex + 1;
- if (type != null) {
- if (SHORT_PRESS.equals(type)) {
- triggerChannel(CHANNEL_BUTTON + channelIndex, CommonTriggerEvents.SHORT_PRESSED);
- } else if (LONG_PRESS.equals(type)) {
- triggerChannel(CHANNEL_BUTTON + channelIndex, CommonTriggerEvents.LONG_PRESSED);
- }
- }
- triggerChannel(CHANNEL_BUTTON + channelIndex, CommonTriggerEvents.PRESSED);
updateState(String.format(CHANNEL_BUTTON_COUNT, channelIndex), new DecimalType(pushCount));
+
+ if (isChangedByEvent) {
+ triggerButtonChannels(type, channelIndex);
+ }
+
+ // Button handled so remove state to avoid re-trigger.
+ capability.getCapabilityState().setPushButtonSensorButtonIndexState(null);
+ capability.getCapabilityState().setPushButtonSensorButtonIndexType(null);
}
- // Button handled so remove state to avoid re-trigger.
- capability.getCapabilityState().setPushButtonSensorButtonIndexState(null);
- capability.getCapabilityState().setPushButtonSensorButtonIndexType(null);
} else {
logStateNull(capability);
}
}
+ private void triggerButtonChannels(@Nullable String type, int channelIndex) {
+ if (type != null) {
+ if (SHORT_PRESS.equals(type)) {
+ triggerChannel(CHANNEL_BUTTON + channelIndex, CommonTriggerEvents.SHORT_PRESSED);
+ } else if (LONG_PRESS.equals(type)) {
+ triggerChannel(CHANNEL_BUTTON + channelIndex, CommonTriggerEvents.LONG_PRESSED);
+ }
+ }
+ triggerChannel(CHANNEL_BUTTON + channelIndex, CommonTriggerEvents.PRESSED);
+ }
+
private void updateEnergyConsumptionSensorChannels(CapabilityDTO capability) {
updateStateForEnergyChannelKiloWattHour(CHANNEL_ENERGY_CONSUMPTION_MONTH_KWH,
capability.getCapabilityState().getEnergyConsumptionSensorEnergyConsumptionMonthKWhState(), capability);
deviceHandler.onDeviceStateChanged(device);
assertTrue(isChannelUpdated("button1Count", new DecimalType(10)));
- assertTrue(isChannelTriggered("button1", CommonTriggerEvents.SHORT_PRESSED));
+ // Trigger channels should only get triggered by events (onDeviceStateChanged(device, event), not
+ // onDeviceStateChanged(device)).
+ assertFalse(isChannelTriggered("button1", CommonTriggerEvents.PRESSED));
+ assertFalse(isChannelTriggered("button1", CommonTriggerEvents.SHORT_PRESSED));
assertFalse(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED));
}
deviceHandler.onDeviceStateChanged(device);
assertTrue(isChannelUpdated("button1Count", new DecimalType(10)));
+ // Trigger channels should only get triggered by events (onDeviceStateChanged(device, event), not
+ // onDeviceStateChanged(device)).
+ assertFalse(isChannelTriggered("button1", CommonTriggerEvents.PRESSED));
assertFalse(isChannelTriggered("button1", CommonTriggerEvents.SHORT_PRESSED));
- assertTrue(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED));
+ assertFalse(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED));
}
@Test
deviceHandler.onDeviceStateChanged(device);
assertTrue(isChannelUpdated("button2Count", new DecimalType(10)));
- assertTrue(isChannelTriggered("button2", CommonTriggerEvents.SHORT_PRESSED));
+ // Trigger channels should only get triggered by events (onDeviceStateChanged(device, event), not
+ // onDeviceStateChanged(device)).
+ assertFalse(isChannelTriggered("button2", CommonTriggerEvents.PRESSED));
+ assertFalse(isChannelTriggered("button2", CommonTriggerEvents.SHORT_PRESSED));
assertFalse(isChannelTriggered("button2", CommonTriggerEvents.LONG_PRESSED));
}
deviceHandler.onDeviceStateChanged(device);
assertTrue(isChannelUpdated("button2Count", new DecimalType(10)));
+ // Trigger channels should only get triggered by events (onDeviceStateChanged(device, event), not
+ // onDeviceStateChanged(device)).
+ assertFalse(isChannelTriggered("button2", CommonTriggerEvents.PRESSED));
assertFalse(isChannelTriggered("button2", CommonTriggerEvents.SHORT_PRESSED));
- assertTrue(isChannelTriggered("button2", CommonTriggerEvents.LONG_PRESSED));
+ assertFalse(isChannelTriggered("button2", CommonTriggerEvents.LONG_PRESSED));
}
@Test
deviceHandler.onDeviceStateChanged(device);
assertFalse(isChannelUpdated(CHANNEL_BUTTON_COUNT));
assertFalse(isChannelUpdated("button1Count"));
+ assertFalse(isChannelTriggered("button1", CommonTriggerEvents.PRESSED));
assertFalse(isChannelTriggered("button1", CommonTriggerEvents.SHORT_PRESSED));
assertFalse(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED));
}
deviceHandler.onDeviceStateChanged(device, event);
assertTrue(isChannelUpdated("button1Count", new DecimalType(10)));
+ assertTrue(isChannelTriggered("button1", CommonTriggerEvents.PRESSED));
assertTrue(isChannelTriggered("button1", CommonTriggerEvents.SHORT_PRESSED));
assertFalse(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED));
}
}
@Test
- public void testOnDeviceStateChanged_Event_PushButtonSensor() {
+ public void testOnDeviceStateChanged_Event_PushButtonSensor_Button1_ShortPress() {
DeviceDTO device = createDevice();
addCapabilityToDevice(CapabilityDTO.TYPE_PUSHBUTTONSENSOR, null, device);
assertFalse(isChannelTriggered("button2", CommonTriggerEvents.PRESSED));
}
+ @Test
+ public void testOnDeviceStateChanged_Event_PushButtonSensor_Button1_LongPress() {
+ DeviceDTO device = createDevice();
+ addCapabilityToDevice(CapabilityDTO.TYPE_PUSHBUTTONSENSOR, null, device);
+
+ LivisiDeviceHandler deviceHandler = createDeviceHandler(device);
+
+ EventDTO event = createCapabilityEvent(c -> {
+ c.setKeyPressCounter(10);
+ c.setKeyPressButtonIndex(0);
+ c.setKeyPressType("LongPress");
+ });
+
+ deviceHandler.onDeviceStateChanged(device, event);
+ assertTrue(isChannelUpdated("button1Count", new DecimalType(10)));
+ assertTrue(isChannelTriggered("button1", CommonTriggerEvents.PRESSED));
+ assertFalse(isChannelTriggered("button1", CommonTriggerEvents.SHORT_PRESSED));
+ assertTrue(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED));
+ assertFalse(isChannelTriggered("button2", CommonTriggerEvents.PRESSED));
+ }
+
@Test
public void testOnDeviceStateChanged_StateChangedEvent_PushButtonSensor_SHC_Classic() {
when(bridgeHandlerMock.isSHCClassic()).thenReturn(true);