Hi,
I have troubles with type checks for event types - hopefully someone can help me out.
I have simplified the problem down to a few lines in two files:
Saving the above (which automatically runs the script) gives me no errors. I double checked with testing a bogus type FooType instead of int - this does result in an error message.
What I want to be able to do, is type-checking in a helper script, which I import:
2020-04-28 12:17:26.084 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/openhab/conf/automation/jsr223/python/personal/_type-test2.py': NameError: name 'PercentType' is not defined in <script> at line number 1
What is going on here? Why is “PercentType” known as a type in a script, but not in an imported script?
I found a (the?) solution: I can import the missing types:
from core.jsr223.scope import events, items, NULL, UNDEF, ON, OFF, OPEN, CLOSED, PercentType, OnOffType
Can someone please explain, if this is the right way to go, and also why are the types already present in the script in the jsr233 folder, but not in the lib?
Aaaaand I think I also found something autoriatative with regards to the “why”. I am adding it here for anybody who might hit on the same problem.
openhab\conf\automation\lib\python\core\jsr223.py
One of the challenges of scripted automation with Jython is that modules
imported into scripts do not have direct access to the JSR223 scope types and
objects. This module allows imported modules to access that data.
You got it. Modules are not scripts, and they have a different scope than scripts do. Scripts all have a default set of objects injected into the scope through ScriptExtension presets. To access those objects within a module, you will need to use the core.jsr223 module to import them.
This is in the documentation, but an updated version is coming soon…
@5iver - thank you for your explanation!
I never really doubted that an explanation to my problem would be somewhere in the documentation or in this great forum. I just failed to find the right terms to search for and after trying searching and reading through bits and pieces of the reference for a while, I decided to reach out