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

Function things.getThing(..) returns invalid Thing if Thing doesn't exist in Thing Registry #314

Closed csowada closed 6 months ago

csowada commented 6 months ago

Expected Behavior

The function things.getThing("xrobonect:mower:automower", true); should return null or a valid Thing.

Current Behavior

The created Thing ha no rawThing as it doesn't exist in the Things Registry.

Possible Solution

Enhance the Thing Constructor to filter such invalid input parameters.

class Thing {
  /**
   * Create a Thing, wrapping a native Java openHAB Thing. Don't use this constructor, instead call {@link getThing}.
   * @param {HostThing} rawThing Java Thing from Host
   * @hideconstructor
   */
  constructor (rawThing) {
    if (!rawThing || typeof rawThing === 'undefined') {
    //if (typeof rawThing === 'undefined') {
      throw Error('Supplied Thing is undefined');
    }

Steps to Reproduce (for Bugs)

This code prints "Check1" but fails with "Check2" as the Thing wrapper doesn't contains a valid rawThing

let myThing;
utils.dumpObject(myThing);

myThing = things.getThing("xrobonect:mower:automower", true);
utils.dumpObject(myThing);

if (myThing) {
  console.warn("Check1 - Found Thing!");
}

if (myThing?.rawThing) {
  console.warn("Check2 - Raw Thing existing!");
}

console.log(`My status ${myThing.status}`);

Context

Your Environment