From 8396a17d2d444f15a79a963711f5c902aac86dd7 Mon Sep 17 00:00:00 2001 From: Mark Hilbush Date: Mon, 14 Oct 2019 15:22:18 -0500 Subject: [PATCH] Add system profile for a 'rawbutton-on-off-switch' (#1131) * Add `rawbutton-on-off-switch` profile and tests Signed-off-by: Mark Hilbush --- .../profiles/RawButtonOnOffSwitchProfile.java | 57 +++++++++++++++++++ .../profiles/SystemProfileFactory.java | 20 ++++--- .../core/thing/profiles/SystemProfiles.java | 7 ++- .../RawButtonOnOffSwitchProfileTest.java | 54 ++++++++++++++++++ .../SystemProfileFactoryOSGiTest.java | 2 +- 5 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/RawButtonOnOffSwitchProfile.java create mode 100644 bundles/org.openhab.core.thing/src/test/java/org/eclipse/smarthome/core/thing/internal/profiles/RawButtonOnOffSwitchProfileTest.java diff --git a/bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/RawButtonOnOffSwitchProfile.java b/bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/RawButtonOnOffSwitchProfile.java new file mode 100644 index 0000000000..3fecde29dc --- /dev/null +++ b/bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/RawButtonOnOffSwitchProfile.java @@ -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); + } + } +} diff --git a/bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/SystemProfileFactory.java b/bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/SystemProfileFactory.java index 0a2e83b53c..24f394a206 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/SystemProfileFactory.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/SystemProfileFactory.java @@ -65,17 +65,17 @@ public class SystemProfileFactory implements ProfileFactory, ProfileAdvisor, Pro private final ChannelTypeRegistry channelTypeRegistry; - private static final Set 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 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 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 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)) { diff --git a/bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/profiles/SystemProfiles.java b/bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/profiles/SystemProfiles.java index 2d62ed039a..a95930b3a1 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/profiles/SystemProfiles.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/eclipse/smarthome/core/thing/profiles/SystemProfiles.java @@ -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(); - } diff --git a/bundles/org.openhab.core.thing/src/test/java/org/eclipse/smarthome/core/thing/internal/profiles/RawButtonOnOffSwitchProfileTest.java b/bundles/org.openhab.core.thing/src/test/java/org/eclipse/smarthome/core/thing/internal/profiles/RawButtonOnOffSwitchProfileTest.java new file mode 100644 index 0000000000..4f34422742 --- /dev/null +++ b/bundles/org.openhab.core.thing/src/test/java/org/eclipse/smarthome/core/thing/internal/profiles/RawButtonOnOffSwitchProfileTest.java @@ -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)); + } +} diff --git a/itests/org.openhab.core.thing.tests/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/SystemProfileFactoryOSGiTest.java b/itests/org.openhab.core.thing.tests/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/SystemProfileFactoryOSGiTest.java index 9a2d5c14a3..5b172c28f3 100644 --- a/itests/org.openhab.core.thing.tests/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/SystemProfileFactoryOSGiTest.java +++ b/itests/org.openhab.core.thing.tests/src/main/java/org/eclipse/smarthome/core/thing/internal/profiles/SystemProfileFactoryOSGiTest.java @@ -47,7 +47,7 @@ public class SystemProfileFactoryOSGiTest extends JavaOSGiTest { @Test public void systemProfileTypesShouldBeAvailable() { Collection systemProfileTypes = profileFactory.getProfileTypes(null); - assertEquals(14, systemProfileTypes.size()); + assertEquals(15, systemProfileTypes.size()); } @Test