Jython openhab2-jython: adding and removing Items

First of all, I would like to thank everybody involved to openHAB and especially
those who made jsr223-scripting possible. While there are good points for openHAB DSL, there are some tasks where jsr223-scripting seems to be more appropriate.
I discovered this topic not long ago, so I consider myself a NewBie.

While adding/removing items works, when the Item name is provided as a hard-coded string, as it is done in


(note, that I am using a slightly modified version of the file, see my other post), it does not work, if those strings are in a variable:

itemName1 = "TestNumber1"

class MyUnitTest(unittest.TestCase):
    def setUp(self):
        openhab.items.add(itemName1, "Number")

causes the following Log output:

19:06:44.381 [INFO ] [ort.internal.loader.ScriptFileWatcher] - Loading script 'testing_example_2.py'
19:06:44.496 [INFO ] [smarthome.event.RuleAddedEvent       ] - Rule '8772a457-f4ab-4660-916e-7f4bc7281703' has been added.
19:06:44.498 [INFO ] [smarthome.event.RuleStatusInfoEvent  ] - 8772a457-f4ab-4660-916e-7f4bc7281703 updated: INITIALIZING
19:06:44.516 [INFO ] [smarthome.event.RuleStatusInfoEvent  ] - 8772a457-f4ab-4660-916e-7f4bc7281703 updated: IDLE
19:06:44.527 [ERROR] [ROOT                                 ] - {
  "run": 1,
  "errors": [{"name":"__builtin__.MyUnitTest.test_item", "stack":"Traceback (most recent call last):
  File \"<script>\", line 25, in setUp
  File \"/etc/openhab2/lib/python/openhab/items.py\", line 16, in add
    JythonItemProvider.add(item)
  File \"<script>\", line 14, in add
ClassCastException: java.lang.ClassCastException: java.lang.String cannot be cast to org.eclipse.smarthome.core.common.registry.Identifiable
"}],
  "failures": [],
  "skipped": []
}

Where line 25 from the Log entry is the line

        openhab.items.add(itemName1, "Number")

After this failed attempt to add an item, there is some entry in the item list of JythonItemProvider,
which prevents testing_example.py, which is working fine before running the version using a string variable, to remove its items, see Log output:

18:23:22.020 [INFO ] [smarthome.event.RuleAddedEvent       ] - Rule 'bc6fc96b-46ef-4d3e-90cb-0c544b0a50d6' has been added.
18:23:22.024 [INFO ] [smarthome.event.RuleStatusInfoEvent  ] - bc6fc96b-46ef-4d3e-90cb-0c544b0a50d6 updated: INITIALIZING
18:23:22.032 [INFO ] [smarthome.event.RuleStatusInfoEvent  ] - bc6fc96b-46ef-4d3e-90cb-0c544b0a50d6 updated: IDLE
18:23:22.067 [INFO ] [smarthome.event.ItemStateChangedEvent] - TestNumber1 changed from NULL to 5
18:23:22.070 [INFO ] [smarthome.event.RuleStatusInfoEvent  ] - bc6fc96b-46ef-4d3e-90cb-0c544b0a50d6 updated: RUNNING
18:23:22.074 [INFO ] [smarthome.event.RuleStatusInfoEvent  ] - bc6fc96b-46ef-4d3e-90cb-0c544b0a50d6 updated: IDLE
18:23:22.077 [INFO ] [smarthome.event.ItemStateChangedEvent] - TestNumber2 changed from NULL to 10.0
18:23:23.053 [ERROR] [ROOT                                 ] - {
  "run": 1,
  "errors": [{"name":"__builtin__.MyUnitTest.test_item", "stack":"Traceback (most recent call last):
  File \"<script>\", line 27, in tearDown
  File \"/etc/openhab2/lib/python/openhab/items.py\", line 19, in remove
    JythonItemProvider.remove(item)
  File \"<script>\", line 19, in remove
AttributeError: 'unicode' object has no attribute 'name'
"}],
  "failures": [],
  "skipped": []
}

Any help appreciated!

Like I said in the other post, the original example is working for me. For some reason, the item name string is not being converted to an Item in items.add before being added to the internal provider. I’d recommend adding some logging to items.py and see what’s happening.