Natural Binding, Things and links to items discovery

In order to ease items file definitions, did I propose this tools that create a group for each installed binding, containing a group for each thing defined, containing the list of linked items :

 from core.log import logging, LOG_PREFIX, log_traceback
from core.items import add_item, remove_item
from core.utils import validate_item
import requests
import sys

# Modify these variables to fit your system
URL_OPENHAB = "http://localhost:8080"

bindingTag = "BindingDiscovery"

def normalizeBindingId(id):
	id = "g" + id.capitalize()
	return id

def cleanBindingName(name):
	name = name.replace("Binding","").replace("Extensions","").replace("Extension","")
	name = name.strip()
	return name

def normalizeThingId(uid):
	groupname = []
	for element in uid:
	groupname[0] = "g"
	return "".join(groupname)

def discoverLinks(log):"Linked items discovery....")

	links = requests.get(url=URL_OPENHAB + "/rest/links").json()
	count = 0
	for link in links:
		count += 1
		channelUID = link["channelUID"]
		itemName = link["itemName"]

		thingUID = channelUID.split(":")
		del thingUID[-1]
		thingGroupName = normalizeThingId(thingUID)
		group = validate_item(thingGroupName)
		item = validate_item(itemName)

		if group != None and item != None:
			group.addMember(item)"Discovered {} links.".format(count))

def discoverThings(log):"Installed things discovery....")

	things = requests.get(url=URL_OPENHAB + "/rest/things").json()
	count = 0
	for thing in things:
		count += 1
		uid = thing["UID"].replace(":"," ").split(" ")
		bindingId = normalizeBindingId(uid[0])
		id = normalizeThingId(uid)
		name = thing["label"]

		add_item(id, item_type="Group", label=name, groups=[bindingId], category=uid[-1], tags=[bindingTag])"Discovered {} things.".format(count))

def discoverBindings(log):"Installed bindings discovery.....")

	bindings = requests.get(url=URL_OPENHAB + "/rest/bindings").json()
	count = 0
	for binding in bindings:
		count += 1
		id = normalizeBindingId(binding["id"])
		name = cleanBindingName(binding["name"])
		add_item(id, item_type="Group", label=name, groups=["gBindings"], category=binding["id"], tags=[bindingTag])"Discovered {} bindings.".format(count))

def scriptLoaded(id):
	log = logging.getLogger("{}.autoDiscovery".format(LOG_PREFIX))
	try:"Removing all auto generated groups and items...")
		for item in ir.getItemsByTag(bindingTag):
			remove_item(item)"Creating top level group...")
		add_item("gBindings", item_type="Group", label="Installed bindings", groups=["gSystem"], category="api",tags=[bindingTag])



Hope this can help somebody

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.