Constructor
new BrainDevice()
Properties
ID of the device
Name of the Device
Device description, possibly blank
User in Kramer Control that created the device in the Space
Date that this device was added to the Space in Kramer Control
Simplified device driver, for internal use, but feel free to examine if interested. Used by all the state/command methods internally.
Classes
Members
(static) STATE_CHANGED
Properties
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
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
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
Name of the event to stop listening from
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
Name of the event to watch
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
Command ID, command Name, or command object - throws BrainDevice.ErrorInvalidCommand if given a command ID or name that doesn't exist
Key/value object of params for the command
Throws
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
State ID or Name - throws BrainDevice.ErrorInvalidState if the ID/Name is not a defined custom state (must be defined in the KC Builder)
Any valid value
Throws
-
BrainDevice.ErrorNotSystemDevice if the device is not a system device
-
BrainDevice.ErrorInvalidState if ID/Name is not a defined custom state value