cap:mode - monitor mode

mode is used for things that have a mode that can be monitored.

if(thing.matches('cap:mode')) {
        console.log('Mode is', await thing.mode());

        thing.on('modeChanged', mode => console.log('Mode is now', mode));
}

API

mode()

Get the current mode of the thing.

Returns:Promises that resolves to a string indicating the identifier of the mode.

Example:

thing.mode()
        .then(mode => ...)
        .catch(...);

const mode = await thing.mode();
modes()

Get the modes that this thing supports.

Returns:Promise that will resolve to the modes as an array containing codes.

Example:

const modes = await thing.modes();

const firstMode = modes[0];
console.log('Id:', firstMode.id);
console.log('Description:', firstMode.description);

Events

modeChanged

The current mode has changed. Payload of the event is the current mode as a string.

thing.on('modeChanged', mode => console.log('Mode is now', mode));
modesChanged

The available modes have changed.

Protected methods

updateMode(mode)

Update the currently detected mode. Calling this method with a new mode will change the mode and trigger the mode event.

Arguments:
  • mode (string) – The id of the current mode.

Example:

this.updateMode('silent');
updateModes(modes)

Update the modes that are available for the thing.

Arguments:
  • modes (array) – Array of modes as codes. Entries in the array will be automatically converted to codes if possible.

Example:

this.updateModes([
        'idle',
        'silent: Silent speed',
        { id: 'auto', description: 'Autoselect speed' }
]);

Implementing capability

When implementing this capability call updateModes in the constructor or initCallback of the thing. updateMode should be used whenever the mode is changed.

Example:

const { Thing, Mode } = require('abstract-things');

class Example exends Thing.with(Mode) {
        initCallback() {
                return super.initCallback()
                        .then(() => this.updateModes(modesDetected));
        }
}