BrainDevice


BrainDevice

Interface for a single device attached to a Brain. See BrainClient#getDevice on how to get an instance of this class for a single device, or BrainClient#getDevices to get all devices attached to the connected brain.

BrainDevices have three main purposes:

  • Enumerating properties about the device, such as id and name (properties are documented below under the constructor)
  • Providing access to commands (enumerate and send commands)
  • Providing access to states (enumerate and receive changes and set custom states)

Additional device overview information can be found the following tutorial:

Commands

See the following methods for more info on working with commands:

Related tutorial:

States

See the following methods for more info on working with states:

Related tutorial:

Listening for State Changes

State Changes are sent via the BrainDevice.STATE_CHANGED event. To listen state changes on the device, just attach an event listener, like this:

someDevice.on(BrainDevice.STATE_CHANGED, change => {
	console.log(`Device ${someDevice.id} > State ${change.id} is now "${change.normalizedValue}`);
})

It's important to realize the BrainDevice.STATE_CHANGED event fires for ALL states on the device. You must filter on the id property of the payload to see if the event represents a change of the state you are interested in.

What's in a state change payload? Glad you asked. Here you go:

const stateChangeExample = {
	id: "SECOND_STATE", // The ID of the state, usually a grokable string like this
	key: undefined, // The State Key - often unused, usually blank/undefined
	name: "Current Second", // Human-readable state name
	value: "58", // The current value of the state as a string
	normalizedValue: "58", // The normalized value of the state (normalized by the Brain), also a string, usually identical to the `value` but not always.
}

Also see related tutorial: Basics/Watching States

Nota Bene

NOTE: You should never call the constructor directly, devices will be created by the BrainClient when enumerating devices internally.

NOTE: This class is not exported directly, but accessible as BrainClient.BrainDevice if you ever need to access it for typechecking, etc.

Constructor

new BrainDevice()

Properties
id:string

ID of the device

name:string

Name of the Device

description:string

Device description, possibly blank

created_by:string

User in Kramer Control that created the device in the Space

created_date:string

Date that this device was added to the Space in Kramer Control

driver:object

Simplified device driver, for internal use, but feel free to examine if interested. Used by all the state/command methods internally.

Classes

ErrorInvalidCommand
ErrorInvalidState
ErrorNotSystemDevice

Members

(static) STATE_CHANGED

Properties
STATE_CHANGED:string

Static class property, event name that is emitted when a state on this device changes on the Brain. Use like: BrainDevice.STATE_CHANGED. See notes at the top of this file titled "Listening for State Changes.

Methods

(async) getCommand(key):object|null

Get an object describing the requested command, or null if if the command doesn't exist. This is the same object as returned from BrainDevice#getCommands as the value associated with each command ID.

NOTE: This command is now async, so you must await the result. This is async so we can JIT download the driver instead of downloading every driver for every device in a space.

An example return value from this method would look like:

{
	id: "SET_SYSTEM_USE",
	name: "Set System Use",
	params: { ... },
	states: { ... },
	category: { ... },
	capability: { ... },
}

The params key provides info for you as the programmer to know what params the command expects. See more discussion on this key below. The states, category, and capability keys are internal keys that provide internal config information.

The params object looks like:

{
	...
	POWER: {
		type: 'boolean',
		constraints: { ... },
	},
	SYSTEM: {
		state: { ... },
	},
	...
}

For a params object like the above, you would pass those params to BrainDevice#sendCommand like this:

device.sendCommand(SOME_COMMAND, {
	POWER: 'ON',
	SYSTEM: '50'
})

The above params object example shows two different types of params:

  • Dynamic (state-based)
  • Static (not state-based)

Dynamic (state-based) params change an associated state, and that state provides type hint info. Whereas static (non state-based) params do not change any states and contain all the type hint info in themselves as shown above.

See the associated examples/command-info.js for a complete example showing how to get the params from the command.

Also see related tutorial: Basics/Sending Commands

Parameters
key:string

Command ID or Name

Returns
Type
:object|null

Returns an object describing the command or null if the command doesn't exist

(async) getCommands()

Get hash of commands with keys being the ID and the values being the info about the command

NOTE: This command is now async, so you must await the result. This is async so we can JIT download the driver instead of downloading every driver for every device in a space.

Also see related tutorial: Basics/Sending Commands

See BrainDevice#getCommand for documentation on what each command looks like.

(async) getCustomStates()

Returns an object containing only custom states. If this is not the system device, returns null.

For more information on Custom States, see the "Device States" tutorial.

(async) getState(key):object|null

Gets information about the state, including current value and normalizedValue

Also see related tutorial: Basics/Device States

Parameters
key:string

State ID or State Name

Returns
Type
:object|null

Object describing the state or null if the state doesn't exist

(async) getStates():object

Get a hash of state IDs => state objects for this device

Also see related tutorial: Basics/Device States

Returns
Type
:object

Object containing keys of state IDs and values being the state info

isSystemDevice():boolean

Returns true if this is the system device for this Brain.

The System Device is a virtual device provided internally by the Brain and it provides core services such as time, weather, brain status, custom states, etc. Only the System Device can have custom states.

Returns
Type
:boolean

true if this is the system device

off(event, callback)

Remove an event listener from this device. If you pass the STATE_CHANGED event as the event name, the attached BrainClient will automatically unsubscribe from state changes for this device if there are no other event listeners for the STATE_CHANGED event.

This overrides EventEmitter's off function to intercept the event name, but still useses EventEmitter to handle events, so you can use the inheritted on from EventEmitter to start listening for changes again.

Parameters
event:string

Name of the event to stop listening from

callback:function

Your event handler callback (must be identical to what you passed to BrainDevice#on)

(async) on(event, callback)

Attach an event listener to this device. If you pass the STATE_CHANGED event as the event name, the attached BrainClient will automatically inform the brain to start sending state changes for this device.

Note: The STATE_CHANGED event fires for ALL states on the device. You must filter on the id property of the payload to see if the event represents a change of the state you are interested in.

Related: See notes at the top of this file titled "Listening for State Changes

This overrides EventEmitter's on function to intercept the event name, but still useses EventEmitter to handle events, so you can use the inheritted off from EventEmitter to stop listening for changes.

Parameters
event:string

Name of the event to watch

callback:function

Your event handler callback

(async) sendCommand(key, params)

Execute command and return any changed states.

Example Usage

device.sendCommand('SEND_SYSTEM_USE', {
	SYSTEM_STATE: 'ON'
});

Also see the related tutorial: Basics/Sending Commands

Parameters
key:string|object

Command ID, command Name, or command object - throws BrainDevice.ErrorInvalidCommand if given a command ID or name that doesn't exist

params:object

Key/value object of params for the command

Throws
Type
:BrainDevice.ErrorInvalidCommand

Throws BrainDevice.ErrorInvalidCommand if given ID/Name not a defined command

(async) setCustomState(key, value)

Set a custom state to a given value

NOTE: The concept of "Custom States" (and hence, this method) is only relevant on the System Device (BrainDevice#isSystemDevice must return true) otherwise calling this method will throw an error.

For more information on device states and custom states, see the Basics/Device States tutorial.

Parameters
key:string

State ID or Name - throws BrainDevice.ErrorInvalidState if the ID/Name is not a defined custom state (must be defined in the KC Builder)

value:any

Any valid value

Throws