[jython] Test if item or GroupItem

How can I test whether a member of the item registry is a group item or not?

So far I only found out type(member) but then I need to parse the 2 space-separated values and have access to the OH types from within Jython (unless I hard-code them in my rule).

For instance,

g = itemRegistry.getItem("gTripleToggle")
# Rules DSL: if not g instanceof GroupItem:
if str(type(g)) != "<type 'org.eclipse.smarthome.core.items.GroupItem'>":
  # Error case - stop
else:
  # Expected situation - proceed

The conditional could also be hacked as follows:

if str(type(g)).split("'")[1].split(".")[-1] == "GroupItem":

But I hope there’s a more elegant way.

This is not a direct answer to your question, but I think you may find the information you’re after in the following code I wrote some time ago to explore groups and group members using Jython:

from org.slf4j import Logger, LoggerFactory

from openhab import osgi
from openhab.rules import rule
from openhab.triggers import when
import openhab.rules
reload(openhab.rules)
import openhab.triggers
reload(openhab.triggers)
from openhab.rules import rule
from openhab.triggers import when


ruleEngine = osgi.get_service("org.eclipse.smarthome.automation.RuleManager")

class GroupLister:
    def __init__(self, instName):
        self.__name__ = instName
        self.logger = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.Rules.%s" %
                                              self.__class__.__name__)
        self.logger.info("Instantiating instance of class %s" %
                         self.__class__.__name__)

    def __call__(self, event):
        self.logger.info("'%s' action (Jython cron rule)" % self.__name__)

        self.logger.info("All Group items:")
        for gItem in sorted(item for item in ir.getItemsOfType("Group"),
                            key = lambda item: item.name):
            self.logger.info("==> %s" % gItem)
            map(lambda mbr: self.logger.info("  |-- %s" % mbr),
                sorted(gMbr for gMbr in gItem.members,
                       key = lambda mItem: mItem.name))

        ruleEngine.setEnabled(self.UID, False) # one-shot rule


logger = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.jsr223")

# requires rule and when imports
ruleInst1 = rule("Group Test Rule")(
            when("Time cron 10 0/1 * * * ?")(
                GroupLister("ruleInst1")))

logger.info("ruleInst1 attributes & methods: %s" % dir(ruleInst1))

Note that this code was written before ESH was reintegrated into OH, it likely needs some modifications to work with the latest 2.5.0-SNAPSHOTs.

Thanks!

The getItemsOfType() method made me look at the code of GenericItem, more precisely:

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getType() {
        return type;
    }

It appears that getType() is also exposed in Jython, so I can rewrite my condition as follows:

if item.getType() != "Group":
if lightItem.type == "Dimmer" or (lightItem.type == "Group" and lightItem.baseItem.type == "Dimmer"):
1 Like

This discussion raises a question for me.

We have assign a Group a type. For example:

Group:Number:AverageTemps

Does getType() return “Group” or “Number”? If “Group”, how/can I determine if the Group is a “Number”? If “Number”, how/can I determine if the Number is also a Group?

Take a look at my example. AverageTemps.type would return “Group”, but AverageTemps.baseItem.type would return “Number”.

2 Likes