For anyone else that wants to try this, here is code that can be placed in your library that implements a get_thing() function … major limitation: it only returns the first, if any, Thing UID that is associated with an Item also might not work for all Thing binding types
"""
This module allows discovery of first Thing UID for given Item.
"""
try:
import typing as t
if t.TYPE_CHECKING:
try:
from org.openhab.core.thing.link import ItemChannelLinkRegistry, ManagedItemChannelLinkProvider
except:
from org.eclipse.smarthome.core.thing.link import ItemChannelLinkRegistry, ManagedItemChannelLinkProvider
except:
pass
from core import osgi
from core.log import getLogger
from core.utils import validate_item #, validate_channel_uid
ITEM_CHANNEL_LINK_REGISTRY = osgi.get_service(
"org.openhab.core.thing.link.ItemChannelLinkRegistry"
) or osgi.get_service(
"org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry"
) # type: ItemChannelLinkRegistry
LOG = getLogger(u"get_thing")
def get_thing(item_or_item_name):
"""
This function returns the first Thing UID from an Item using a
ManagedItemChannelLinkProvider.
Args:
item_or_item_name (Item or str): the Item object or name to create
the Link for
Returns:
Thing or None: the Thing UID or None
"""
try:
item = validate_item(item_or_item_name)
if item is None:
return None
things = ITEM_CHANNEL_LINK_REGISTRY.getBoundChannels(item.name)
if len(things) > 0:
Thing,Channel = str(things.pop()).rsplit(":",1)
return Thing
else:
return None
except:
import traceback
LOG.warn(traceback.format_exc())
return None
And here is what I used to test it:
import personal.get_thing
reload(personal.get_thing)
from personal.get_thing import get_thing
from org.openhab.core.thing import ThingUID
from org.slf4j import LoggerFactory
from configuration import LOG_PREFIX
log = LoggerFactory.getLogger("{}.test_get_thing".format(LOG_PREFIX))
def test_getthing(item):
thing = get_thing(item)
if (thing != None):
log.info("Thing for item: " + item + " is: " + thing)
log.info("Status for Thing is: " + str(things.get(ThingUID(thing)).status))
else:
log.info("No Thing found for item: " + item)