[livisismarthome] Button trigger channels are triggered on restart #12968 (#12969)

* [livisismarthome] Button trigger channels are triggered on restart #12968

Signed-off-by: Sven Strohschein <sven.strohschein@gmail.com>
This commit is contained in:
Sven Strohschein 2022-06-23 09:25:07 +02:00 committed by GitHub
parent 3465e8f30d
commit df06cd3af3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 25 deletions

View File

@ -348,7 +348,7 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu
@Override @Override
public void onDeviceStateChanged(final DeviceDTO device) { public void onDeviceStateChanged(final DeviceDTO device) {
synchronized (this.lock) { synchronized (this.lock) {
updateChannels(device); updateChannels(device, false);
} }
} }
@ -367,18 +367,18 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu
if (capability.hasState()) { if (capability.hasState()) {
boolean deviceChanged = updateDevice(event, capability); boolean deviceChanged = updateDevice(event, capability);
if (deviceChanged) { if (deviceChanged) {
updateChannels(device); updateChannels(device, true);
} }
} else { } else {
logger.debug("Capability {} has no state (yet?) - refreshing device.", capability.getName()); logger.debug("Capability {} has no state (yet?) - refreshing device.", capability.getName());
Optional<DeviceDTO> deviceOptional = refreshDevice(linkedCapabilityId); Optional<DeviceDTO> deviceOptional = refreshDevice(linkedCapabilityId);
deviceOptional.ifPresent(this::updateChannels); deviceOptional.ifPresent((d) -> updateChannels(d, true));
} }
} }
} else if (event.isLinkedtoDevice()) { } else if (event.isLinkedtoDevice()) {
if (device.hasDeviceState()) { if (device.hasDeviceState()) {
updateChannels(device); updateChannels(device, true);
} else { } else {
logger.debug("Device {}/{} has no state.", device.getConfig().getName(), device.getId()); logger.debug("Device {}/{} has no state.", device.getConfig().getName(), device.getId());
} }
@ -566,7 +566,7 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu
return true; return true;
} }
private void updateChannels(DeviceDTO device) { private void updateChannels(DeviceDTO device, boolean isChangedByEvent) {
// DEVICE STATES // DEVICE STATES
final boolean isReachable = updateStatus(device); 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()); logger.debug("->capability:{} ({}/{})", capability.getId(), capability.getType(), capability.getName());
if (capability.hasState()) { if (capability.hasState()) {
updateCapabilityChannels(device, capability); updateCapabilityChannels(device, capability, isChangedByEvent);
} else { } else {
logger.debug("Capability not available for device {} ({})", device.getConfig().getName(), logger.debug("Capability not available for device {} ({})", device.getConfig().getName(),
device.getType()); 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()) { switch (capability.getType()) {
case CapabilityDTO.TYPE_VARIABLEACTUATOR: case CapabilityDTO.TYPE_VARIABLEACTUATOR:
updateVariableActuatorChannels(capability); updateVariableActuatorChannels(capability);
@ -647,7 +647,7 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu
updateLuminanceSensorChannels(capability); updateLuminanceSensorChannels(capability);
break; break;
case CapabilityDTO.TYPE_PUSHBUTTONSENSOR: case CapabilityDTO.TYPE_PUSHBUTTONSENSOR:
updatePushButtonSensorChannels(capability); updatePushButtonSensorChannels(capability, isChangedByEvent);
break; break;
case CapabilityDTO.TYPE_ENERGYCONSUMPTIONSENSOR: case CapabilityDTO.TYPE_ENERGYCONSUMPTIONSENSOR:
updateEnergyConsumptionSensorChannels(capability); 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 pushCount = capability.getCapabilityState().getPushButtonSensorCounterState();
final Integer buttonIndex = capability.getCapabilityState().getPushButtonSensorButtonIndexState(); final Integer buttonIndex = capability.getCapabilityState().getPushButtonSensorButtonIndexState();
final String type = capability.getCapabilityState().getPushButtonSensorButtonIndexType(); final String type = capability.getCapabilityState().getPushButtonSensorButtonIndexType();
@ -841,24 +841,32 @@ public class LivisiDeviceHandler extends BaseThingHandler implements DeviceStatu
if (buttonIndex != null && pushCount != null) { if (buttonIndex != null && pushCount != null) {
if (buttonIndex >= 0 && buttonIndex <= 7) { if (buttonIndex >= 0 && buttonIndex <= 7) {
final int channelIndex = buttonIndex + 1; 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)); 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 { } else {
logStateNull(capability); 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) { private void updateEnergyConsumptionSensorChannels(CapabilityDTO capability) {
updateStateForEnergyChannelKiloWattHour(CHANNEL_ENERGY_CONSUMPTION_MONTH_KWH, updateStateForEnergyChannelKiloWattHour(CHANNEL_ENERGY_CONSUMPTION_MONTH_KWH,
capability.getCapabilityState().getEnergyConsumptionSensorEnergyConsumptionMonthKWhState(), capability); capability.getCapabilityState().getEnergyConsumptionSensorEnergyConsumptionMonthKWhState(), capability);

View File

@ -855,7 +855,10 @@ public class LivisiDeviceHandlerTest {
deviceHandler.onDeviceStateChanged(device); deviceHandler.onDeviceStateChanged(device);
assertTrue(isChannelUpdated("button1Count", new DecimalType(10))); 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)); assertFalse(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED));
} }
@ -872,8 +875,11 @@ public class LivisiDeviceHandlerTest {
deviceHandler.onDeviceStateChanged(device); deviceHandler.onDeviceStateChanged(device);
assertTrue(isChannelUpdated("button1Count", new DecimalType(10))); 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)); assertFalse(isChannelTriggered("button1", CommonTriggerEvents.SHORT_PRESSED));
assertTrue(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED)); assertFalse(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED));
} }
@Test @Test
@ -889,7 +895,10 @@ public class LivisiDeviceHandlerTest {
deviceHandler.onDeviceStateChanged(device); deviceHandler.onDeviceStateChanged(device);
assertTrue(isChannelUpdated("button2Count", new DecimalType(10))); 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)); assertFalse(isChannelTriggered("button2", CommonTriggerEvents.LONG_PRESSED));
} }
@ -906,8 +915,11 @@ public class LivisiDeviceHandlerTest {
deviceHandler.onDeviceStateChanged(device); deviceHandler.onDeviceStateChanged(device);
assertTrue(isChannelUpdated("button2Count", new DecimalType(10))); 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)); assertFalse(isChannelTriggered("button2", CommonTriggerEvents.SHORT_PRESSED));
assertTrue(isChannelTriggered("button2", CommonTriggerEvents.LONG_PRESSED)); assertFalse(isChannelTriggered("button2", CommonTriggerEvents.LONG_PRESSED));
} }
@Test @Test
@ -920,6 +932,7 @@ public class LivisiDeviceHandlerTest {
deviceHandler.onDeviceStateChanged(device); deviceHandler.onDeviceStateChanged(device);
assertFalse(isChannelUpdated(CHANNEL_BUTTON_COUNT)); assertFalse(isChannelUpdated(CHANNEL_BUTTON_COUNT));
assertFalse(isChannelUpdated("button1Count")); assertFalse(isChannelUpdated("button1Count"));
assertFalse(isChannelTriggered("button1", CommonTriggerEvents.PRESSED));
assertFalse(isChannelTriggered("button1", CommonTriggerEvents.SHORT_PRESSED)); assertFalse(isChannelTriggered("button1", CommonTriggerEvents.SHORT_PRESSED));
assertFalse(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED)); assertFalse(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED));
} }
@ -1079,6 +1092,7 @@ public class LivisiDeviceHandlerTest {
deviceHandler.onDeviceStateChanged(device, event); deviceHandler.onDeviceStateChanged(device, event);
assertTrue(isChannelUpdated("button1Count", new DecimalType(10))); assertTrue(isChannelUpdated("button1Count", new DecimalType(10)));
assertTrue(isChannelTriggered("button1", CommonTriggerEvents.PRESSED));
assertTrue(isChannelTriggered("button1", CommonTriggerEvents.SHORT_PRESSED)); assertTrue(isChannelTriggered("button1", CommonTriggerEvents.SHORT_PRESSED));
assertFalse(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED)); assertFalse(isChannelTriggered("button1", CommonTriggerEvents.LONG_PRESSED));
} }
@ -1202,7 +1216,7 @@ public class LivisiDeviceHandlerTest {
} }
@Test @Test
public void testOnDeviceStateChanged_Event_PushButtonSensor() { public void testOnDeviceStateChanged_Event_PushButtonSensor_Button1_ShortPress() {
DeviceDTO device = createDevice(); DeviceDTO device = createDevice();
addCapabilityToDevice(CapabilityDTO.TYPE_PUSHBUTTONSENSOR, null, device); addCapabilityToDevice(CapabilityDTO.TYPE_PUSHBUTTONSENSOR, null, device);
@ -1222,6 +1236,27 @@ public class LivisiDeviceHandlerTest {
assertFalse(isChannelTriggered("button2", CommonTriggerEvents.PRESSED)); 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 @Test
public void testOnDeviceStateChanged_StateChangedEvent_PushButtonSensor_SHC_Classic() { public void testOnDeviceStateChanged_StateChangedEvent_PushButtonSensor_SHC_Classic() {
when(bridgeHandlerMock.isSHCClassic()).thenReturn(true); when(bridgeHandlerMock.isSHCClassic()).thenReturn(true);