openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

[items] Add a "rawItem" attribute to Item similar to rawState #162

Closed rkoshak closed 1 year ago

rkoshak commented 1 year ago

There are rare but useful circumstances where getting access to the raw Java Item Object is required. One example is when using the Semantic Actions (e.g. getEquipment(), getLocation(), etc.). These Actions require the Java Item Object and, at least based on my experience, there is nothing to autotranslate between the JS Item and Java Item like there is for ZonedDateTime.

So my thoughts are:

  1. Simply add a rawItem attribute/setter/getter to Item that returns the original Java Item Object. This will give the user access to it when it's needed without needing to import the ItemRegistry and pulling it from there.
  2. Add the magic to the add-on that auto translates between the Java and JS like is done for ZonedDateTime (if that's even possible)
  3. Add the semantic actions to the Item class itself similar to ItemHistory which solves my immediate problem but leaves open problems where the Java Item is required elsewhere that I don't know about yet.

I can implement 1 or 3 myself but I think 2 needs to be moved to the add-on.

Thoughts? What feels like the best approach. I really don't like needing to import the ItemRegistry to access these calls.

florian-h05 commented 1 year ago

Simply add a rawItem attribute/setter/getter to Item that returns the original Java Item Object. This will give the user access to it when it's needed without needing to import the ItemRegistry and pulling it from there.

This is the simplest solution, we can of course add access to the raw Java Item using a rawItem property, but ...

Add the semantic actions to the Item class itself similar to ItemHistory which solves my immediate problem but leaves open problems where the Java Item is required elsewhere that I don't know about yet.

... this would be the much cleaner way to access the Semantics.

Regarding 2., I can neither tell if that's possible nor how it is done with ZonedDateTime without looking it up, but that doesn't seem as an option to me.

If you come up with a PR providing solution 1, I'll merge it, but I would be happy to the see 3 implemented as well to avoid having the normal user work with the raw Java stuff.

rkoshak commented 1 year ago

I'll see what I can come up with. It might be a couple of weeks though. I'm on travel next week so my spare time is short. I need to pick back up the rules docs too and there is a big PR on one of my other repos I need to reveiw. 😭

florian-h05 commented 1 year ago

Maybe I'll come up with a PR earlier, but I also have some open PRs (one at your openhab-docs fork for the rule docs and one at your rules tools repo) to work on, so we'll see when this issue is fixed.

florian-h05 commented 1 year ago

@rkoshak I am working on implementing this now. One thing I noticed is, that an rawItem attribute is already there, it is just not documented.