Add system profile for a 'rawbutton-on-off-switch' (#1131)

* Add `rawbutton-on-off-switch` profile and tests

Signed-off-by: Mark Hilbush <mark@hilbush.com>
pull/1135/head
Mark Hilbush 2019-10-14 15:22:18 -05:00 committed by Christoph Weitkamp
parent d8a0225b48
commit 8396a17d2d
5 changed files with 129 additions and 11 deletions

View File

@ -0,0 +1,57 @@
/**
* Copyright (c) 2010-2019 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.eclipse.smarthome.core.thing.internal.profiles;
import static org.eclipse.smarthome.core.thing.profiles.SystemProfiles.RAWBUTTON_ON_OFF_SWITCH;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.thing.CommonTriggerEvents;
import org.eclipse.smarthome.core.thing.profiles.ProfileCallback;
import org.eclipse.smarthome.core.thing.profiles.ProfileTypeUID;
import org.eclipse.smarthome.core.thing.profiles.TriggerProfile;
import org.eclipse.smarthome.core.types.State;
/**
* The {@link RawButtonOnOffSwitchProfile} transforms raw button switch
* channel events into ON and OFF commands.
*
* @author Mark Hilbush - Initial contribution
*/
@NonNullByDefault
public class RawButtonOnOffSwitchProfile implements TriggerProfile {
private final ProfileCallback callback;
RawButtonOnOffSwitchProfile(ProfileCallback callback) {
this.callback = callback;
}
@Override
public ProfileTypeUID getProfileTypeUID() {
return RAWBUTTON_ON_OFF_SWITCH;
}
@Override
public void onStateUpdateFromItem(State state) {
}
@Override
public void onTriggerFromHandler(String event) {
if (CommonTriggerEvents.PRESSED.equals(event)) {
callback.sendCommand(OnOffType.ON);
} else if (CommonTriggerEvents.RELEASED.equals(event)) {
callback.sendCommand(OnOffType.OFF);
}
}
}

View File

@ -65,17 +65,17 @@ public class SystemProfileFactory implements ProfileFactory, ProfileAdvisor, Pro
private final ChannelTypeRegistry channelTypeRegistry;
private static final Set<ProfileType> SUPPORTED_PROFILE_TYPES = Stream
.of(DEFAULT_TYPE, FOLLOW_TYPE, OFFSET_TYPE, RAWBUTTON_TOGGLE_PLAYER_TYPE, RAWBUTTON_TOGGLE_PLAYER_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_UPDATE_TYPE)
.collect(Collectors.toSet());
private static final Set<ProfileType> SUPPORTED_PROFILE_TYPES = Stream.of(DEFAULT_TYPE, FOLLOW_TYPE, OFFSET_TYPE,
RAWBUTTON_ON_OFF_SWITCH_TYPE, RAWBUTTON_TOGGLE_PLAYER_TYPE, RAWBUTTON_TOGGLE_PLAYER_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_UPDATE_TYPE).collect(Collectors.toSet());
private static final Set<ProfileTypeUID> SUPPORTED_PROFILE_TYPE_UIDS = Stream.of(DEFAULT, FOLLOW, OFFSET,
RAWBUTTON_TOGGLE_PLAYER, RAWBUTTON_TOGGLE_PLAYER, 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_UPDATE).collect(Collectors.toSet());
RAWBUTTON_ON_OFF_SWITCH, RAWBUTTON_TOGGLE_PLAYER, RAWBUTTON_TOGGLE_PLAYER, 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_UPDATE)
.collect(Collectors.toSet());
private final Map<LocalizedKey, @Nullable ProfileType> localizedProfileTypeCache = new ConcurrentHashMap<>();
@ -100,6 +100,8 @@ public class SystemProfileFactory implements ProfileFactory, ProfileAdvisor, Pro
return new SystemFollowProfile(callback);
} else if (OFFSET.equals(profileTypeUID)) {
return new SystemOffsetProfile(callback, context);
} else if (RAWBUTTON_ON_OFF_SWITCH.equals(profileTypeUID)) {
return new RawButtonOnOffSwitchProfile(callback);
} else if (RAWBUTTON_TOGGLE_SWITCH.equals(profileTypeUID)) {
return new RawButtonToggleSwitchProfile(callback);
} else if (RAWBUTTON_TOGGLE_PLAYER.equals(profileTypeUID)) {

View File

@ -29,6 +29,7 @@ public interface SystemProfiles {
ProfileTypeUID DEFAULT = new ProfileTypeUID(SYSTEM_SCOPE, "default");
ProfileTypeUID FOLLOW = new ProfileTypeUID(SYSTEM_SCOPE, "follow");
ProfileTypeUID OFFSET = new ProfileTypeUID(SYSTEM_SCOPE, "offset");
ProfileTypeUID RAWBUTTON_ON_OFF_SWITCH = new ProfileTypeUID(SYSTEM_SCOPE, "rawbutton-on-off-switch");
ProfileTypeUID RAWBUTTON_TOGGLE_PLAYER = new ProfileTypeUID(SYSTEM_SCOPE, "rawbutton-toggle-player");
ProfileTypeUID RAWBUTTON_TOGGLE_ROLLERSHUTTER = new ProfileTypeUID(SYSTEM_SCOPE, "rawbutton-toggle-rollershutter");
ProfileTypeUID RAWBUTTON_TOGGLE_SWITCH = new ProfileTypeUID(SYSTEM_SCOPE, "rawbutton-toggle-switch");
@ -50,6 +51,11 @@ public interface SystemProfiles {
.withSupportedItemTypes(CoreItemFactory.NUMBER).withSupportedItemTypesOfChannel(CoreItemFactory.NUMBER)
.build();
TriggerProfileType RAWBUTTON_ON_OFF_SWITCH_TYPE = ProfileTypeBuilder
.newTrigger(RAWBUTTON_ON_OFF_SWITCH, "Raw Button On Off Switch")
.withSupportedItemTypes(CoreItemFactory.SWITCH, CoreItemFactory.DIMMER, CoreItemFactory.COLOR)
.withSupportedChannelTypeUIDs(DefaultSystemChannelTypeProvider.SYSTEM_RAWBUTTON.getUID()).build();
TriggerProfileType RAWBUTTON_TOGGLE_PLAYER_TYPE = ProfileTypeBuilder
.newTrigger(RAWBUTTON_TOGGLE_PLAYER, "Raw Button Toggle Player")
.withSupportedItemTypes(CoreItemFactory.PLAYER)
@ -102,5 +108,4 @@ public interface SystemProfiles {
StateProfileType TIMESTAMP_UPDATE_TYPE = ProfileTypeBuilder.newState(TIMESTAMP_UPDATE, "Timestamp on update")
.withSupportedItemTypes(CoreItemFactory.DATETIME).build();
}

View File

@ -0,0 +1,54 @@
/**
* Copyright (c) 2010-2019 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.eclipse.smarthome.core.thing.internal.profiles;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.thing.CommonTriggerEvents;
import org.eclipse.smarthome.core.thing.profiles.ProfileCallback;
import org.eclipse.smarthome.core.thing.profiles.TriggerProfile;
import org.eclipse.smarthome.core.types.Command;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
/**
* Tests for the system:rawbutton-on-off-switch profile
*
* @author Mark Hilbush - Initial contribution
*/
public class RawButtonOnOffSwitchProfileTest {
@Mock
private ProfileCallback mockCallback;
@Before
public void setup() {
mockCallback = mock(ProfileCallback.class);
}
@Test
public void testOnOffSwitchItem() {
TriggerProfile profile = new RawButtonOnOffSwitchProfile(mockCallback);
verifyAction(profile, CommonTriggerEvents.PRESSED, OnOffType.ON);
verifyAction(profile, CommonTriggerEvents.RELEASED, OnOffType.OFF);
}
private void verifyAction(TriggerProfile profile, String trigger, Command expectation) {
reset(mockCallback);
profile.onTriggerFromHandler(trigger);
verify(mockCallback, times(1)).sendCommand(eq(expectation));
}
}

View File

@ -47,7 +47,7 @@ public class SystemProfileFactoryOSGiTest extends JavaOSGiTest {
@Test
public void systemProfileTypesShouldBeAvailable() {
Collection<ProfileType> systemProfileTypes = profileFactory.getProfileTypes(null);
assertEquals(14, systemProfileTypes.size());
assertEquals(15, systemProfileTypes.size());
}
@Test