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

UI: provide a default pattern for string and number channels if not defined #3822

Merged
merged 3 commits into from Jul 17, 2017
Merged

UI: provide a default pattern for string and number channels if not defined #3822

merged 3 commits into from Jul 17, 2017

Conversation

lolodomo
Copy link
Contributor

@lolodomo lolodomo commented Jul 10, 2017

Fix issue #3741

Signed-off-by: Laurent Garnier lg.hc@free.fr

Copy link
Contributor

@sjsf sjsf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for providing this PR so quickly!
Nevertheless, I would prefer to keep separate things separate and rather split them up into several PRs.

Also I'm missing some test-cases which prevent any regressions - so could you please put down the new assumptions into ChannelStateDescriptionProviderOSGiTest?

}
}
if (found) {
fullStateDescription = new StateDescription(minimum, maximum, step, pattern, readOnly, options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is to mix up state descriptions of different providers, right?
This will lead to quite unexpected results, therefore I don't think this is a good idea. Also I wouldn't want to mix this into a PR which intends to add a default state description at all (or I did not understand how it is related).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is exactly the idea. It gives us a chance to get options from channel for example. I could add a comment.
What are the unexpected results ?
Do you prefer I keep this method unchanged and i create another one with that code ?
My PR is clearly to fix two different things that are more or less linked to state description. Splitting in two PRs means more work, more time. But ok, I will take the time !

@@ -307,6 +309,14 @@ public String getLabel(Widget w) {
}
} else {
state = item.getState();
if (stateDescription != null && stateDescription.getOptions() != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are the changes within this file related to the PR? I would not have expected any changes needed in the UI layer for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the fix for options. If you have options defined for a string channel, geLabel(w) should translate the value to its label. By the way, I think my code is not ewactly at the right place, I have to consider the case where the user requests for a transformation.

pattern = "%.0f";
}
if (pattern != null) {
logger.debug("Provide a default pattern {} for item {}", pattern, itemName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please reduce it to trace level

snippet = StringUtils.replace(snippet, "%label%", getLabel(w));
snippet = StringUtils.replace(snippet, "%value%", getValue(w));
snippet = StringUtils.replace(snippet, "%has_value%", new Boolean(hasValue(w)).toString());
// Optimization: avoid calling 3 times itemUIRegistry.getLabel(w)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree this makes sense, I also would have preferred to keep it separate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will create a separate PR !

@lolodomo lolodomo changed the title UI: handle channel options and provide a default pattern UI: provide a default pattern for string and number channels if not defined Jul 10, 2017
@lolodomo
Copy link
Contributor Author

@SJKA : split done, you have here only the fix for issue #3741

@lolodomo
Copy link
Contributor Author

Regarding ChannelStateDescriptionProviderOSGiTest, my Eclipse IDE is showing an error at this line: https://github.com/eclipse/smarthome/blob/master/bundles/core/org.eclipse.smarthome.core.thing.test/src/test/java/org/eclipse/smarthome/core/thing/internal/ChannelStateDescriptionProviderOSGiTest.java#L76
JavaOSGiTest.bundleContext is not visible.

Same error in TranslationProviderOSGiTest.

Is there something broken with current tests ?

@lolodomo
Copy link
Contributor Author

mvn clean install on org.eclipse.smarthome.core.thing.test succeeeded.
Should I just ignore the error in Eclipse ?

@lolodomo
Copy link
Contributor Author

Tests have been added.

Copy link
Contributor

@htreu htreu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a quick review, please find some minor remarks in the java test. Which btw is awesome to have! Thanks for that.

Item item;
try {
item = itemRegistry.getItem("TestItem");
} catch (ItemNotFoundException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test could also be declared as throws Exception and fail happily if this happens. that would reduce lines 218 - 223 to Item item = itemRegistry.getItem("TestItem");.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I do that, the next tests will not be run. Not a problem ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next assertions in this test method will not run, but as this aspect fails anyway this is not a problem. The other test methods should be executed individually.

@@ -182,6 +238,77 @@ public void presentItemStateDescription() {
Assert.assertEquals("SOUND", opt.getValue());
Assert.assertEquals("My great sound.", opt.getLabel());

try {
item = itemRegistry.getItem("TestItem2");
} catch (ItemNotFoundException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@@ -153,22 +193,38 @@ public void presentItemStateDescription() {
null, "test thing", new Configuration());
Assert.assertNotNull(thing);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have all assert* methods to be static imports like in the other java tests.

}
Assert.assertNotNull(numberItem);
Assert.assertNotNull(item);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obsolete since the next line will fail if item is null. Please also review other assertNotNull checks.

…efined

Fix issue #3741

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
@lolodomo
Copy link
Contributor Author

Ok, tests are now fixed according to your suggestions.

@lolodomo
Copy link
Contributor Author

The Travis error has apparently nothing to do with my changes.

@lolodomo
Copy link
Contributor Author

To be very clear, this PR changes the current behaviour.

The old behavior is to display a value for a string or number item only if a pattern is found either in the sitemap or in the item or in the thing channel, and only if there is no [] in the label at the first level. It could lead to no display of value, a frequent case for users using Paper UI to automatically link channels to items.

The new behavior is to always display a value for a string or number item except if there is a [] in the label at the first level. The pattern is taken first in the sitemap then in the item then in the thing channel and finally a default one is considered. The default one is [%s] for string item and [%.0f] for number items.

There is no change for other kinds of items.

@lolodomo
Copy link
Contributor Author

Maybe we should consider a default pattern for DateTime items too.

@lolodomo
Copy link
Contributor Author

lolodomo commented Jul 14, 2017

Are you still waiting for me on this PR ? I think I handled all requests ?

@sjsf
Copy link
Contributor

sjsf commented Jul 14, 2017

Thanks for the ping. No, the change looks good to me now.
Due to it's nature of changing the current behavior (to the better in my belief though), I would like to give @kaikreuzer / @maggu2810 also a change to express their consent here.

Copy link
Contributor

@kaikreuzer kaikreuzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good to me, just a few very minor remarks.

StateDescription state = channelType.getState();
if ((channelType.getItemType() != null) && ((state == null) || (state.getPattern() == null))) {
String pattern = null;
if (channelType.getItemType().equalsIgnoreCase("String")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use the constant CoreItemFactory.STRING.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

org.eclipse.smarthome.core.library is not exported.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. This is against our own guidelines, so feel free to add this export as part of this PR :-)

String pattern = null;
if (channelType.getItemType().equalsIgnoreCase("String")) {
pattern = "%s";
} else if (channelType.getItemType().equalsIgnoreCase("Number")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use the constant CoreItemFactory.NUMBER.

@@ -57,7 +61,29 @@ public StateDescription getStateDescription(String itemName, Locale locale) {
Channel channel = thingRegistry.getChannel(channelUID);
if (channel != null) {
ChannelType channelType = thingTypeRegistry.getChannelType(channel, locale);
return channelType != null ? channelType.getState() : null;
if (channelType != null) {
StateDescription state = channelType.getState();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you call the variable stateDescription? (I know that the name of the getter method of ChannelType is not ideal...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
@lolodomo
Copy link
Contributor Author

Done

@sjsf sjsf merged commit ca531c4 into eclipse-archived:master Jul 17, 2017
@sjsf
Copy link
Contributor

sjsf commented Jul 17, 2017

Great, thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants