cap:switchable-power - switch power state

The switchable-power-capability is an extension to the power-capability for things that can also switch their power state.

if(thing.matches('cap:switchable-power')) {
  console.log('Power is', await thing.power());

  // Switch the thing on
  await thing.power(true);
}

Related capabilities: power, state

API

power([powerState])

Get or set the current power state.

Arguments:
  • powerState (boolean) – Optional boolean to change power state to.
Returns:

Promise when switching state, boolean if getting.

Example:

// Getting returns a boolean
const powerIsOn = await thing.power();

// Switching returns a promise
thing.power(false)
  .then(result => console.log('Power is now', result))
  .catch(err => console.log('Error occurred', err);
setPower(powerState)

Set the power of the thing.

Arguments:
  • powerState (boolean) – The new power state as a boolean.
Returns:

Promise that will resolve to the new power state.

Example:

thing.setPower(true)
  .then(result => console.log('Power is now', result))
  .catch(err => console.log('Error occurred', err);

await thing.setPower(true);
togglePower()

Toggle the power of the thing. Will use the currently detected power state and switch to the opposite.

Returns:Promise that will resolve to the new power state.

Example:

thing.togglePower()
  .then(result => console.log('Power is now', result))
  .catch(err => console.log('Error occurred', err);
turnOn()

Turn the thing on.

Returns:Promise that will resolve to the new power state.

Example:

thing.turnOn()
  .then(result => console.log('Power is now', result))
  .catch(err => console.log('Error occurred', err);
turnOff()

Turn the thing off.

Returns:Promise that will resolve to the new power state.

Example:

thing.turnOff()
  .then(result => console.log('Power is now', result))
  .catch(err => console.log('Error occurred', err);

Protected methods

changePower(power)

Abstract. Change the power of this thing. Called on the thing when of the power methods request a change. Implementations should call updatePower before resolving to indicate that a change has occurred.

Can be called with the same power state as is currently set.

Arguments:
  • power (boolean) – The new power of the thing as a boolean.
Returns:

Promise if asynchronous.

Implementing capability

The switchable-power-capability requires that the function changePower is implemented.

Example:

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

class Example extends Thing.with(SwitchablePower) {
  constructor() {
    super();

    // Make sure to initialize the power state via updatePower
  }

  changePower(power) {
    /*
     * This method is called whenever a power change is requested.
     *
     * Change the power here and return a Promise if the method is
     * asynchronous. Also call updatePower to indicate the new state
     * if not done by switching.
     */
     return switchWithPromise(power)
      .then(() => this.updatePower(power));
  }
}