-
-
Notifications
You must be signed in to change notification settings - Fork 4
Description
- I think the documentation is not clear enough what happens when the setting “Use scope and import wrapper” is off.
With try-and-error I found that when the option is on, this works:
import sys
import scope
scope.lifecycleTracker.addDisposeHook(lambda : print("Bye!"))
and when the option is off, the above produces:
2025-10-11 12:52:54.856 [ERROR] [nhab.automation.pythonscripting.b.py] - Traceback (most recent call last):
2025-10-11 12:52:54.859 [ERROR] [nhab.automation.pythonscripting.b.py] - File "/etc/openhab/automation/python/b.py", line 2, in <module>
2025-10-11 12:52:54.861 [ERROR] [nhab.automation.pythonscripting.b.py] - import scope
2025-10-11 12:52:54.864 [ERROR] [nhab.automation.pythonscripting.b.py] - ModuleNotFoundError: No module named 'scope'
while then this works
import sys
lifecycleTracker.addDisposeHook(lambda : print("Bye!"))
I described over openhab/openhab-docs#2567 how the scriptLoaded(String)
and scriptUnloaded()
functions and the lifecycleTracker
work in JSR223, the result is visible at https://deploy-preview-2567--openhab-docs-preview.netlify.app/docs/configuration/jsr223.html#the-scriptloaded-and-scriptunloaded-functions . Only for Python(GraalVM) there are distinct syntaxes, depending on the configuration. All other OH Automation Add-Ons put the data from the implicit presets in the global namespace: there is no way around this.
-
Is there a single syntax, which works irrespective of whether the scope module is enabled, or disabled?
-
The examples at https://www.openhab.org/addons/automation/pythonscripting/#scripting-basics should offer the same functionality, once with the
scope
module, once when thescope
module is disabled - to show the difference.
In particular in the openhab-python/MIGRATION.md file, section “JSR223 Scope” after “to have the same behavior again” should suggest, as one possibility, to turn “Use scope and import wrapper” off.
I also think that once the option is changed, the files under automation/python must be re-evaluated, that is, if a file has led in the past to ModuleNotFoundError: No module named 'scope'
, changing “Use scope and import wrapper” should re-read the file and make the error disappear.
The Python toggle setting “Use scope and import wrapper” and “Install openHAB Python helper modules and keep it updated (requires scope module)” should actually be one setting with three options, as only three states are valid. When JavaScript(GraalVM) has impossible options, it logs “[WARN ] [nal.GraalJSScriptEngineConfiguration] - Injection disabled for UI-based scripts, but event conversion is enabled. Event conversion will not work.” Python(Graal) does not log a warning, when “Use scope and import wrapper” is disabled and “Install openHAB Python helper modules and keep it updated (requires scope module)” is enabled.