I put together a class to assist with read/writing of metadata. Still working on some details but starting out with the code below.
from org.eclipse.smarthome.core.items import Metadata
from org.eclipse.smarthome.core.items import MetadataKey
from openhab import osgi
MetadataRegistry = osgi.get_service("org.eclipse.smarthome.core.items.MetadataRegistry")
class Metadata:
def __init__(self,item_name,namespace):
self.item_name = item_name
self.namespace = namespace
def __str__(self):
return 'Item: {}, Namespace = {}, Value {}, Configuration {}'.format(self.item_name,self.namespace,self.get_value(),self.get_configuration())
def does_namespace_exist(self):
if self.read_metadata() is not None:
return True
else:
return False
def read_metadata(self):
return MetadataRegistry.get(MetadataKey(self.namespace,self.item_name)) # returns None if namespace does not exist
def get_value(self):
metadata = self.read_metadata()
return metadata and str(metadata.value) or None
def get_configuration(self):
metadata=self.read_metadata()
md_configuration = hasattr(metadata, 'configuration') and metadata.configuration or {}
configuration = {} # process any configuration values here
for key,value in md_configuration.iteritems():
configuration [str(key)] = str(value)
return configuration
def get_configuration_value_for_key(self,key):
configuration = self.get_configuration()
return configuration.has_key(key) and configuration [key] or None
def write(self,value='',configuration={}):
MetadataRegistry.add(Metadata(MetadataKey(self.namespace,self.item_name),str(value),configuration))