diff --git a/bundles/org.openhab.binding.livisismarthome/src/main/java/org/openhab/binding/livisismarthome/internal/handler/LivisiDeviceHandler.java b/bundles/org.openhab.binding.livisismarthome/src/main/java/org/openhab/binding/livisismarthome/internal/handler/LivisiDeviceHandler.java index f5a6bb5c3db..e0968245b5e 100644 --- a/bundles/org.openhab.binding.livisismarthome/src/main/java/org/openhab/binding/livisismarthome/internal/handler/LivisiDeviceHandler.java +++ b/bundles/org.openhab.binding.livisismarthome/src/main/java/org/openhab/binding/livisismarthome/internal/handler/LivisiDeviceHandler.java @@ -348,7 +348,7 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu @Override public void onDeviceStateChanged(final DeviceDTO device) { synchronized (this.lock) { - updateChannels(device); + updateChannels(device, false); } } @@ -367,18 +367,18 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu 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 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()); } @@ -566,7 +566,7 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu return true; } - private void updateChannels(DeviceDTO device) { + private void updateChannels(DeviceDTO device, boolean isChangedByEvent) { // DEVICE STATES final boolean isReachable = updateStatus(device); @@ -580,7 +580,7 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu 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()); @@ -608,7 +608,7 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu } } - 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); @@ -647,7 +647,7 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu updateLuminanceSensorChannels(capability); break; case CapabilityDTO.TYPE_PUSHBUTTONSENSOR: - updatePushButtonSensorChannels(capability); + updatePushButtonSensorChannels(capability, isChangedByEvent); break; case CapabilityDTO.TYPE_ENERGYCONSUMPTIONSENSOR: updateEnergyConsumptionSensorChannels(capability); @@ -833,7 +833,7 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu } } - 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(); @@ -841,24 +841,32 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu 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); diff --git a/bundles/org.openhab.binding.livisismarthome/src/test/java/org/openhab/binding/livisismarthome/internal/handler/LivisiDeviceHandlerTest.java b/bundles/org.openhab.binding.livisismarthome/src/test/java/org/openhab/binding/livisismarthome/internal/handler/LivisiDeviceHandlerTest.java index a10b6da286e..c27b7a2f24b 100644 --- a/bundles/org.openhab.binding.livisismarthome/src/test/java/org/openhab/binding/livisismarthome/internal/handler/LivisiDeviceHandlerTest.java +++ b/bundles/org.openhab.binding.livisismarthome/src/test/java/org/openhab/binding/livisismarthome/internal/handler/LivisiDeviceHandlerTest.java @@ -855,7 +855,10 @@ public class LivisiDeviceHandlerTest { 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)); } @@ -872,8 +875,11 @@ public class LivisiDeviceHandlerTest { 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 @@ -889,7 +895,10 @@ public class LivisiDeviceHandlerTest { 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)); } @@ -906,8 +915,11 @@ public class LivisiDeviceHandlerTest { 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 @@ -920,6 +932,7 @@ public class LivisiDeviceHandlerTest { 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)); } @@ -1079,6 +1092,7 @@ public class LivisiDeviceHandlerTest { 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)); } @@ -1202,7 +1216,7 @@ public class LivisiDeviceHandlerTest { } @Test - public void testOnDeviceStateChanged_Event_PushButtonSensor() { + public void testOnDeviceStateChanged_Event_PushButtonSensor_Button1_ShortPress() { DeviceDTO device = createDevice(); addCapabilityToDevice(CapabilityDTO.TYPE_PUSHBUTTONSENSOR, null, device); @@ -1222,6 +1236,27 @@ public class LivisiDeviceHandlerTest { 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);