Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] Allow for complex Item metadata in .item files #992

Open
5iver opened this issue Aug 23, 2019 · 0 comments
Open

[feature request] Allow for complex Item metadata in .item files #992

5iver opened this issue Aug 23, 2019 · 0 comments

Comments

@5iver
Copy link

5iver commented Aug 23, 2019

Currently, Item metadata in .item files is limited to strings, numbers and booleans. Metadata objects are able to contain collections, which allows a hierarchical structure of the keys in the metadata. For example, this Item definition...

Switch      Test_Switch_1           "Test Switch 1 [%s]"            <switch>      (gVirtual,gTest)                      { Test_Namespace="Test_Value" [Test_Key_1={Test_Subkey_1="Test_value_1", Test_Subkey_2=false, Test_Subkey_3=5}] }

... causes the following to be logged...

2020-05-27 15:46:28.136 [WARN ] [org.eclipse.smarthome.model.core.internal.ModelRepositoryImpl] - Configuration model 'test.items' has errors, therefore ignoring it: [16,163]: no viable alternative at input '{'
[16,230]: extraneous input '}' expecting ']'

Using the Jython or Javascript helper libraries, Item metadata can be set with a hierarchical key structure, which is not possible in an .items file...

from core.metadata import set_metadata, get_metadata, get_key_value

from core.log import logging, LOG_PREFIX#, log_traceback
LOG = logging.getLogger("{}.TEST_2".format(LOG_PREFIX))

set_metadata("Test_Switch_1", "Test_Namespace", {"Test_Key_1": {"Test_Subkey_1": "Test_value_1", "Test_Subkey_2":False, "Test_Subkey_3":5}}, "Test_Value")

LOG.warn("Test_Switch_1: {}".format(get_metadata("Test_Switch_1", "Test_Namespace")))
LOG.warn("Test_Key_1: {}".format(get_key_value("Test_Switch_1", "Test_Namespace", "Test_Key_1")))
LOG.warn("Test_Subkey_1: {}".format(get_key_value("Test_Switch_1", "Test_Namespace", "Test_Key_1", "Test_Subkey_1")))
LOG.warn("Test_Subkey_2: {}".format(get_key_value("Test_Switch_1", "Test_Namespace", "Test_Key_1", "Test_Subkey_2")))
LOG.warn("Test_Subkey_3: {}".format(get_key_value("Test_Switch_1", "Test_Namespace", "Test_Key_1", "Test_Subkey_3")))
2020-05-27 15:52:48.992 [WARN ] [jython.TEST_2] - Java version [Java-1.8.0_242-OpenJDK_64-Bit_Server_VM,_25.242-b08,_Oracle_Corporation-on-Linux-5.4.10-200.fc31.x86_64-amd64]
2020-05-27 15:52:48.994 [WARN ] [jython.TEST_2] - Test_Switch_1: Metadata [key=Test_Namespace:Test_Switch_1, value=Test_Value, configuration=[Test_Key_1={'Test_Subkey_3': 5, 'Test_Subkey_1': 'Test_value_1', 'Test_Subkey_2': False}]]
2020-05-27 15:52:48.994 [WARN ] [jython.TEST_2] - Test_Key_1: {'Test_Subkey_3': 5, 'Test_Subkey_1': 'Test_value_1', 'Test_Subkey_2': False}
2020-05-27 15:52:48.995 [WARN ] [jython.TEST_2] - Test_Subkey_1: Test_value_1
2020-05-27 15:52:48.995 [WARN ] [jython.TEST_2] - Test_Subkey_2: 0
2020-05-27 15:52:48.996 [WARN ] [jython.TEST_2] - Test_Subkey_3: 5

Using Jython and the core.metadata module, you can create structures like this, which is not currently possible with a text-based Items file...

"Mode": {
    "Morning": {"low_lux_trigger":5, "Level":10},
    "Day":{"low_lux_trigger":50, "Level":45},
    "Evening":{"low_lux_trigger":50, "Level":45},
    "Night":{"low_lux_trigger":50, "Level":30},
    "Late":{"low_lux_trigger":5, "Level":1}
}
"Timer": {
    "Action": {
        "ON": {
            "Time":60,
            "Recurring":False
        },
        "OFF": {
            "Time":3600,
            "Recurring":False
        }
    },
    "Notification": {
        "ON": {
            "Time":3600,
            "Message": "The doors are currently unlocked",
            "Types": {
                "Audio": True,
                "Kodi": True,
                "Mobile": True
            },
            "Priority": 0,
            "Recurring":True
        },
        "OFF": {
            "Time":5,
            "Recurring":False
        }
    }
}

https://github.com/openhab/openhab-core/blob/master/bundles/org.openhab.core.model.item/src/org/eclipse/smarthome/model/item/internal/GenericItemProvider.java#L341

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant