UI: provide a default pattern for string and number channels if not defined #3822
Conversation
There was a problem hiding this 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); |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 !
Regarding Same error in Is there something broken with current tests ? |
|
Tests have been added. |
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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");
.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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>
Ok, tests are now fixed according to your suggestions. |
The Travis error has apparently nothing to do with my changes. |
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 The new behavior is to always display a value for a string or number item except if there is a There is no change for other kinds of items. |
Maybe we should consider a default pattern for DateTime items too. |
Are you still waiting for me on this PR ? I think I handled all requests ? |
Thanks for the ping. No, the change looks good to me now. |
There was a problem hiding this 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")) { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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")) { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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...)
There was a problem hiding this comment.
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>
Done |
Great, thanks! |
Fix issue #3741
Signed-off-by: Laurent Garnier lg.hc@free.fr