Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
fix GenerericItem.getStateAs for non-Convertibles (#2393)
Browse files Browse the repository at this point in the history
When the target type is identical to the current state's
type, then the current state should be returned.

fixes #2334
Signed-off-by: Simon Kaufmann <simon.kfm@googlemail.com>
  • Loading branch information
Simon Kaufmann authored and kaikreuzer committed Nov 3, 2016
1 parent 9393013 commit 093a547
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import org.eclipse.smarthome.core.events.EventPublisher
import org.eclipse.smarthome.core.items.events.ItemEventFactory
import org.eclipse.smarthome.core.items.events.ItemStateChangedEvent
import org.eclipse.smarthome.core.items.events.ItemStateEvent
import org.eclipse.smarthome.core.library.types.OnOffType
import org.eclipse.smarthome.core.library.types.PercentType
import org.eclipse.smarthome.core.library.types.RawType
import org.eclipse.smarthome.core.library.types.StringType
import org.junit.Before
import org.junit.Test

Expand Down Expand Up @@ -87,4 +90,34 @@ class GenericItemTest {
def item = new TestItem("member1")
item.removeGroupName(null)
}

@Test
void 'assert that getStateAs works with the same type for a Convertible'() {
def item = new TestItem("member1")
item.setState(PercentType.HUNDRED)
assertThat item.getStateAs(PercentType), isA(PercentType)
}

@Test
void 'assert that getStateAs works with a different type for a Convertible'() {
def item = new TestItem("member1")
item.setState(PercentType.HUNDRED)
assertThat item.getStateAs(OnOffType), isA(OnOffType)
}

@Test
void 'assert that getStateAs works with the same type for a non-Convertible'() {
def item = new TestItem("member1")
item.setState(StringType.valueOf("Hello World"))
assertThat item.getStateAs(StringType), isA(StringType)
}

@Test
void 'assert that getStateAs works with null'() {
def item = new TestItem("member1")
item.setState(StringType.valueOf("Hello World"))
assertThat item.getStateAs(null), is(nullValue())
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.smarthome.core.common.ThreadPoolManager;
import org.eclipse.smarthome.core.events.EventPublisher;
import org.eclipse.smarthome.core.items.events.ItemEventFactory;
import org.eclipse.smarthome.core.library.internal.StateConverterUtil;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.Convertible;
import org.eclipse.smarthome.core.types.RefreshType;
Expand Down Expand Up @@ -94,7 +95,7 @@ public State getStateAs(Class<? extends State> typeClass) {
if (state instanceof Convertible) {
return ((Convertible) state).as(typeClass);
} else {
return null;
return StateConverterUtil.defaultConversion(state, typeClass);
}
}

Expand Down

0 comments on commit 093a547

Please sign in to comment.