From cc04127a36d25dcd552ed7a68263106aad28672d Mon Sep 17 00:00:00 2001 From: Stefan Triller Date: Fri, 19 May 2017 11:21:34 +0200 Subject: [PATCH 1/4] BasicUI: Treat Switch on NumberItem not as ON/OFF Switch Signed-off-by: Stefan Triller --- .../smarthome/ui/internal/items/ItemUIRegistryImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java b/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java index 1bb4aaa5e53..9a10050682e 100644 --- a/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java +++ b/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java @@ -498,7 +498,8 @@ private State convertState(Widget w, Item i) { // RollerShutter are represented as Switch in a Sitemap but need a PercentType state if (w instanceof Slider || (w instanceof Switch && i instanceof RollershutterItem)) { returnState = i.getStateAs(PercentType.class); - } else if (w instanceof Switch) { + // if a Switch is attached to a Number item we do not want to convert it here + } else if (w instanceof Switch && !(i instanceof NumberItem)) { returnState = i.getStateAs(OnOffType.class); } From a5e1b0a957037828237e0fe474576455291bb78c Mon Sep 17 00:00:00 2001 From: Stefan Triller Date: Fri, 19 May 2017 16:09:51 +0200 Subject: [PATCH 2/4] Only convert state of plain switches without mappings Signed-off-by: Stefan Triller --- .../smarthome/ui/internal/items/ItemUIRegistryImpl.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java b/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java index 9a10050682e..337522e7e0b 100644 --- a/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java +++ b/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java @@ -498,9 +498,11 @@ private State convertState(Widget w, Item i) { // RollerShutter are represented as Switch in a Sitemap but need a PercentType state if (w instanceof Slider || (w instanceof Switch && i instanceof RollershutterItem)) { returnState = i.getStateAs(PercentType.class); - // if a Switch is attached to a Number item we do not want to convert it here - } else if (w instanceof Switch && !(i instanceof NumberItem)) { - returnState = i.getStateAs(OnOffType.class); + } else if (w instanceof Switch) { + Switch sw = (Switch) w; + if (sw.getMappings().size() == 0) { // only convert plain switches with no mappings + returnState = i.getStateAs(OnOffType.class); + } } // if returnState is null, a conversion was not possible From beb8f0d49bdb56f54be73625b28074423b5edd21 Mon Sep 17 00:00:00 2001 From: Stefan Triller Date: Mon, 22 May 2017 11:16:02 +0200 Subject: [PATCH 3/4] Fix exception in test due to new check in implementation Signed-off-by: Stefan Triller --- .../smarthome/ui/internal/items/ItemUIRegistryImplTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bundles/ui/org.eclipse.smarthome.ui.test/src/test/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImplTest.java b/bundles/ui/org.eclipse.smarthome.ui.test/src/test/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImplTest.java index 8f41e3f9a51..33c1c8b4bb1 100644 --- a/bundles/ui/org.eclipse.smarthome.ui.test/src/test/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImplTest.java +++ b/bundles/ui/org.eclipse.smarthome.ui.test/src/test/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImplTest.java @@ -13,6 +13,7 @@ import java.text.DecimalFormatSymbols; +import org.eclipse.emf.common.util.BasicEList; import org.eclipse.smarthome.core.items.Item; import org.eclipse.smarthome.core.items.ItemNotFoundException; import org.eclipse.smarthome.core.items.ItemRegistry; @@ -26,6 +27,7 @@ import org.eclipse.smarthome.core.types.State; import org.eclipse.smarthome.core.types.StateDescription; import org.eclipse.smarthome.core.types.UnDefType; +import org.eclipse.smarthome.model.sitemap.Mapping; import org.eclipse.smarthome.model.sitemap.Sitemap; import org.eclipse.smarthome.model.sitemap.SitemapFactory; import org.eclipse.smarthome.model.sitemap.Slider; @@ -328,6 +330,7 @@ public void testStateConversionForSwitchWidgetThroughGetState() throws ItemNotFo Switch switchWidget = mock(Switch.class); when(switchWidget.getItem()).thenReturn("myItem"); + when(switchWidget.getMappings()).thenReturn(new BasicEList()); State stateForSwitch = uiRegistry.getState(switchWidget); From 9c96499d9e86f21da870b0541ee8ad4ccb82f1dd Mon Sep 17 00:00:00 2001 From: Stefan Triller Date: Mon, 22 May 2017 15:52:45 +0200 Subject: [PATCH 4/4] Test for state conversion on switch with mapping Signed-off-by: Stefan Triller --- .../items/ItemUIRegistryImplTest.java | 23 +++++++++++++++++++ .../ui/internal/items/ItemUIRegistryImpl.java | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/bundles/ui/org.eclipse.smarthome.ui.test/src/test/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImplTest.java b/bundles/ui/org.eclipse.smarthome.ui.test/src/test/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImplTest.java index 33c1c8b4bb1..23cdcd60c06 100644 --- a/bundles/ui/org.eclipse.smarthome.ui.test/src/test/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImplTest.java +++ b/bundles/ui/org.eclipse.smarthome.ui.test/src/test/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImplTest.java @@ -337,6 +337,29 @@ public void testStateConversionForSwitchWidgetThroughGetState() throws ItemNotFo assertEquals(OnOffType.ON, stateForSwitch); } + @Test + public void testStateConversionForSwitchWidgetWithMappingThroughGetState() throws ItemNotFoundException { + State colorState = new HSBType("23,42,50"); + + ColorItem colorItem = new ColorItem("myItem"); + colorItem.setLabel("myItem"); + colorItem.setState(colorState); + + when(registry.getItem("myItem")).thenReturn(colorItem); + + Switch switchWidget = mock(Switch.class); + when(switchWidget.getItem()).thenReturn("myItem"); + + Mapping mapping = mock(Mapping.class); + BasicEList mappings = new BasicEList(); + mappings.add(mapping); + when(switchWidget.getMappings()).thenReturn(mappings); + + State stateForSwitch = uiRegistry.getState(switchWidget); + + assertEquals(colorState, stateForSwitch); + } + @Test public void testStateConversionForSliderWidgetThroughGetState() throws ItemNotFoundException { State colorState = new HSBType("23,42,75"); diff --git a/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java b/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java index 337522e7e0b..afdc2a41fc7 100644 --- a/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java +++ b/bundles/ui/org.eclipse.smarthome.ui/src/main/java/org/eclipse/smarthome/ui/internal/items/ItemUIRegistryImpl.java @@ -500,7 +500,7 @@ private State convertState(Widget w, Item i) { returnState = i.getStateAs(PercentType.class); } else if (w instanceof Switch) { Switch sw = (Switch) w; - if (sw.getMappings().size() == 0) { // only convert plain switches with no mappings + if (sw.getMappings().size() == 0) { returnState = i.getStateAs(OnOffType.class); } }