]> git.basschouten.com Git - openhab-addons.git/commitdiff
[livisismarthome] Button trigger channels are triggered on restart #12968 (#12969)
authorSven Strohschein <novanic@gmx.de>
Thu, 23 Jun 2022 07:25:07 +0000 (09:25 +0200)
committerGitHub <noreply@github.com>
Thu, 23 Jun 2022 07:25:07 +0000 (09:25 +0200)
* [livisismarthome] Button trigger channels are triggered on restart #12968

Signed-off-by: Sven Strohschein <sven.strohschein@gmail.com>
bundles/org.openhab.binding.livisismarthome/src/main/java/org/openhab/binding/livisismarthome/internal/handler/LivisiDeviceHandler.java
bundles/org.openhab.binding.livisismarthome/src/test/java/org/openhab/binding/livisismarthome/internal/handler/LivisiDeviceHandlerTest.java

index f5a6bb5c3db6cfb41fba63a4cded12b3c31fb358..e0968245b5ea5fbe81b458f6e256e6482e9454e7 100644 (file)
@@ -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<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());
                 }
@@ -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);
index a10b6da286e00d4fd76910625833cbe9f75435ab..c27b7a2f24b535d9da4ab4402c9f29a97f9133c5 100644 (file)
@@ -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);