Added "timestamp-trigger" Profile (#2364)

* Added Timestamp on Trigger Profile

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
pull/2380/head
Christoph Weitkamp 2021-05-23 22:43:27 +02:00 committed by GitHub
parent 37d88a41fd
commit d43106c016
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 130 additions and 14 deletions

View File

@ -67,13 +67,13 @@ public class SystemProfileFactory implements ProfileFactory, ProfileAdvisor, Pro
RAWBUTTON_TOGGLE_ROLLERSHUTTER_TYPE, RAWBUTTON_TOGGLE_SWITCH_TYPE, RAWROCKER_DIMMER_TYPE,
RAWROCKER_NEXT_PREVIOUS_TYPE, RAWROCKER_ON_OFF_TYPE, RAWROCKER_PLAY_PAUSE_TYPE,
RAWROCKER_REWIND_FASTFORWARD_TYPE, RAWROCKER_STOP_MOVE_TYPE, RAWROCKER_UP_DOWN_TYPE, TIMESTAMP_CHANGE_TYPE,
TIMESTAMP_OFFSET_TYPE, TIMESTAMP_UPDATE_TYPE);
TIMESTAMP_OFFSET_TYPE, TIMESTAMP_TRIGGER_TYPE, TIMESTAMP_UPDATE_TYPE);
private static final Set<ProfileTypeUID> SUPPORTED_PROFILE_TYPE_UIDS = Set.of(DEFAULT, FOLLOW, HYSTERESIS, OFFSET,
RANGE, RAWBUTTON_ON_OFF_SWITCH, RAWBUTTON_TOGGLE_PLAYER, RAWBUTTON_TOGGLE_ROLLERSHUTTER,
RAWBUTTON_TOGGLE_SWITCH, RAWROCKER_DIMMER, RAWROCKER_NEXT_PREVIOUS, RAWROCKER_ON_OFF, RAWROCKER_PLAY_PAUSE,
RAWROCKER_REWIND_FASTFORWARD, RAWROCKER_STOP_MOVE, RAWROCKER_UP_DOWN, TIMESTAMP_CHANGE, TIMESTAMP_OFFSET,
TIMESTAMP_UPDATE);
TIMESTAMP_TRIGGER, TIMESTAMP_UPDATE);
private final Map<LocalizedKey, ProfileType> localizedProfileTypeCache = new ConcurrentHashMap<>();
@ -128,6 +128,8 @@ public class SystemProfileFactory implements ProfileFactory, ProfileAdvisor, Pro
return new TimestampChangeProfile(callback);
} else if (TIMESTAMP_OFFSET.equals(profileTypeUID)) {
return new TimestampOffsetProfile(callback, context);
} else if (TIMESTAMP_TRIGGER.equals(profileTypeUID)) {
return new TimestampTriggerProfile(callback);
} else if (TIMESTAMP_UPDATE.equals(profileTypeUID)) {
return new TimestampUpdateProfile(callback);
} else {

View File

@ -0,0 +1,56 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.thing.internal.profiles;
import static org.openhab.core.thing.profiles.SystemProfiles.TIMESTAMP_TRIGGER;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.thing.profiles.ProfileCallback;
import org.openhab.core.thing.profiles.ProfileTypeUID;
import org.openhab.core.thing.profiles.TriggerProfile;
import org.openhab.core.types.State;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This is the default implementation for a trigger timestamp profile.
* The timestamp updates to now each time the channel is triggered.
*
* @author Christoph Weitkamp - Initial contribution
*/
@NonNullByDefault
public class TimestampTriggerProfile implements TriggerProfile {
private final Logger logger = LoggerFactory.getLogger(TimestampTriggerProfile.class);
private final ProfileCallback callback;
TimestampTriggerProfile(ProfileCallback callback) {
this.callback = callback;
}
@Override
public ProfileTypeUID getProfileTypeUID() {
return TIMESTAMP_TRIGGER;
}
@Override
public void onStateUpdateFromItem(State state) {
}
@Override
public void onTriggerFromHandler(String event) {
logger.debug("Received trigger from Handler, sending timestamp to callback");
callback.sendUpdate(new DateTimeType());
}
}

View File

@ -51,6 +51,7 @@ public interface SystemProfiles {
public static final ProfileTypeUID RAWROCKER_UP_DOWN = new ProfileTypeUID(SYSTEM_SCOPE, "rawrocker-to-up-down");
public static final ProfileTypeUID TIMESTAMP_CHANGE = new ProfileTypeUID(SYSTEM_SCOPE, "timestamp-change");
public static final ProfileTypeUID TIMESTAMP_OFFSET = new ProfileTypeUID(SYSTEM_SCOPE, "timestamp-offset");
public static final ProfileTypeUID TIMESTAMP_TRIGGER = new ProfileTypeUID(SYSTEM_SCOPE, "timestamp-trigger");
public static final ProfileTypeUID TIMESTAMP_UPDATE = new ProfileTypeUID(SYSTEM_SCOPE, "timestamp-update");
static final ProfileType DEFAULT_TYPE = ProfileTypeBuilder.newState(DEFAULT, "Default").build();
@ -74,55 +75,55 @@ public interface SystemProfiles {
static final ProfileType RAWBUTTON_ON_OFF_SWITCH_TYPE = ProfileTypeBuilder
.newTrigger(RAWBUTTON_ON_OFF_SWITCH, "Raw Button To On Off")
.withSupportedItemTypes(CoreItemFactory.SWITCH, CoreItemFactory.DIMMER, CoreItemFactory.COLOR)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWBUTTON.getUID()).build();
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_RAWBUTTON).build();
static final ProfileType RAWBUTTON_TOGGLE_PLAYER_TYPE = ProfileTypeBuilder
.newTrigger(RAWBUTTON_TOGGLE_PLAYER, "Raw Button Toggle Player")
.withSupportedItemTypes(CoreItemFactory.PLAYER)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWBUTTON.getUID()).build();
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_RAWBUTTON).build();
static final ProfileType RAWBUTTON_TOGGLE_ROLLERSHUTTER_TYPE = ProfileTypeBuilder
.newTrigger(RAWBUTTON_TOGGLE_ROLLERSHUTTER, "Raw Button Toggle Rollershutter")
.withSupportedItemTypes(CoreItemFactory.ROLLERSHUTTER)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWBUTTON.getUID()).build();
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_RAWBUTTON).build();
static final ProfileType RAWBUTTON_TOGGLE_SWITCH_TYPE = ProfileTypeBuilder
.newTrigger(RAWBUTTON_TOGGLE_SWITCH, "Raw Button Toggle Switch")
.withSupportedItemTypes(CoreItemFactory.SWITCH, CoreItemFactory.DIMMER, CoreItemFactory.COLOR)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWBUTTON.getUID()).build();
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_RAWBUTTON).build();
static final ProfileType RAWROCKER_ON_OFF_TYPE = ProfileTypeBuilder
.newTrigger(RAWROCKER_ON_OFF, "Raw Rocker To On Off")
.withSupportedItemTypes(CoreItemFactory.SWITCH, CoreItemFactory.DIMMER, CoreItemFactory.COLOR)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWROCKER.getUID()).build();
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_RAWROCKER).build();
static final ProfileType RAWROCKER_DIMMER_TYPE = ProfileTypeBuilder
.newTrigger(RAWROCKER_DIMMER, "Raw Rocker To Dimmer").withSupportedItemTypes(CoreItemFactory.DIMMER)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWROCKER.getUID()).build();
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_RAWROCKER).build();
static final ProfileType RAWROCKER_NEXT_PREVIOUS_TYPE = ProfileTypeBuilder
.newTrigger(RAWROCKER_NEXT_PREVIOUS, "Raw Rocker To Next/Previous")
.withSupportedItemTypes(CoreItemFactory.PLAYER)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWROCKER.getUID()).build();
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_RAWROCKER).build();
static final ProfileType RAWROCKER_PLAY_PAUSE_TYPE = ProfileTypeBuilder
.newTrigger(RAWROCKER_PLAY_PAUSE, "Raw Rocker To Play/Pause").withSupportedItemTypes(CoreItemFactory.PLAYER)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWROCKER.getUID()).build();
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_RAWROCKER).build();
static final ProfileType RAWROCKER_REWIND_FASTFORWARD_TYPE = ProfileTypeBuilder
.newTrigger(RAWROCKER_REWIND_FASTFORWARD, "Raw Rocker To Rewind/Fastforward")
.withSupportedItemTypes(CoreItemFactory.PLAYER)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWROCKER.getUID()).build();
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_RAWROCKER).build();
static final ProfileType RAWROCKER_STOP_MOVE_TYPE = ProfileTypeBuilder
.newTrigger(RAWROCKER_STOP_MOVE, "Raw Rocker To Stop/Move")
.withSupportedItemTypes(CoreItemFactory.ROLLERSHUTTER)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWROCKER.getUID()).build();
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_RAWROCKER).build();
static final ProfileType RAWROCKER_UP_DOWN_TYPE = ProfileTypeBuilder
.newTrigger(RAWROCKER_UP_DOWN, "Raw Rocker To Up/Down")
.withSupportedItemTypes(CoreItemFactory.ROLLERSHUTTER)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWROCKER.getUID()).build();
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_TYPE_UID_RAWROCKER).build();
static final ProfileType TIMESTAMP_CHANGE_TYPE = ProfileTypeBuilder
.newState(TIMESTAMP_CHANGE, "Timestamp on change").withSupportedItemTypes(CoreItemFactory.DATETIME).build();
@ -131,6 +132,10 @@ public interface SystemProfiles {
.withSupportedItemTypes(CoreItemFactory.DATETIME).withSupportedItemTypesOfChannel(CoreItemFactory.DATETIME)
.build();
static final ProfileType TIMESTAMP_TRIGGER_TYPE = ProfileTypeBuilder
.newTrigger(TIMESTAMP_TRIGGER, "Timestamp on Trigger").withSupportedItemTypes(CoreItemFactory.DATETIME)
.build();
static final ProfileType TIMESTAMP_UPDATE_TYPE = ProfileTypeBuilder
.newState(TIMESTAMP_UPDATE, "Timestamp on update").withSupportedItemTypes(CoreItemFactory.DATETIME).build();
}

View File

@ -23,4 +23,5 @@ profile.config.system.timestamp-offset.offset.label = Offset
profile.config.system.timestamp-offset.offset.description = Offset to be applied on the state towards the item. The negative offset will be applied in the reverse direction.
profile.config.system.timestamp-offset.timezone.label = Time Zone
profile.config.system.timestamp-offset.timezone.description = A time zone to be applied on the state.
profile-type.system.timestamp-trigger.label = Timestamp on Trigger
profile-type.system.timestamp-update.label = Timestamp on Update

View File

@ -0,0 +1,52 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.thing.internal.profiles;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.thing.CommonTriggerEvents;
import org.openhab.core.thing.profiles.ProfileCallback;
import org.openhab.core.thing.profiles.TriggerProfile;
import org.openhab.core.types.State;
/**
* @author Christoph Weitkamp - Initial contribution
*/
@NonNullByDefault
public class TimestampTriggerProfileTest {
@Test
public void testTimestampOnTrigger() {
ProfileCallback callback = mock(ProfileCallback.class);
TriggerProfile profile = new TimestampTriggerProfile(callback);
ZonedDateTime now = ZonedDateTime.now();
profile.onTriggerFromHandler(CommonTriggerEvents.PRESSED);
ArgumentCaptor<State> capture = ArgumentCaptor.forClass(State.class);
verify(callback, times(1)).sendUpdate(capture.capture());
State result = capture.getValue();
DateTimeType updateResult = (DateTimeType) result;
ZonedDateTime timestamp = updateResult.getZonedDateTime();
long difference = ChronoUnit.MINUTES.between(now, timestamp);
assertTrue(difference < 1);
}
}

View File

@ -71,7 +71,7 @@ public class SystemProfileFactoryOSGiTest extends JavaOSGiTest {
@Test
public void systemProfileTypesAndUidsShouldBeAvailable() {
Collection<ProfileTypeUID> systemProfileTypeUIDs = profileFactory.getSupportedProfileTypeUIDs();
assertThat(systemProfileTypeUIDs, hasSize(19));
assertThat(systemProfileTypeUIDs, hasSize(20));
Collection<ProfileType> systemProfileTypes = profileFactory.getProfileTypes(null);
assertThat(systemProfileTypes, hasSize(systemProfileTypeUIDs.size()));