openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

[items] Refactor `metadata` & `itemchannllink` APIs #212

Closed florian-h05 closed 1 year ago

florian-h05 commented 1 year ago

Fixes #189.

Description

This PR completely refactors the metadata and the itemchannellink APIs. Their old versions have been removed (since metadata was unable to support configuration, I'm not sure how many even used it).

The new metadata API is available under the items namespace and is able to add and change metadata configuration as well. itemchannellink got a few internal improvements and a renaming.

To Do

Testing

The following script tests all methods of these APIs:

// replaceMetadata and replaceItemChannelLink are used inside the replaceItem method
items.replaceItem({
  type: 'String',
  name: 'Hallway_Light',
  label: 'Hallway Light',
  category: 'light',
  groups: ['Hallway', 'Light'],
  tags: ['Lightbulb'],
  channels: {
    'binding:thing:device:hallway#light': {},
    'binding:thing:device:livingroom#light': { 
      profile: 'system:follow' 
    }
  },
  metadata: {
    expire: '10m,command=1',
    stateDescription: {
      config: {
        pattern: '%d%%',
        options: '1=Red, 2=Green, 3=Blue'
      }
    }
  }
});

// getMetadata
console.log('Metadata expire = ' + JSON.stringify(items.getItem('Hallway_Light').getMetadata('expire')));
console.log('Metadata stateDescription configuration = ' + items.getItem('Hallway_Light').getMetadata('stateDescription').configuration);

// removeMetadata
console.warn('Removing metadata expire ...')
items.getItem('Hallway_Light').removeMetadata('expire');
console.log('Metadata expire = ' + items.getItem('Hallway_Light').getMetadata('expire'));

// getItemChannelLink
console.log(items.metadata.itemchannellink.getItemChannelLink('Hallway_Light', 'binding:thing:device:livingroom#light'))
console.log(items.metadata.itemchannellink.getItemChannelLink('Hallway_Light', 'binding:thing:device:livingroom#light').configuration)

// removeItemChannelLink
items.metadata.itemchannellink.removeItemChannelLink('Hallway_Light', 'binding:thing:device:livingroom#light');
console.log(items.metadata.itemchannellink.getItemChannelLink('Hallway_Light', 'binding:thing:device:livingroom#light'))
florian-h05 commented 1 year ago

@rkoshak Ping.

florian-h05 commented 1 year ago

@digitaldan I have addressed your review, can you please have a look at the changes?

i think its probably a very small population if any .

I thing the same, but I'll label it as breaking to make sure that no one is suprised

rkoshak commented 1 year ago

Sorry I missed the ping. It all looks as I would expect from the metadata side (I never messed with channel links in rules so don't have much to say there.

proggprod commented 1 year ago

Been playing around with this. Would be nice if I could remove all channel links or metadata by excluding the name or some other wildcard. Sometimes I don't want to specify the name or channel link, I just want to remove/replace.

florian-h05 commented 1 year ago

IIRC, openHAB Core provides that possibility, so I could add this to the library as well. Can you please open an issue and describe your feature request?