From 5121e614c2618748fac88d98f62489cde7198a2c Mon Sep 17 00:00:00 2001 From: Andreas Holstenson Date: Tue, 23 Jan 2018 10:02:18 +0100 Subject: [PATCH] Refactoring to fully promis-based API --- climate/adjustable-fan-speed.js | 8 +++- climate/adjustable-target-humidity.js | 8 +++- climate/autonomous-cleaning.js | 16 +++++-- climate/cleaning-state.js | 4 +- climate/fan-speed.js | 2 +- climate/spot-cleaning.js | 8 +++- climate/target-humidity.js | 2 +- common/autonomous-charging.js | 8 +++- common/battery-level.js | 4 +- common/charging-state.js | 2 +- common/children.js | 13 +----- common/error-state.js | 7 +-- common/mode.js | 10 ++--- common/power.js | 2 +- common/restorable-state.js | 10 +++-- common/state.js | 18 +++++--- common/switchable-mode.js | 12 +++-- common/switchable-power.js | 10 +++-- controllers/actions.js | 4 +- docs/common/battery-level.rst | 12 +++-- docs/common/charging-state.rst | 12 +++-- docs/common/children.rst | 2 +- docs/common/mode.rst | 22 +++++----- docs/common/nameable.rst | 2 +- docs/common/power.rst | 22 ++++------ docs/common/restorable-state.rst | 16 ++++--- docs/common/state.rst | 8 ++-- docs/common/switchable-mode.rst | 12 ++--- docs/common/switchable-power.rst | 10 ++--- docs/controllers/actions.rst | 11 +++-- docs/lights/brightness.rst | 8 ++-- docs/lights/colorable.rst | 19 ++++---- docs/lights/dimmable.rst | 8 ++-- docs/lights/fading.rst | 20 ++++++++- docs/sensors/atmospheric-pressure.rst | 14 ++++-- docs/sensors/carbon-dioxide.rst | 27 +++++++++--- docs/sensors/carbon-monoxide.rst | 24 ++++++---- docs/sensors/contact.rst | 37 +++++++++++++--- docs/sensors/illuminance.rst | 15 +++++-- docs/sensors/motion.rst | 13 ++++-- docs/sensors/pm10.rst | 17 +++++--- docs/sensors/pm2.5.rst | 24 +++++++--- docs/sensors/power-consumed.rst | 20 ++++++--- docs/sensors/power-load.rst | 19 +++++--- docs/sensors/relative-humidity.rst | 19 ++++---- docs/sensors/temperature.rst | 13 +++++- docs/sensors/voltage.rst | 13 +++++- lights/color-temperature.js | 8 ++-- lights/colorable.js | 24 +++++----- lights/dimmable.js | 63 +++++++++++++++------------ lights/fading.js | 6 +-- lights/light-state.js | 16 ++++--- media/adjustable-volume.js | 45 +++++++++++-------- media/muteable.js | 13 ++++-- media/muted.js | 2 +- media/volume.js | 2 +- sensors/carbon-dioxide.js | 4 +- sensors/carbon-monoxide.js | 4 +- sensors/contact.js | 11 ++--- sensors/illuminance.js | 2 +- sensors/motion.js | 2 +- sensors/pm10.js | 2 +- sensors/pm2_5.js | 4 +- sensors/power-consumed.js | 2 +- sensors/power-load.js | 2 +- sensors/relative-humidity.js | 4 +- sensors/sensor.js | 4 +- sensors/temperature.js | 2 +- sensors/voltage.js | 2 +- 69 files changed, 506 insertions(+), 305 deletions(-) diff --git a/climate/adjustable-fan-speed.js b/climate/adjustable-fan-speed.js index ab58566..8711dfa 100644 --- a/climate/adjustable-fan-speed.js +++ b/climate/adjustable-fan-speed.js @@ -29,8 +29,12 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(FanSpeed) { setFanSpeed(speed) { speed = percentage(speed, true); - return this.changeFanSpeed(speed) - .then(() => super.fanSpeed()); + try { + return Promise.resolve(this.changeFanSpeed(speed)) + .then(() => super.fanSpeed()); + } catch(ex) { + return Promise.reject(ex); + } } changeFanSpeed(speed) { diff --git a/climate/adjustable-target-humidity.js b/climate/adjustable-target-humidity.js index 1493a9d..83d7d67 100644 --- a/climate/adjustable-target-humidity.js +++ b/climate/adjustable-target-humidity.js @@ -29,8 +29,12 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(TargetHumidity) setTargetHumidity(humidity) { humidity = percentage(humidity, true); - return this.changeTargetHumidity(humidity) - .then(() => super.targetHumidity()); + try { + return Promise.resolve(this.changeTargetHumidity(humidity)) + .then(() => super.targetHumidity()); + } catch(ex) { + return Promise.reject(ex); + } } changeTargetHumidity(humidity) { diff --git a/climate/autonomous-cleaning.js b/climate/autonomous-cleaning.js index ea0f998..02dc885 100644 --- a/climate/autonomous-cleaning.js +++ b/climate/autonomous-cleaning.js @@ -21,13 +21,21 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State, Cleaning } clean() { - return Promise.resolve(this.activateCleaning()) - .then(() => null); + try { + return Promise.resolve(this.activateCleaning()) + .then(() => null); + } catch(ex) { + return Promise.reject(ex); + } } stop() { - return Promise.resolve(this.deactivateCleaning()) - .then(() => null); + try { + return Promise.resolve(this.deactivateCleaning()) + .then(() => null); + } catch(ex) { + return Promise.reject(ex); + } } activateCleaning() { diff --git a/climate/cleaning-state.js b/climate/cleaning-state.js index 2933016..d1ca84c 100644 --- a/climate/cleaning-state.js +++ b/climate/cleaning-state.js @@ -53,8 +53,8 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(ErrorState, Sta /** * Get if thing is cleaning. */ - get cleaning() { - return this.getState('cleaning'); + cleaning() { + return Promise.resolve(this.getState('cleaning')); } updateCleaning(cleaning) { diff --git a/climate/fan-speed.js b/climate/fan-speed.js index 83c68e9..1093558 100644 --- a/climate/fan-speed.js +++ b/climate/fan-speed.js @@ -23,7 +23,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { } fanSpeed() { - return this.getState('fanSpeed'); + return Promise.resolve(this.getState('fanSpeed')); } updateFanSpeed(fanSpeed) { diff --git a/climate/spot-cleaning.js b/climate/spot-cleaning.js index a362798..cda32c0 100644 --- a/climate/spot-cleaning.js +++ b/climate/spot-cleaning.js @@ -17,8 +17,12 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State, Cleaning } spotClean() { - return Promise.resolve(this.activateSpotClean()) - .then(() => null); + try { + return Promise.resolve(this.activateSpotClean()) + .then(() => null); + } catch(ex) { + return Promise.reject(ex); + } } activateSpotClean() { diff --git a/climate/target-humidity.js b/climate/target-humidity.js index 74c3a65..2c9cb58 100644 --- a/climate/target-humidity.js +++ b/climate/target-humidity.js @@ -23,7 +23,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { } targetHumidity() { - return this.getState('targetHumidity'); + return Promise.resolve(this.getState('targetHumidity')); } updateTargetHumidity(target) { diff --git a/common/autonomous-charging.js b/common/autonomous-charging.js index 3d7e394..5e1cd65 100644 --- a/common/autonomous-charging.js +++ b/common/autonomous-charging.js @@ -17,8 +17,12 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State, Charging } charge() { - return Promise.resolve(this.activateCharging()) - .then(() => null); + try { + return Promise.resolve(this.activateCharging()) + .then(() => null); + } catch(ex) { + return Promise.reject(ex); + } } activateCharging() { diff --git a/common/battery-level.js b/common/battery-level.js index 73ea439..89bbb9a 100644 --- a/common/battery-level.js +++ b/common/battery-level.js @@ -33,8 +33,8 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { this.updateState('batteryLevel', -1); } - get batteryLevel() { - return this.getState('batteryLevel'); + batteryLevel() { + return Promise.resolve(this.getState('batteryLevel')); } updateBatteryLevel(level) { diff --git a/common/charging-state.js b/common/charging-state.js index debd6b4..26812e7 100644 --- a/common/charging-state.js +++ b/common/charging-state.js @@ -45,7 +45,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { * Get if thing is charging. */ get charging() { - return this.getState('charging'); + return Promise.resolve(this.getState('charging')); } updateCharging(charging) { diff --git a/common/children.js b/common/children.js index 3604ebf..275ab74 100644 --- a/common/children.js +++ b/common/children.js @@ -136,19 +136,8 @@ module.exports = Thing.mixin(Parent => class extends Parent { /** * Get all of the children that are registered. */ - get children() { + children() { return this[childrenSymbol].values(); } - /** - * Synchronize the children based on the given definitions. This will remove - * any child that is not in the list of definitions and create new ones - * for new definitions. - * - * @param {Iterable} defs - * @param {Function} func - */ - syncChildren(defs, func) { - - } }); diff --git a/common/error-state.js b/common/error-state.js index 7db0d92..6e25f51 100644 --- a/common/error-state.js +++ b/common/error-state.js @@ -30,11 +30,6 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { .description('Get the current error state') .returns('code', 'The current error state or null if no error') .done(); - - builder.action('hasError') - .description('Get if the thing is in an error state') - .returns('boolean') - .done(); } static get capability() { @@ -51,7 +46,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { * Get if thing has an error. */ get error() { - return this.getState('error'); + return Promise.resolve(this.getState('error')); } updateError(error) { diff --git a/common/mode.js b/common/mode.js index d88926a..2226818 100644 --- a/common/mode.js +++ b/common/mode.js @@ -61,13 +61,11 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { /** * Get or switch the mode of the appliance. * - * @param {string} mode - * optional mode to switch to * @returns * string indicating the mode */ - mode(mode=undefined) { - return this.getState('mode'); + mode() { + return Promise.resolve(this.getState('mode')); } /** @@ -84,8 +82,8 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { /** * Get the available modes of the device. */ - get modes() { - return this.getState('modes'); + modes() { + return Promise.resolve(this.getState('modes')); } /** diff --git a/common/power.js b/common/power.js index 76ba684..deaa8cf 100644 --- a/common/power.js +++ b/common/power.js @@ -49,7 +49,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { * boolean indicating the power level */ power() { - return this.getState('power'); + return Promise.resolve(this.getState('power')); } /** diff --git a/common/restorable-state.js b/common/restorable-state.js index 011ee34..e261cf6 100644 --- a/common/restorable-state.js +++ b/common/restorable-state.js @@ -51,7 +51,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { for(const property of this.restorableState) { result[property] = this.state[property]; } - return result; + return Promise.resolve(result); } /** @@ -60,8 +60,12 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { * @param {object} state */ setState(state) { - return Promise.resolve(this.changeState(state)) - .then(() => this.state); + try { + return Promise.resolve(this.changeState(state)) + .then(() => this.state); + } catch(ex) { + return Promise.reject(ex); + } } /** diff --git a/common/state.js b/common/state.js index 2f63589..674ac24 100644 --- a/common/state.js +++ b/common/state.js @@ -3,6 +3,8 @@ const Thing = require('../thing'); const deepEqual = require('deep-equal'); +const state = Symbol('state'); + /** * State capability for things. Exposes a property named `state` to clients. * @@ -35,7 +37,11 @@ module.exports = Thing.mixin(Parent => class extends Parent { constructor(...args) { super(...args); - this.state = {}; + this[state] = {}; + } + + state() { + return Promise.resolve(this[state]); } /** @@ -45,12 +51,12 @@ module.exports = Thing.mixin(Parent => class extends Parent { * @param {*} value */ updateState(key, value) { - if(deepEqual(this.state[key], value)) { + if(deepEqual(this[state][key], value)) { // If the value has not changed, skip updating and emitting event return false; } else { // Value has changed, update and queue event emittal - this.state[key] = value; + this[state][key] = value; const event = { key: key, value: value @@ -69,8 +75,8 @@ module.exports = Thing.mixin(Parent => class extends Parent { * @param {string} key */ removeState(key) { - const emitEvent = typeof this.state[key] !== 'undefined'; - delete this.state[key]; + const emitEvent = typeof this[state][key] !== 'undefined'; + delete this[state][key]; if(emitEvent) { const event = { @@ -90,7 +96,7 @@ module.exports = Thing.mixin(Parent => class extends Parent { * @param {*} defaultValue */ getState(key, defaultValue=null) { - const value = this.state[key]; + const value = this[state][key]; return typeof value === 'undefined' ? defaultValue : value; } }); diff --git a/common/switchable-mode.js b/common/switchable-mode.js index b9f8f2a..c3dda8d 100644 --- a/common/switchable-mode.js +++ b/common/switchable-mode.js @@ -58,11 +58,15 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Mode) { * @param {string} mode */ setMode(mode) { - if(typeof mode === 'undefined') throw new Error('Mode must be specified'); - mode = String(mode); + try { + if(typeof mode === 'undefined') throw new Error('Mode must be specified'); + mode = String(mode); - return Promise.resolve(this.changeMode(mode)) - .then(() => this.getState('mode')); + return Promise.resolve(this.changeMode(mode)) + .then(() => this.getState('mode')); + } catch(ex) { + return Promise.reject(ex); + } } /** diff --git a/common/switchable-power.js b/common/switchable-power.js index 9720d83..f8ea1e1 100644 --- a/common/switchable-power.js +++ b/common/switchable-power.js @@ -77,10 +77,14 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Power, Restorab * @param {boolean} power */ setPower(power) { - power = boolean(power); + try { + power = boolean(power); - return Promise.resolve(this.changePower(power)) - .then(() => this.power()); + return Promise.resolve(this.changePower(power)) + .then(() => this.power()); + } catch(ex) { + return Promise.reject(ex); + } } togglePower() { diff --git a/controllers/actions.js b/controllers/actions.js index be6acfd..86f00e3 100644 --- a/controllers/actions.js +++ b/controllers/actions.js @@ -58,8 +58,8 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { * Get the available actions for this controller. All of these actions * can be emitted. */ - get actions() { - return this.getState('actions'); + actions() { + return Promise.resolve(this.getState('actions')); } /** diff --git a/docs/common/battery-level.rst b/docs/common/battery-level.rst index 9439176..47a32a8 100644 --- a/docs/common/battery-level.rst +++ b/docs/common/battery-level.rst @@ -9,24 +9,28 @@ being charged. .. sourcecode:: js if(thing.matches('cap:battery-level')) { - console.log('Current battery level:', thing.batteryLevel); + console.log('Current battery level:', await thing.batteryLevel()); } API --- -.. js:attribute:: batteryLevel +.. js:function:: batteryLevel() Get the current battery level as a :doc:`percentage ` between 0 and 100. - :returns: The battery level in percent. + :returns: Promise that resolves to the battery level in percent. Example: .. sourcecode:: js - const level = thing.batteryLevel; + thing.batteryLevel() + .then(level => ...) + .catch(...); + + const level = await thing.batteryLevel(); Events ------ diff --git a/docs/common/charging-state.rst b/docs/common/charging-state.rst index 2de1504..979a223 100644 --- a/docs/common/charging-state.rst +++ b/docs/common/charging-state.rst @@ -8,7 +8,7 @@ have the :doc:`battery-level ` capability. .. sourcecode:: js if(thing.matches('cap:charging-state')) { - if(thing.charging) { + if(await thing.charging()) { // This thing is charging } } @@ -16,18 +16,22 @@ have the :doc:`battery-level ` capability. API --- -.. js:attribute:: charging +.. js:function:: charging() Get the current charging state as a :doc:`boolean `. ``true`` indicates that the thing is charging. - :returns: The current charging state. + :returns: Promise that resolves to the current charging state. Example: .. sourcecode:: js - const isCharging = thing.charging; + thing.charging() + .then(isCharging => ...) + .catch(...); + + const isCharging = await thing.charging(); Events ------ diff --git a/docs/common/children.rst b/docs/common/children.rst index 04ed008..d513dc5 100644 --- a/docs/common/children.rst +++ b/docs/common/children.rst @@ -19,7 +19,7 @@ support control or monitoring of their indivudal outlets. API --- -.. js:attribute:: children +.. js:function:: children() Get the children of the thing as an iterable. diff --git a/docs/common/mode.rst b/docs/common/mode.rst index 5988297..6f7c72a 100644 --- a/docs/common/mode.rst +++ b/docs/common/mode.rst @@ -6,7 +6,7 @@ .. sourcecode:: js if(thing.matches('cap:mode')) { - console.log('Mode is', thing.mode()); + console.log('Mode is', await thing.mode()); thing.on('modeChanged', mode => console.log('Mode is now', mode)); } @@ -18,15 +18,21 @@ API Get the current mode of the thing. - :returns: :doc:`String ` indicating the mode. + :returns: + Promises that resolves to a :doc:`string `indicating + the mode. Example: .. sourcecode:: js - console.log(thing.mode()); + thing.mode() + .then(mode => ...) + .catch(...); -.. js:attribute:: modes + const mode = await thing.mode(); + +.. js:function:: modes() Get the modes that this thing supports. Will return an array with strings representing the modes. @@ -37,14 +43,6 @@ API console.log(thing.modes); -.. js:attribute:: state.mode - - State-key representing the current mode. - -.. js:attribute:: state.modes - - State-key representing the modes. - Events ------- diff --git a/docs/common/nameable.rst b/docs/common/nameable.rst index 3fe37bb..25a1075 100644 --- a/docs/common/nameable.rst +++ b/docs/common/nameable.rst @@ -19,7 +19,7 @@ API Update the name of this thing. :param string name: Name for thing. - :returns: The name set + :returns: Promise that resolves to the name set. Protected methods ----------------- diff --git a/docs/common/power.rst b/docs/common/power.rst index c083cc1..fff3bea 100644 --- a/docs/common/power.rst +++ b/docs/common/power.rst @@ -7,7 +7,7 @@ state. .. sourcecode:: js if(thing.matches('cap:power')) { - console.log('Power is', thing.power()); + console.log('Power is', await thing.power()); thing.on('powerChanged', power => console.log('Power is now', power)); } @@ -21,23 +21,19 @@ API Get the current power state. - :returns: :doc:`Boolean ` representing the current power state. + :returns: + Promise that resolves to a :doc:`boolean ` + representing the current power state. Example: .. sourcecode:: js - const powerIsOn = thing.power(); + thing.power() + .then(power => ...) + .catch(...); -.. js:attribute:: state.power - - State-key indicating the current power state. - - Example: - - .. sourcecode:: js - - console.log(thing.state.power); + const powerIsOn = await thing.power(); Events ------ @@ -79,7 +75,7 @@ Example: // Indicate that power has been switched every second setInterval(() => { - this.updatePower(! this.state.power); + this.updatePower(! this.getState('power')); }, 1000); } } diff --git a/docs/common/restorable-state.rst b/docs/common/restorable-state.rst index d42b008..b0ca53d 100644 --- a/docs/common/restorable-state.rst +++ b/docs/common/restorable-state.rst @@ -10,12 +10,10 @@ capturing and setting state. console.log('Keys that can be restored:' , thing.restorableState); // Capture the state - const state = thing.captureState(); + const state = await thing.captureState(); // A bit later the state can be restored - thing.setState(state) - .then(() => console.log('State has been restored')) - .catch(err => console.log('Error during state restore', err)); + await thing.setState(state); } API @@ -36,13 +34,17 @@ API Capture all the state that can be restored. - :returns: Object representing the state. + :returns: Promise that resolves to the object representing the state. Example: .. sourcecode:: js - const state = thing.captureState(); + thing.setState(state) + .then(...) + .catch(...); + + const state = await thing.captureState(); .. js:function:: setState(state) @@ -60,6 +62,8 @@ API .then(...) .catch(...); + await thing.setState(state); + Protected methods ----------------- diff --git a/docs/common/state.rst b/docs/common/state.rst index 0b72600..6019da7 100644 --- a/docs/common/state.rst +++ b/docs/common/state.rst @@ -13,17 +13,19 @@ State is split into several state keys that are updated separately. API --- -.. js:attribute:: state +.. js:function:: state() Get the current overall state. - :returns: Object representing the current state. Keys represent names of the state key. + :returns: + Promise that resolves to an object representing the current state. + Keys represent names of the state key. Usage: .. sourcecode:: js - const state = thing.state; + const state = await thing.state(); console.log('State is', state); console.log('Value of power is', state.power); diff --git a/docs/common/switchable-mode.rst b/docs/common/switchable-mode.rst index d614a34..bbe241f 100644 --- a/docs/common/switchable-mode.rst +++ b/docs/common/switchable-mode.rst @@ -6,12 +6,10 @@ Capability used for things that can switch their mode. .. sourcecode:: js if(thing.matches('cap:switchable-mode')) { - console.log('Mode is', thing.mode()); + console.log('Mode is', await thing.mode()); // Switch the mode - thing.mode('new-mode') - .then(mode => console.log('Mode is now', mode)) - .catch(err => console.log('Failed to switch mode', err)); + await thing.mode('new-mode'); } API @@ -30,7 +28,7 @@ API .. sourcecode:: js // Getting returns a string - const currentMode = thing.mode(); + const currentMode = await thing.mode(); // Switching returns a promise thing.mode('new-mode') @@ -48,10 +46,12 @@ API .. sourcecode:: js - thing.setMode('new-mode) + thing.setMode('new-mode') .then(result => console.log('Mode is now', result)) .catch(err => console.log('Error occurred', err); + await thing.setMode('new-mode'); + Protected methods ----------------- diff --git a/docs/common/switchable-power.rst b/docs/common/switchable-power.rst index e74b992..8afc043 100644 --- a/docs/common/switchable-power.rst +++ b/docs/common/switchable-power.rst @@ -7,12 +7,10 @@ for things that can also switch their power state. .. sourcecode:: js if(thing.matches('cap:switchable-power')) { - console.log('Power is', thing.power()); + console.log('Power is', await thing.power()); // Switch the thing on - thing.power(true) - .then(() => console.log('Power is now on')) - .catch(err => console.log('Failed to switch power', err)); + await thing.power(true); } Related capabilities: :doc:`power `, :doc:`state ` @@ -32,7 +30,7 @@ API .. sourcecode:: js // Getting returns a boolean - const powerIsOn = thing.power(); + const powerIsOn = await thing.power(); // Switching returns a promise thing.power(false) @@ -54,6 +52,8 @@ API .then(result => console.log('Power is now', result)) .catch(err => console.log('Error occurred', err); + await thing.setPower(true); + .. js:function:: togglePower() Toggle the power of the thing. Will use the currently detected power state diff --git a/docs/controllers/actions.rst b/docs/controllers/actions.rst index 7246981..8cee9ff 100644 --- a/docs/controllers/actions.rst +++ b/docs/controllers/actions.rst @@ -17,16 +17,19 @@ such a button press occurs. API --- -.. js:attribute:: actions +.. js:function:: actions(); - Get the actions that the thing supports. Array containing actions supported - as :doc:`codes `. + Get the actions that the thing supports. + + :returns: + Promise that resolves to any array containing the actions as + :doc:`codes `. Example: .. sourcecode:: js - const actions = thing.actions; + const actions = await thing.actions(); const action = actions[0]; console.log('First action id:', action.id); diff --git a/docs/lights/brightness.rst b/docs/lights/brightness.rst index 8429ca3..c52844d 100644 --- a/docs/lights/brightness.rst +++ b/docs/lights/brightness.rst @@ -8,7 +8,7 @@ their brightness. .. sourcecode:: js if(thing.matches('cap:brightness')) { - console.log(thing.brightness()); + console.log(await thing.brightness()); } API @@ -19,14 +19,14 @@ API Get the brightness of the light. :returns: - :doc:`Percentage `, number between 0 and 100, - representing the brightness. + Promise that resolves to a :doc:`percentage ` + between 0 and 100, representing the brightness. Example: .. sourcecode:: js - console.log(thing.brightness); + console.log(await thing.brightness()); Events ------ diff --git a/docs/lights/colorable.rst b/docs/lights/colorable.rst index e0efc25..14b5384 100644 --- a/docs/lights/colorable.rst +++ b/docs/lights/colorable.rst @@ -6,11 +6,10 @@ Capability used for lights that can be colored. .. sourcecode:: js if(thing.matches('type:light', 'cap:colorable')) { - console.log('Current color', thing.color()); + console.log('Current color', await thing.color()); - thing.color('red') - .then(color => console.log('Color is now', color)) - .catch(err => console.log('Error while setting color', err)); + // Set the color + await thing.color('red'); } API @@ -29,23 +28,21 @@ API Optional :doc:`duration ` to perform change in brightness over. Supported when the light has the :doc:`fading `-capability. + :returns: + Promise that resolves to the current or set color. Example: .. sourcecode:: js // Get the current color - const currentColor = thing.color(); + const currentColor = await thing.color(); // Change color - thing.color('4000K') - .then(color => console.log('Color is now', color)) - .catch(err => console.log('Error while setting color', err)); + const newColor = await thing.color('4000K'); // Change color over 2 seconds - thing.color('#00ffff', '2s') - .then(...) - .catch(...); + await thing.color('#00ffff', '2s'); Events ------ diff --git a/docs/lights/dimmable.rst b/docs/lights/dimmable.rst index 966c4e7..73d7dc7 100644 --- a/docs/lights/dimmable.rst +++ b/docs/lights/dimmable.rst @@ -8,12 +8,10 @@ Capability used when a light supports changing the brightness, extends if(thing.matches('cap:dimmable')) { // Get the current brightness - console.log(thing.brightness()); + console.log(await thing.brightness()); // Set the current brightness - thing.brightness(10) - .then(bri => console.log('Brightness is now', bri)) - .catch(err => console.log('Could not change brightness', err)); + const newBrightness = await thing.brightness(10); } API @@ -33,7 +31,7 @@ API Optional :doc:`duration ` to perform change in brightness over. Supported when the light has the :doc:`fading `-capability. - :returns: Current brightness when getting, promise when setting. + :returns: Promise that resolves to the current or the set brightness. Example: diff --git a/docs/lights/fading.rst b/docs/lights/fading.rst index d4969e3..94729f1 100644 --- a/docs/lights/fading.rst +++ b/docs/lights/fading.rst @@ -8,7 +8,8 @@ capability is present the ``duration`` argument for other methods is available. if(thing.matches('type:light', 'cap:fading')) { // This light supports fading - console.log('Maximum fading time in milliseconds:', this.maxChangeTime.ms); + const time = await this.maxChangeTime(); + console.log('Maximum fading time in milliseconds:', time.ms); } API @@ -18,6 +19,21 @@ API The maximum :doc:`duration ` of time a change can be. +Protected methods +----------------- + +.. js:function:: updateMaxChangeTime(time) + + :param duration time: + The maximum time the light can fade as a + :doc:`duration `. + + Example: + + .. sourcecode:: js + + this.updateMaxChangeTime('20s'); + Implementing capability ----------------------- @@ -35,7 +51,7 @@ Example: initCallback() { return super.initCallback() // Set the maximum change time to 5 seconds - .then(() => this.maxChangeTime = '5s')); + .then(() => this.updateMaxChangeTime('5s')); } } diff --git a/docs/sensors/atmospheric-pressure.rst b/docs/sensors/atmospheric-pressure.rst index 43796c4..44bb852 100644 --- a/docs/sensors/atmospheric-pressure.rst +++ b/docs/sensors/atmospheric-pressure.rst @@ -6,19 +6,25 @@ This capability is used to mark sensors that report the atmospheric pressure. .. sourcecode:: js if(thing.matches('cap:atmospheric-pressure')) { - console.log('Atmospheric pressure:', thing.atmosphericPressure); + console.log('Atmospheric pressure:', await thing.atmosphericPressure()); } API --- -.. js:attribute:: atmosphericPressure +.. js:function:: atmosphericPressure() Get the current atmospheric pressure. + :returns: + Promise that resolves to the atmospheric pressure as a + :doc:`pressure `. + + Example: + .. sourcecode:: js - console.log('Atmospheric pressure:', thing.atmosphericPressure); + console.log('Atmospheric pressure:', await thing.atmosphericPressure()); Events ------ @@ -27,6 +33,8 @@ Events The atmospheric pressure has changed. + Example: + .. sourcecode:: js thing.on('atmosphericPressureChanged', value => console.log('Pressure changed to:', value)); diff --git a/docs/sensors/carbon-dioxide.rst b/docs/sensors/carbon-dioxide.rst index e27e634..bf768d3 100644 --- a/docs/sensors/carbon-dioxide.rst +++ b/docs/sensors/carbon-dioxide.rst @@ -7,29 +7,40 @@ as PPM (parts per million). The value is reported as a :doc:`number `. + Get the current carbon dioxide levels as PPM. + + :returns: + Promise that resolves to the current value as a + :doc:`number `. + + Example: .. sourcecode:: js - console.log('CO2 is:', thing.carbonDioxide); + console.log('CO2 is:', await thing.carbonDioxide()); -.. js:attribute:: co2 +.. js:function:: co2() Get the current carbon dioxide levels as PPM. Reported as a :doc:`number `. + :returns: + Promise that resolves to the current value as a + :doc:`number `. + + Example: + .. sourcecode:: js - console.log('CO2 is:', thing.co2); + console.log('CO2 is:', await thing.co2()); Events ------ @@ -39,6 +50,8 @@ Events The carbon dioxide level has changed. Payload is the new PPM as a :doc:`number `. + Example: + .. sourcecode:: js thing.on('carbonDioxideChanged', v => console.log('Changed to:', v)); diff --git a/docs/sensors/carbon-monoxide.rst b/docs/sensors/carbon-monoxide.rst index 3abf2b5..1f984fd 100644 --- a/docs/sensors/carbon-monoxide.rst +++ b/docs/sensors/carbon-monoxide.rst @@ -13,23 +13,29 @@ as PPM (parts per million). The value is reported as a :doc:`number `. + Get the current carbon monoxide levels as PPM. + + :returns: + Promise that resolves to the current value as a + :doc:`number `. .. sourcecode:: js - console.log('CO is:', thing.carbonMonoxide); + console.log('CO is:', await thing.carbonMonoxide()); -.. js:attribute:: co() +.. js:function:: co() - Get the current carbon monoxide levels as PPM. Reported as a - :doc:`number `. + Get the current carbon monoxide levels as PPM. + + :returns: + Promise that resolves to the current value as a + :doc:`number `. .. sourcecode:: js - console.log('CO is:', thing.co); + console.log('CO is:', await thing.co()); Events ------ @@ -39,6 +45,8 @@ Events The carbon monoxide level has changed. Payload is the new PPM as a :doc:`number `. + Example: + .. sourcecode:: js thing.on('carbonMonoxideChanged', v => console.log('Changed to:', v)); diff --git a/docs/sensors/contact.rst b/docs/sensors/contact.rst index 8320c48..b033446 100644 --- a/docs/sensors/contact.rst +++ b/docs/sensors/contact.rst @@ -8,38 +8,55 @@ window is open. .. sourcecode:: js if(thing.matches('cap:contact')) { - console.log('Has contact:', thing.contact); + console.log('Has contact:', await thing.contact()); } API --- -.. js:attribute:: contact +.. js:function:: contact() :doc:`Boolean ` representing if the sensor is currently detecting contact. + :returns: + Promise that resolves to if the sensor is detecting contact. + + Example: + .. sourcecode:: js - console.log('Contact is:', thing.contact); + if(await thing.contact()) { + console.log('Thing has detected contact'); + } -.. js:attribute:: open +.. js:function:: isOpen() :doc:`Boolean ` representing if the sensor is currently open (not detecting contact). + :returns: + Promise that resolves to if the sensor is in an open state. + + Example: + .. sourcecode:: js - console.log('Is open:', thing.open); + console.log('Is open:', await thing.isOpen()); -.. js:attribute:: closed +.. js:function:: isClosed() :doc:`Boolean ` representing if the sensor is currently closed (detecting contact). + :returns: + Promise that resolves to if the sensir is in a closed state. + + Example: + .. sourcecode:: js - console.log('Is closed:', thing.closed); + console.log('Is closed:', await thing.isClosed()); Events ------ @@ -48,6 +65,8 @@ Events The contact value has changed. Payload is the new contact state as a :doc:`boolean `. + Example: + .. sourcecode:: js thing.on('contactChanged', v => console.log('Contact is now:', c)); @@ -56,6 +75,8 @@ Events The sensor has detected it is does not have contact and is now opened. + Example: + .. sourcecode:: js thing.on('opened', v => console.log('Sensor is now open')); @@ -64,6 +85,8 @@ Events The sensor has detect it has contact is is now closed. + Example: + .. sourcecode:: js thing.on('closed', v => console.log('Sensor is now closed')); diff --git a/docs/sensors/illuminance.rst b/docs/sensors/illuminance.rst index 3d9e0c4..cb420cb 100644 --- a/docs/sensors/illuminance.rst +++ b/docs/sensors/illuminance.rst @@ -8,19 +8,26 @@ that read light levels. .. sourcecode:: js if(thing.matches('cap:illuminance')) { - console.log('Light level:', thing.illuminance); + console.log('Light level:', await thing.illuminance()); } API --- -.. js:attribute:: illuminance +.. js:function:: illuminance() Get the current :doc:`illuminance `. + :returns: + Promise that resolves to the current + :doc:`illuminance `. + + Example: + .. sourcecode:: js - console.log('Light level:', thing.illuminance); + const lightLevel = await thing.illuminance(); + console.log('Light level:', lightLevel.lux); Events ------ @@ -29,6 +36,8 @@ Events The illuminance has changed. Payload is the new :doc:`illuminance `. + Example: + .. sourcecode:: js thing.on('illuminanceChanged', v => console.log('Changed to:', v)); diff --git a/docs/sensors/motion.rst b/docs/sensors/motion.rst index 9f1c75d..e0b272d 100644 --- a/docs/sensors/motion.rst +++ b/docs/sensors/motion.rst @@ -6,7 +6,7 @@ This capability is used to mark sensors that monitor movement. .. sourcecode:: js if(thing.matches('cap:motion')) { - console.log('Detected motion:', thing.motion); + console.log('Detected motion:', await thing.motion()); thing.on('movement', () => console.log('Motion detected')); thing.on('inactivity', () => console.log('Inactivity detected')); @@ -15,10 +15,15 @@ This capability is used to mark sensors that monitor movement. API --- -.. js:attribute:: motion +.. js:function:: motion() - Get the motion status. :doc:`Boolean ` indicating if - movement is currently detected. + Get the motion status. + + :returns: + Promise that resolves to a :doc:`boolean ` indicating + if movement is currently detected. + + Example: .. sourcecode:: js diff --git a/docs/sensors/pm10.rst b/docs/sensors/pm10.rst index 6316300..c0376ea 100644 --- a/docs/sensors/pm10.rst +++ b/docs/sensors/pm10.rst @@ -7,20 +7,25 @@ between 2.5 and 10 micrometers (μm). .. sourcecode:: js if(thing.matches('cap:pm10')) { - console.log('PM10:', thing.pm10); + console.log('PM10:', await thing.pm10()); } API --- -.. js:attribute:: pm10 +.. js:function:: pm10() - Get the current PM10 as micrograms per cubic meter (μg/m³). Value is a - :doc:`number `. + Get the current PM10 as micrograms per cubic meter (μg/m³). + + :returns: + The current value as micrograms per cubic meter (μg/m³). Value is a + :doc:`number `. + + Example: .. sourcecode:: js - console.log('PM10:', thing.pm10); + console.log('PM10:', await thing.pm10()); Events ------ @@ -30,6 +35,8 @@ Events The PM10 has changed. Payload is a :doc:`number ` with the new PM10 as micrograms per cubic meter (μg/m³). + Example: + .. sourcecode:: js thing.on('pm10Changed', v => console.log('Changed to:', v)); diff --git a/docs/sensors/pm2.5.rst b/docs/sensors/pm2.5.rst index 69ea438..5643737 100644 --- a/docs/sensors/pm2.5.rst +++ b/docs/sensors/pm2.5.rst @@ -7,29 +7,41 @@ This capability is used to mark sensors that monitor fine particulate matter .. sourcecode:: js if(thing.matches('cap:pm2.5')) { - console.log('PM2.5:', thing.pm2_5); + console.log('PM2.5:', await thing.pm2_5()); } API --- -.. js:attribute:: pm2_5 +.. js:function:: pm2_5() Get the current PM2.5 as micrograms per cubic meter (μg/m³). Value is a :doc:`number `. + :returns: + The current value as micrograms per cubic meter (μg/m³). Value is a + :doc:`number `. + + Example: + .. sourcecode:: js - console.log('PM2.5:', thing.pm2_5); + console.log('PM2.5:', await thing.pm2_5()); -.. js:attribute:: 'pm2.5' +.. js:function:: 'pm2.5'() Get the current PM2.5 as micrograms per cubic meter (μg/m³). Value is a :doc:`number `. + :returns: + The current value as micrograms per cubic meter (μg/m³). Value is a + :doc:`number `. + + Example: + .. sourcecode:: js - console.log('PM2.5:', thing['pm2.5']); + console.log('PM2.5:', await thing['pm2.5']()); Events ------ @@ -39,6 +51,8 @@ Events The PM2.5 has changed. Payload is a :doc:`number ` with the new PM2.5 as micrograms per cubic meter (μg/m³). + Example: + .. sourcecode:: js thing.on('pm2.5Changed', v => console.log('Changed to:', v)); diff --git a/docs/sensors/power-consumed.rst b/docs/sensors/power-consumed.rst index 016f974..89a74f6 100644 --- a/docs/sensors/power-consumed.rst +++ b/docs/sensors/power-consumed.rst @@ -6,20 +6,28 @@ This capability is used to mark sensors that report power consumed by something. .. sourcecode:: js if(thing.matches('cap:power-consumed')) { - console.log('Power consumed:', thing.powerConsumed.wattHours); + const powerConsumed = await thing.powerConsumed(); + console.log('Power consumed:', powerConsumed.wattHours); } API --- -.. js:attribute:: powerConsumed +.. js:function:: powerConsumed - Get the current amount of power consumed, reports values as - :doc:`energy `. + Get the current amount of power consumed. + . + + :returns: + Promise that resolves to the amount of power consumed as + :doc:`energy `. + + Example: .. sourcecode:: js - console.log('Power consumed:', thing.powerConsumed.wattHours); + const powerConsumed = await thing.powerConsumed(); + console.log('Power consumed:', powerConsumed.wattHours); Events ------ @@ -29,6 +37,8 @@ Events The amount of power consumed has changed. Payload is the power consumed as :doc:`energy `. + Example: + .. sourcecode:: js thing.on('powerConsumedChanged', v => console.log('Changed to:', v)); diff --git a/docs/sensors/power-load.rst b/docs/sensors/power-load.rst index 5dfd24b..86587b4 100644 --- a/docs/sensors/power-load.rst +++ b/docs/sensors/power-load.rst @@ -7,20 +7,27 @@ This capability is used to mark sensors that report power load, that is the .. sourcecode:: js if(thing.matches('cap:power-load')) { - console.log('Power load:', thing.powerLoad.watts); + const powerLoad = await thing.powerLoad(); + console.log('Power load:', powerLoad.watts); } API --- -.. js:attribute:: powerLoad +.. js:function:: powerLoad() - Get the current amount of power used, reports values as - :doc:`power `. + Get the current amount of power being used. + + :returns: + Promise that resolves to the current amount of power used as a + :doc:`power `. + + Example: .. sourcecode:: js - console.log('Power load:', thing.powerLoad.watts); + const powerLoad = await thing.powerLoad(); + console.log('Power load:', powerLoad.watts); Events ------ @@ -30,6 +37,8 @@ Events The amount of power being used has changed. Payload is the power load as :doc:`power `. + Example: + .. sourcecode:: js thing.on('powerLoadChanged', v => console.log('Changed to:', v)); diff --git a/docs/sensors/relative-humidity.rst b/docs/sensors/relative-humidity.rst index 39ab306..2a3113b 100644 --- a/docs/sensors/relative-humidity.rst +++ b/docs/sensors/relative-humidity.rst @@ -7,28 +7,25 @@ the air. .. sourcecode:: js if(thing.matches('cap:relative-humidity')) { - console.log('RH:', thing.relativeHumidity); + console.log('RH:', await thing.relativeHumidity()); } API --- -.. js:attribute:: relativeHumidity +.. js:function:: relativeHumidity() Get the current relative humidity as a :doc:`percentage `. - .. sourcecode:: js - - console.log('RH:', thing.relativeHumidity); - - -.. js:attribute:: rh + :returns: + Promise that resolves to the current relative humidity as a + :doc:`percentage `. - Get the current relative humidity as a :doc:`percentage `. + Example: .. sourcecode:: js - console.log('RH:', thing.rh); + console.log('RH:', await thing.relativeHumidity()); Events ------ @@ -38,6 +35,8 @@ Events The relative humidity has changed. Payload is the new humidity as a :doc:`percentage `. + Example: + .. sourcecode:: js thing.on('relativeHumidityChanged', v => console.log('Changed to:', v)); diff --git a/docs/sensors/temperature.rst b/docs/sensors/temperature.rst index 4834b23..362ed79 100644 --- a/docs/sensors/temperature.rst +++ b/docs/sensors/temperature.rst @@ -6,16 +6,23 @@ This capability is used to mark sensors that report a :doc:`temperature `. + :returns: + Promise that resolves to the current + :doc:`temperature `. + + Example: + .. sourcecode:: js console.log('Temperature is:', thing.temperature); @@ -27,6 +34,8 @@ Events The temperature has changed. Payload is the new :doc:`temperature `. + Example: + .. sourcecode:: js thing.on('temperatureChanged', temp => console.log('Temp changed to:', temp)); diff --git a/docs/sensors/voltage.rst b/docs/sensors/voltage.rst index d5e82c6..0cab0c6 100644 --- a/docs/sensors/voltage.rst +++ b/docs/sensors/voltage.rst @@ -7,7 +7,8 @@ This capability is used to mark sensors that report the .. sourcecode:: js if(thing.matches('cap:voltage')) { - console.log('Voltage:', thing.voltage.volts); + const voltage = await thing.voltage(); + console.log('Voltage:', voltage.volts); } API @@ -17,9 +18,15 @@ API Get the current :doc:`voltage `. + :returns: + Promise that resolves to the current :doc:`voltage `. + + Example: + .. sourcecode:: js - console.log('Voltage:', thing.voltage.volts); + const voltage = await thing.voltage(); + console.log('Voltage:', voltage.volts); Events ------ @@ -29,6 +36,8 @@ Events The voltage has changed. Payload is the new voltage as a :doc:`voltage `. + Example: + .. sourcecode:: js thing.on('voltageChanged', v => console.log('Changed to:', v)); diff --git a/lights/color-temperature.js b/lights/color-temperature.js index fad6492..5c7bbe0 100644 --- a/lights/color-temperature.js +++ b/lights/color-temperature.js @@ -27,17 +27,17 @@ module.exports = Thing.mixin(Parent => class extends Parent { .done(); } - get colorTemperatureRange() { + colorTemperatureRange() { const range = this[colorTemperatureRange]; if(! range) { - throw new Error('Temperature range has not been set'); + return Promise.reject(new Error('Temperature range has not been set')); } - return { + return Promise.resolve({ min: range.min, max: range.max - }; + }); } updateColorTemperatureRange(min, max) { diff --git a/lights/colorable.js b/lights/colorable.js index 1045d3c..d5573af 100644 --- a/lights/colorable.js +++ b/lights/colorable.js @@ -49,19 +49,23 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(LightState) { return this.setColor(color, duration); } - return this.getState('color'); + return Promise.resolve(this.getState('color')); } setColor(color0, duration0=Light.DURATION) { - if(typeof color0 === 'undefined') throw new Error('Color must be specified'); - color0 = color(color0); - - const options = { - duration: duration(duration0) - }; - - return Promise.resolve(this.changeColor(color0, options)) - .then(() => this.getState('color')); + try { + if(typeof color0 === 'undefined') throw new Error('Color must be specified'); + color0 = color(color0); + + const options = { + duration: duration(duration0) + }; + + return Promise.resolve(this.changeColor(color0, options)) + .then(() => this.getState('color')); + } catch(ex) { + return Promise.reject(ex); + } } updateColor(color) { diff --git a/lights/dimmable.js b/lights/dimmable.js index 8bd5781..0e02cc9 100644 --- a/lights/dimmable.js +++ b/lights/dimmable.js @@ -48,25 +48,30 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Brightness, Lig * Get or change the brightness of this light. */ brightness(brightness, duration=Light.DURATION) { - let currentBrightness = super.brightness(); - - if(typeof brightness !== 'undefined') { - brightness = change(brightness); - - let powerOn = true; - let toSet; - if(brightness.isIncrease) { - toSet = currentBrightness + brightness.value; - } else if(brightness.isDecrease) { - toSet = currentBrightness - brightness.value; - powerOn = false; - } else { - toSet = brightness.value; + try { + let currentBrightness = this.getState('brightness', 0); + + if(typeof brightness !== 'undefined') { + brightness = change(brightness); + + let powerOn = true; + let toSet; + if(brightness.isIncrease) { + toSet = currentBrightness + brightness.value; + } else if(brightness.isDecrease) { + toSet = currentBrightness - brightness.value; + powerOn = false; + } else { + toSet = brightness.value; + } + + return this.setBrightness(toSet, duration, powerOn); } - return this.setBrightness(toSet, duration, powerOn); - } - return currentBrightness; + return Promise.resolve(currentBrightness); + } catch(ex) { + return Promise.reject(ex); + } } increaseBrightness(amount, duration=Light.DURATION) { @@ -78,16 +83,20 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Brightness, Lig } setBrightness(brightness, duration0=Light.DURATION, powerOn=true) { - if(typeof brightness === 'undefined') throw new Error('Brightness must be specified'); - brightness = percentage(brightness, { min: 0, max: 100 }); - - const options = { - duration: duration(duration0), - powerOn: brightness <= 0 ? false : boolean(powerOn) - }; - - return Promise.resolve(this.changeBrightness(brightness, options)) - .then(() => this.getState('brightness', 0)); + try { + if(typeof brightness === 'undefined') throw new Error('Brightness must be specified'); + brightness = percentage(brightness, { min: 0, max: 100 }); + + const options = { + duration: duration(duration0), + powerOn: brightness <= 0 ? false : boolean(powerOn) + }; + + return Promise.resolve(this.changeBrightness(brightness, options)) + .then(() => this.getState('brightness', 0)); + } catch(ex) { + return Promise.reject(ex); + } } /** diff --git a/lights/fading.js b/lights/fading.js index 9a59aa9..1cfe758 100644 --- a/lights/fading.js +++ b/lights/fading.js @@ -21,11 +21,11 @@ module.exports = Thing.mixin(Parent => class extends Parent { .done(); } - get maxChangeTime() { - return this[maxChangeTime]; + maxChangeTime() { + return Promise.resolve(this[maxChangeTime]); } - set maxChangeTime(t) { + updateMaxChangeTime(t) { this[maxChangeTime] = duration(t); } }); diff --git a/lights/light-state.js b/lights/light-state.js index f486935..f497a81 100644 --- a/lights/light-state.js +++ b/lights/light-state.js @@ -28,11 +28,15 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(RestorableState * @param {object} state */ changeState(state) { - return Promise.resolve(super.changeState(state)) - .then(() => { - const stateCopy = Object.assign({}, state); - this.mapLightState(stateCopy); - return this.setLightState(stateCopy); - }); + try { + return Promise.resolve(super.changeState(state)) + .then(() => { + const stateCopy = Object.assign({}, state); + this.mapLightState(stateCopy); + return this.setLightState(stateCopy); + }); + } catch(ex) { + return Promise.reject(ex); + } } }); diff --git a/media/adjustable-volume.js b/media/adjustable-volume.js index 866569e..419f2fd 100644 --- a/media/adjustable-volume.js +++ b/media/adjustable-volume.js @@ -36,23 +36,28 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Volume) { } volume(volume) { - let currentVolume = super.volume(); - - if(typeof volume !== 'undefined') { - volume = change(volume); - - let toSet; - if(volume.isIncrease) { - toSet = currentVolume + volume.value; - } else if(volume.isDecrease) { - toSet = currentVolume - volume.value; - } else { - toSet = volume.value; + try { + let currentVolume = this.getState('volume'); + + if(typeof volume !== 'undefined') { + volume = change(volume); + + let toSet; + if(volume.isIncrease) { + toSet = currentVolume + volume.value; + } else if(volume.isDecrease) { + toSet = currentVolume - volume.value; + } else { + toSet = volume.value; + } + + return this.setVolume(toSet); } - return this.setVolume(toSet); - } - return currentVolume; + return Promise.resolve(currentVolume); + } catch(ex) { + return Promise.reject(ex); + } } increaseVolume(amount) { @@ -64,10 +69,14 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Volume) { } setVolume(volume) { - volume = percentage(volume, { min: 0, max: 100 }); + try { + volume = percentage(volume, { min: 0, max: 100 }); - return Promise.resolve(this.changeVolume(volume)) - .then(() => this.state.volume); + return Promise.resolve(this.changeVolume(volume)) + .then(() => this.state.volume); + } catch(ex) { + return Promise.reject(ex); + } } changeVolume(volume) { diff --git a/media/muteable.js b/media/muteable.js index 6848e14..d518b50 100644 --- a/media/muteable.js +++ b/media/muteable.js @@ -46,10 +46,14 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Muted) { } setMuted(muted) { - muted = boolean(muted, true, 'Boolean for new muted state is required'); + try { + muted = boolean(muted, true, 'Boolean for new muted state is required'); - return Promise.resolve(this.changeMuted(muted)) - .then(() => this.state.muted); + return Promise.resolve(this.changeMuted(muted)) + .then(() => this.state.muted); + } catch(ex) { + return Promise.reject(ex); + } } mute() { @@ -61,7 +65,8 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Muted) { } toggleMuted() { - return this.setMuted(! this.muted()); + return this.muted() + .then(muted => this.setMuted(! muted)); } changeMuted(muted) { diff --git a/media/muted.js b/media/muted.js index d7262fe..b31bae6 100644 --- a/media/muted.js +++ b/media/muted.js @@ -30,7 +30,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { } muted() { - return this.getState('muted'); + return Promise.resolve(this.getState('muted')); } updateMuted(muted) { diff --git a/media/volume.js b/media/volume.js index 2c22e9f..050082a 100644 --- a/media/volume.js +++ b/media/volume.js @@ -22,7 +22,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) { } volume() { - return this.getState('volume'); + return Promise.resolve(this.getState('volume')); } updateVolume(volume) { diff --git a/sensors/carbon-dioxide.js b/sensors/carbon-dioxide.js index ba163b3..28d1f6f 100644 --- a/sensors/carbon-dioxide.js +++ b/sensors/carbon-dioxide.js @@ -32,11 +32,11 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'carbonDioxide' ]; } - get carbonDioxide() { + carbonDioxide() { return this.value('carbonDioxide'); } - get co2() { + co2() { return this.value('carbonDioxide'); } diff --git a/sensors/carbon-monoxide.js b/sensors/carbon-monoxide.js index 9c26f58..8e5c7f7 100644 --- a/sensors/carbon-monoxide.js +++ b/sensors/carbon-monoxide.js @@ -32,11 +32,11 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'carbonMonoxide' ]; } - get carbonMonoxide() { + carbonMonoxide() { return this.value('carbonMonoxide'); } - get co() { + co() { return this.value('carbonMonoxide'); } diff --git a/sensors/contact.js b/sensors/contact.js index 00d550c..bf4ef9c 100644 --- a/sensors/contact.js +++ b/sensors/contact.js @@ -34,16 +34,17 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'contact' ]; } - get contact() { + contact() { return this.value('contact'); } - get open() { - return ! this.contact; + isOpen() { + return this.contact() + .then(v => ! v); } - get closed() { - return this.contact; + isClosed() { + return this.contact(); } updateContact(contact) { diff --git a/sensors/illuminance.js b/sensors/illuminance.js index d08bce0..0766f85 100644 --- a/sensors/illuminance.js +++ b/sensors/illuminance.js @@ -26,7 +26,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'illuminance' ]; } - get illuminance() { + illuminance() { return this.value('illuminance'); } diff --git a/sensors/motion.js b/sensors/motion.js index 1b14bc6..7f2dfd7 100644 --- a/sensors/motion.js +++ b/sensors/motion.js @@ -35,7 +35,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'motion' ]; } - get motion() { + motion() { return this.value('motion'); } diff --git a/sensors/pm10.js b/sensors/pm10.js index dde4ec0..b57333a 100644 --- a/sensors/pm10.js +++ b/sensors/pm10.js @@ -26,7 +26,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'pm10' ]; } - get pm10() { + pm10() { return this.value('pm10'); } diff --git a/sensors/pm2_5.js b/sensors/pm2_5.js index 50d6b5f..15fc3de 100644 --- a/sensors/pm2_5.js +++ b/sensors/pm2_5.js @@ -32,11 +32,11 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'pm2.5' ]; } - get pm2_5() { + pm2_5() { return this.value('pm2.5'); } - get ['pm2.5']() { + ['pm2.5']() { return this.value('pm2.5'); } diff --git a/sensors/power-consumed.js b/sensors/power-consumed.js index 08649c2..94f5056 100644 --- a/sensors/power-consumed.js +++ b/sensors/power-consumed.js @@ -26,7 +26,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'powerConsumed' ]; } - get powerConsumed() { + powerConsumed() { return this.value('powerConsumed'); } diff --git a/sensors/power-load.js b/sensors/power-load.js index 99e72ef..39d2dbe 100644 --- a/sensors/power-load.js +++ b/sensors/power-load.js @@ -26,7 +26,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'powerLoad' ]; } - get powerLoad() { + powerLoad() { return this.value('powerLoad'); } diff --git a/sensors/relative-humidity.js b/sensors/relative-humidity.js index c1454af..c9c1c78 100644 --- a/sensors/relative-humidity.js +++ b/sensors/relative-humidity.js @@ -32,11 +32,11 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'relativeHumidity' ]; } - get relativeHumidity() { + relativeHumidity() { return this.value('relativeHumidity'); } - get rh() { + rh() { return this.value('relativeHumidity'); } diff --git a/sensors/sensor.js b/sensors/sensor.js index 5523fb1..e9827f9 100644 --- a/sensors/sensor.js +++ b/sensors/sensor.js @@ -19,7 +19,7 @@ module.exports = Thing.type(Parent => class extends Parent.with(State) { } value(sensorType) { - return this.getState(sensorType); + return Promise.resolve(this.getState(sensorType)); } get sensorTypes() { @@ -31,7 +31,7 @@ module.exports = Thing.type(Parent => class extends Parent.with(State) { for(const type of this.sensorTypes) { result[type] = this.getState(type); } - return result; + return Promise.resolve(result); } updateValue(sensorType, value) { diff --git a/sensors/temperature.js b/sensors/temperature.js index bae741c..2e4d338 100644 --- a/sensors/temperature.js +++ b/sensors/temperature.js @@ -26,7 +26,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'temperature' ]; } - get temperature() { + temperature() { return this.value('temperature'); } diff --git a/sensors/voltage.js b/sensors/voltage.js index b4896db..1624782 100644 --- a/sensors/voltage.js +++ b/sensors/voltage.js @@ -26,7 +26,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Sensor) { return [ ...super.sensorTypes, 'voltage' ]; } - get voltage() { + voltage() { return this.value('voltage'); }