• Stars
    star
    106
  • Rank 314,113 (Top 7 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SDK for calling the SmartThings API from JavaScript and TypeScript applications

SmartThings Core SDK

The SmartThings Core SDK is a wrapper designed to simplify the use of the SmartThings REST API from JavaScript and TypeScript applications. This is the very first release of this SDK and should be considered a work in progress. Changes may still be made that are not backwardly compatible.

Installation

npm install @smartthings/core-sdk

Importing

NodeJS:

const {SmartThingsClient} = require('@smartthings/core-sdk')

Or ES2015+:

import {SmartThingsClient} from '@smartthings/core-sdk'

Example Usage

Substitue your Personal Access Token (PAT) with at least the r:locations:* scope for {YOUR-PAT-TOKEN} in the following code.

const {SmartThingsClient, BearerTokenAuthenticator} = require('@smartthings/core-sdk')
const client = new SmartThingsClient(new BearerTokenAuthenticator('{YOUR-PAT-TOKEN}'))

client.locations.list().then(locations => {
    console.log(`Found ${locations.length} locations`)
})

Logging

There is some logging done of requests and responses made to the API. The default logger does nothing but you can pass your own. Logging is done via a generic interface so you can use whatever logger you want in your application.

First, write an implementation of the Logger interface defined in logger.ts which proxies to your logger. For example:

import { Logger as WinstonLogger } from 'winston'
import { Logger } from '@smartthings/core-sdk'


export class WinstonLoggerProxy implements Logger {
	proxy: WinstonLogger
	level: string

	constructor(winstonLogger) {
		this.level = proxy.level
	}

	trace(message: any, ...args: any[]): void {
		// Winston doesn't have a "trace" level but it has a "silly" level in the same place.
		proxy.silly(message, args)
	}

	debug(message: any, ...args: any[]): void {
		proxy.debug(message, args)
	}

	info(message: any, ...args: any[]): void {
		proxy.info(message, args)
	}

	...

	isTraceEnabled(): boolean {
		return proxy.isSillyEnabled()
	}

	...
}

Then, when you create your SmartThingsClient, pass this in via the config parameter.

const config = {
	logger: new WinstonLoggerProxy(myWinstonLoggerInstance)
}
const client = new SmartThingsClient(new BearerTokenAuthenticator('{YOUR-PAT-TOKEN}'), config)

Reference

Authenticators

This SDK supports multiple ways of authenticating with the SmartThings platform. The currently available authenticators are:

  • BearerTokenAuthenticator -- Authenticator that is instantiated with any valid bearer token. This is the authenticator you would use with a PAT (Personal Access) Token.

  • RefreshTokenAuthenticator -- Authenticator that is instantiated with a bearer token and a RefreshTokenStore that provides methods for retrieving and storing access and refresh tokens. When this authenticator is used the API will automatically refresh expired access tokens, save the new tokens, and retry the original request.

  • SequentialRefreshTokenAuthenticator

Endpoints

  • apps -- A SmartApp can be an AWS lambda function or WebHook endpoint. Like to code interface here, link to wiki page description here

  • capabilities - Operations to read standard Capability definitions as well as create and modify custom Capabilities. Link to code interface here, link to wiki page here.

  • deviceProfiles - A Device Profile contains the Components, Capabilities, and metadata (ID, name, ownership, etc.) that define a SmartThings Device. Link to code interface here, link to wiki page here

  • devices - Operations to access, control, create, update, and delete Devices. Like to code interface here, link to wiki page here

  • history - Operations to query event history. Link to code interface here.

  • installedApps - Apps are installed by users. Link to code interface here, link to wiki page description here

  • locations - Locations can include Hubs, Devices, and Automations. Link to code interface here, link to wiki page description here

  • modes - Operations to change the current Mode of a Location. Link to code interface here, link to wiki page description here

  • notifications - Operations to send push notifications to SmartThings mobile app. Link to code interface here, link to wiki page description here

  • organizations - Operations to list and get Organizations. Link to code interface here. Future feature. Not yet supported.

  • presentation - Operations to query and create Device Configurations and Presentations. Link to code interface here.

  • rooms - Operations related to Rooms, a grouping of Devices within a Location. Link to code interface here, link to wiki page description here

  • rules - Operations for working with Rules. Rules allow you to create Automations that can operate on SmartThings connected Devices. Link to code interface here, link to wiki page description here

  • scenes - Operations to list and execute Scenes. Currently this endpoint does not support creating or updating Scenes. Link to code interface here, link to wiki page description here

  • schedules - Operations for scheduling future executions for use in SmartApps. Link to code interface here, link to wiki page description here

  • schema - Operations for ST Schema connectors and installed instances, along with operations to list the Devices owned by each installed instance. Link to code interface here, link to wiki page description here

  • services - Operations to query for and subscribe to location service data, currently consisting of current weather conditions, weather forecast, and air quality data. Link to code interface here, link to wiki page description here

  • subscriptions - Operations for subscribing to events, for use in SmartApps and API Access apps. Link to code interface here, link to wiki page description here

More Repositories

1

SmartThingsPublic

SmartThings open-source DeviceType Handlers and SmartApps code
Groovy
2,533
star
2

SmartThingsEdgeDrivers

Lua
229
star
3

smartthings-cli

Command-line Interface for the SmartThings APIs.
TypeScript
203
star
4

smartapp-sdk-nodejs

Javascript/NodeJS SDK to create SmartThings SmartApps
JavaScript
136
star
5

st-device-sdk-c

SmartThings SDK for Direct Connected Devices for C
C
117
star
6

st-device-sdk-c-ref

SmartThings SDK Reference for Direct Connected Devices for C
C
113
star
7

Code

DEPRECATED A collection of code examples from the SmartThings team, and the community
Groovy
81
star
8

weather-color-light-smartapp-nodejs

This SmartApp sets the color of a light based on the weather forecast.
JavaScript
53
star
9

smartapp-sdk-java

A collection of consumer-oriented Java (JVM) libraries for creating SmartApps and using the public API
Java
49
star
10

cli-example-nodejs

An example CLI to interact with SmartThings-connected devices, written in Node.js
JavaScript
37
star
11

st-schema-nodejs

ST Schema helper library for NodeJS
JavaScript
33
star
12

app-examples

JavaScript
12
star
13

example-lifx-nodejs-web-connector

LIFX C2C connector implementation as NodeJS web service
JavaScript
12
star
14

st-schema-connectors

Example connectors written for ST Schema
JavaScript
10
star
15

api-app-subscription-example-js

Example API Access SmartApp that shows the state and allows control of devices
JavaScript
9
star
16

api-app-minimal-example-js

Simple API Access integration that allows scenes to be executed
JavaScript
8
star
17

acme-control-panel-example

Example SmartApp that creates C2C devices as well as providing scene and device control
JavaScript
8
star
18

st-schema-oauth-example

Compete ST Schema connector example including OAuth server and virtual device web app
JavaScript
8
star
19

dynamodb-context-store-nodejs

Stores SmartApp configuration and auth tokens for use in app-initiated calls
JavaScript
7
star
20

generator-smartthings

Yeoman generator to bootstrap a SmartThings Cloud SDK-based app
JavaScript
6
star
21

MyCloudToCloudSchemaConnection

JavaScript
5
star
22

homebrew-smartthings

Ruby
4
star
23

smartapp-example-no-devices-nodejs-lambda

Give Lambda SmartApps a try without any physical devices.
JavaScript
4
star
24

example-lifx-lambda-connector

Imports LIFX devices into SmartThings
JavaScript
3
star
25

smartapp-example-open-close-nodets

The Typescript version of the Open/Close example SmartApp.
TypeScript
3
star
26

edge-cli-plugin

TypeScript
3
star
27

st-schema-simple-example-js

Very simple ST-Schema connector that creates one device
JavaScript
2
star
28

smartapp-example-every-setting-nodejs

This SmartApp has multiple configuration pages that contain examples of all setting types.
JavaScript
2
star
29

firestore-context-store-nodejs

Stores SmartApp configuration and auth tokens for use in app-initiated calls
JavaScript
2
star
30

github-actions

A library of reusable workflows
2
star
31

st-schema-callback-example-js

ST Schema virtual device example with proactive state updates
JavaScript
2
star
32

file-context-store-nodejs

File-based context store for SmartApps
JavaScript
1
star
33

device-scene-example-nodejs

Example SmartApp that stores installed app context makes callback to the SmartThings platform
JavaScript
1
star
34

slack-nodejs-workshop

JavaScript
1
star
35

smartapp-example-open-close-nodejs

This SmartApp turns on and off a light when something opens and closes.
JavaScript
1
star
36

drlc-prototype

JavaScript
1
star
37

dummy-oauth-server

OAuth2 server intended for testing clients, especially ST Schema connectors
1
star