-
-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit hint in thing channels #4079
Conversation
Signed-off-by: Mark Herwege <[email protected]>
Signed-off-by: Mark Herwege <[email protected]>
I was thinking if this is sufficient to cover the requirements. First I just want to make an observation that Number:Dimensionless is really a pretty vague class / concept which actually has several sub-classes in physical reality as follows.
Anyway, to summarise, I think this would have the ability to cover all those needs. When creating an item, the MainUI would have to offer a selection of a) all the most likely units suggested in the bullet points above, plus b) the unit hint proposed in this PR, plus c) a 'custom' option allowing the user to manually enter some other unit. |
Apropos MainUI when creating a new item (see below) I have three suggestions..
|
Signed-off-by: Mark Herwege <[email protected]>
I don't think this can easily be generalized. Think about all the scale modifiers (micro, milli, centi, deca, hecto, kilo...). I know this is not relevant for Dimensionless, but I want to start with a general solution first, before trying to become specific. |
Do you know if this would also be the case if added as an attribute to the <item-type unitHint="%">Number:Dimensionless</item-type> I don't know if it makes any difference, but it seems somehow like a smaller change. |
I agree. 'item-type" is the most obvious and logical holder for this attribute. |
It doesn't make a difference (and is actually a bigger change, as item-type is now a String node and would have to be converted in an object). |
OK, will look into modifying it to be an attribute to item-type. |
Signed-off-by: Mark Herwege <[email protected]>
@mherwege do you need to modify Rest API to include the new field JSON? But to answer my own question, I suppose GSON does that automatically.. |
GSON does, but it needed to be added to the ChannelTypeResource in the Rest API package. I tested with a modified UI from the linked PR (one example), and it was available in the UI and REST API. |
I replaced the default pattern with
There is no predefined list of allowed units (with their modifiers) I can query from the Rest API. So this is not easy to solve immediately and is subject for another potential enhancement.
Could be done, however, without a list of potential units, nobody will know they have to set one if they want it. It is not a natural thing. % is more obvious to set explicitely. |
Did you consider also this scenario: <channel-type id="water-consumption">
<item-type unitHint="l,gal">Number:Volume</item-type>
<label>Water Consumption</label>
<description>Water consumption by the currently running program on the appliance</description>
<category>Water</category>
<tags>
<tag>Measurement</tag>
<tag>Water</tag>
</tags>
<state readOnly="true" pattern="%.1f %unit%"/>
</channel-type> where it should default either "l" or "gal" depending on configured measurement system? |
I considered it and created the code for it, but I must say I didn't explicitely test it. |
That is why I was thinking of a drop down plus 'custom' input. Both one and % could be in the standard dropdown (with unit-hint preselected or % if unit-hint == null). The other standard options would be ppM, ppB, and dB. Any other unit, including those with milli, kilo, prefixes etc. could still be entered via the 'custom' input.. EDIT: the standard units being those here... |
This makes sense for Dimensionless. But if you check the list of 'standard' units, you will notice it actually only lists exceptions not covered out of the box by the libraries. For instance, it creates mm/h, but km/h comes from the libraries. So the code you link is by no way exhaustive. Also, it would have to be created manually in the UI code. |
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.
Assuming that openhab/openhab-webui#2312 can be approved without requiring further changes in this PR => LGTM
BUT -- just a reminder -- the following things will also need to be done..
- Upload the revised XSD schema file to the OH main server. Otherwise the mvn build process for addons will fail due to a schema violation..
- Update the respective addon developer readMe entry.
Closes openhab/openhab-core#4082. This PR adds: 1. A curated list of units to show as a drop-down list when creating a Number item with dimension. 2. The possibility to use a different default unit on item creation than the system default unit. 3. The ability to change unit and state description for items. 4. The ability to use the `unitHint` provided by channel types for "Link channel to Item" -> "Create a new Item". By default, the system default unit (for the configured measurement system) will be shown when editing or creating an item. `units.js` contains a number of frequently used units by dimension and measurement system. These will be available in a autosuggest dropdown list. It is still possible to not select from the list and use any other string as a unit. All units for the dimension in `units.js` will be in the dropdown list, but they will be sorted by measurement system. If the measurement system is set to US, imperial units will appear higher in the list. Units that have not explicitely been listed as SI or US will always appear higher. When typing a unit that is not in the curated list, a longer list will be used for autocompletion that considers allowed prefixes to base units and constructs all combinations. `units.js` also contains a field to set a different default unit on item creation than the system default unit. With openhab/openhab-core#4079, the REST API of channel types will provide a unit hint if defined in the binding channel types. If such information is available, this PR adds support for this to be the the suggested unit. If that information is unavailable, the UI will fall back to behavious described above. --------- Also-by: Florian Hotze <[email protected]> Signed-off-by: Mark Herwege <[email protected]>
@mherwege given that the UI change has now been merged, I think this one must be merged too. Or?? |
Would be nice, but the UI PR provides extra functionality without this already. Merging this PR will enable binding developers to include a unit hint in their channel definitions and the merged UI PR will consider it. There would be no change required to the UI PR. |
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/units-are-not-propagated-when-creating-items/154748/6 |
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/inconsistent-percentage-behaviour/154595/40 |
@@ -83,6 +86,7 @@ protected ChannelType(ChannelTypeUID uid, boolean advanced, @Nullable String ite | |||
} | |||
|
|||
this.itemType = itemType; | |||
this.unitHint = unitHint; |
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.
In general LGTM. Would it make sense to throw an IAE here when the item-type is not Number:*
(similar to l. 85 above)?
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.
Indeed, makes sense. Done.
Signed-off-by: Mark Herwege <[email protected]>
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!
@mherwege / @J-N-K see this openhab/openhab-docs#2283 |
@J-N-K - can you upload the new schema or is it only @kaikreuzer who can do that? |
I may have missed this, but did you analyze and find any impact on backwards/forwards compatibility? |
Signed-off-by: Kai Kreuzer <[email protected]>
Signed-off-by: Kai Kreuzer <[email protected]>
@jlaur It's very simple - you only need to create a PR against https://github.com/openhab/website/tree/main/.vuepress/public/schemas. |
Thanks, and good to know! I assume some website build job is needed before the update is available here? |
I see it's already available now. 🙂 |
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/unithint-error-in-xml-schema/156558/3 |
Resolves #3854
To help the user in configuring the right unit when creating items from thing channels, one of the suggestions was to use a unitHint that could be provided by the binding developer. This PR implements the core part of this:
unit-hint
field to the channel type.unit-hint
field in the REST API.The second part of this will have to be extending mainUI to use the
unit-hint
as proposed unit in configuration when available.This can be especially useful for channels that provide % values, where QuantityType:Dimensionless has a default of Units.ONE. Note that an alternative solution with changing the default unit for QuantityType:Dimensionless has not gained support (#4077 and #4078).
This PR does help for users creating new items from channels in mainUI. It will not help for users using textual configuration.
For dimensions with different units in SI or US measurement systems, it is possible to provide 2 unit hints in one string separated by comma. The first one will be SI, the second US. The suggested one in the UI will depend on the measurement system setting.
This PR extends the
thing-description-1.0.0.xsd
and adds a new attribute to the input-type. Further investigation is required, as this may make it impossible to run bindings that have this field added on an older core (parsing the xml may fail). This is very similar to the situation described in #4062 for a change inaddon.xml
. To limit issues with newer bindings on older cores (going forward from 4.2), this may have to be solved in parallel.