Add support for setting unit of measure when updating Number items #27
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@sim0nx, first of all, thank you for this great library for communicating with the OpenHAB REST API.
This PR adds support for updating OpenHAB Number items of QuantityType and setting the unit of measure. Currently, the library only supports retrieving the unit of measure. In my specific case, I was trying to update a Number:Temperature item that is stored in OpenHAB in Fahrenheit, using values from a sensor that only reports in Celsius. OpenHAB can handle the unit conversion as long as the string sent to the REST API includes the proper unit of measure, but
NumberItem.update()
could only be called with afloat
orint
. This PR allows theNumberItem.update()
andNumberItem.command()
methods to also accept a string of the form "[numeric value] [unit of measure]" or a tuple of(float, str)
to represent numbers with units.While adding this feature, I also discovered that the OpenHAB REST API does not support the full latin-1 encoding but only accepts ascii or utf-8. If the degree symbol (°) is encoded as latin-1 (as is the default when a
str
is provided as data to the underlying requests library), my OpenHAB server (v3.1.0) returned a Bad Request error when updating the Number item. However, encoding as utf-8 was fine. A similar problem occurred if a degree symbol was set in a String item: the value was accepted but improperly decoded. Therefore, I switched the check for non-latin-1 characters to check for non-ascii characters. We could also consider always encoding the final string to utf-8 for simplicity.Lastly, the unit of measure was also exposed in the
Item.__str__()
method.Thanks for considering this PR.