Skip to content

Commit 661e05f

Browse files
authored
Merge pull request #1 from erodewald/improve-package
Improve readme, add .hound, add xo linter
2 parents 76341ed + 9ba6527 commit 661e05f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+6503
-3161
lines changed

.eslintrc.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

README.md

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,56 @@
1-
# SmartThings Javascript SDK
1+
# SmartThings SmartApp NodeJS SDK (preview)
2+
3+
<p align="center">
4+
<a href="https://www.npmjs.com/package/@smartthings/smartapp"><img src="https://badgen.net/npm/v/@smartthings/smartapp" /></a>
5+
<a href="https://www.npmjs.com/package/@smartthings/smartapp"><img src="https://badgen.net/npm/license/@smartthings/smartapp" /></a>
6+
<a href="https://status.badgen.net/"><img src="https://badgen.net/xo/status/@smartthings/smartapp" /></a>
7+
<a href="https://lgtm.com/projects/g/SmartThingsCommunity/smartapp-sdk-nodejs/context:javascript"><img alt="Language grade: JavaScript" src="https://img.shields.io/lgtm/grade/javascript/g/SmartThingsCommunity/smartapp-sdk-nodejs.svg?logo=lgtm&logoWidth=18"/></a>
8+
<a href="https://lgtm.com/projects/g/SmartThingsCommunity/smartapp-sdk-nodejs/alerts/"><img alt="Total alerts" src="https://img.shields.io/lgtm/alerts/g/SmartThingsCommunity/smartapp-sdk-nodejs.svg?logo=lgtm&logoWidth=18"/></a>
9+
<a href="https://snyk.io/test/github/SmartThingsCommunity/smartapp-sdk-nodejs?targetFile=package.json"><img src="https://snyk.io/test/github/SmartThingsCommunity/smartapp-sdk-nodejs/badge.svg?targetFile=package.json" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/SmartThingsCommunity/smartapp-sdk-nodejs?targetFile=package.json" style="max-width:100%;"></a>
10+
<a href="https://smartthingsdev.slack.com/messages/CG595N08N"><img src="https://badgen.net/badge//smartthingsdev?icon=slack" /></a>
11+
</p>
212

313
[Reference Documentation](doc/index.md)
414

5-
SDK that wraps the SmartThings REST API and reduces the amount of code necessary
6-
to write a SmartApp app. Supports both web-hook and AWS Lambda implementations.
7-
This is a pre-release version of the API and may change over
8-
time time.
15+
SDK that wraps the SmartThings REST API and reduces the amount of code necessary to write a SmartApp app. It supports both webhook and AWS Lambda implementations. This is a preview version of the API and will change over time time.
16+
17+
## Installation
18+
19+
```bash
20+
npm i @smartthings/smartapp --save
21+
```
22+
23+
## Importing
24+
25+
`NodeJS`:
926

10-
## Installation:
27+
```javascript
28+
const smartapp = require('@smartthings/smartapp')
1129
```
12-
npm install @smartthings/smartapp --save
30+
31+
Or, if you're transpiling to `ES6`/`ES2015`+:
32+
33+
```javascript
34+
import smartapp from '@smartthings/smartapp'
1335
```
1436

15-
## Key Features
16-
* Javascript API hides details of REST calls and authentication.
17-
* Event handler framework dispatches lifecycle evebnts to named event handlers.
18-
* Configuration page API simplifies page definition.
19-
* Integrated [i18n](https://www.npmjs.com/package/i18n) framework provides configuration page localization.
20-
* [Winston](https://www.npmjs.com/package/winston) framework manges log messages.
37+
## Highlights
38+
39+
- [x] Javascript API hides details of REST calls and authentication.
40+
- [x] Event handler framework dispatches lifecycle evebnts to named event handlers.
41+
- [x] Configuration page API simplifies page definition.
42+
- [x] Integrated [i18n](https://www.npmjs.com/package/i18n) framework provides configuration page localization.
43+
- [x] [Winston](https://www.npmjs.com/package/winston) framework manges log messages.
2144

2245
## Example
2346

2447
### Run as an AWS Lambda function
25-
Here's the equivalent of the origial SmartThings Groovy _Let There Be Light_ app that
26-
turns on and off a light when a door opens and closes, set up to run as a Lambda.
2748

28-
```
29-
require('@smartthings/smartapp');
30-
app.configureI18n()
49+
Here's the equivalent of the original SmartThings Groovy _Let There Be Light_ app that turns on and off a light when a door opens and closes, set up to run as a Lambda.
50+
51+
```javascript
52+
const smartapp = require('@smartthings/smartapp')
53+
smartapp.configureI18n()
3154
.page('mainPage', (page) => {
3255
page.section('sensors', (section) => {
3356
section.deviceSetting('contactSensor').capabilities(['contactSensor']);
@@ -37,26 +60,24 @@ app.configureI18n()
3760
});
3861
})
3962
.updated(() => {
40-
app.api.devices.unsubscribeAll().then(() => {
41-
app.api.devices.subscribe(app.config.contactSensor, 'contactSensor', 'contact', 'openCloseHandler');
63+
smartapp.api.devices.unsubscribeAll().then(() => {
64+
smartapp.api.devices.subscribe(smartapp.config.contactSensor, 'contactSensor', 'contact', 'openCloseHandler');
4265
});
4366
})
4467
.subscribedEventHandler('openCloseHandler', (event) => {
4568
const value = event.value === 'open' ? 'on' : 'off';
46-
app.api.devices.sendCommands(app.config.lights, 'switch', value);
69+
smartapp.api.devices.sendCommands(smartapp.config.lights, 'switch', value);
4770
});
48-
4971
exports.handle = (evt, context, callback) => {
50-
app.handleLambdaCallback(evt, context, callback);
72+
smartapp.handleLambdaCallback(evt, context, callback);
5173
};
5274
```
5375

5476
### Localization
5577

56-
Configuration page strings are specified in a separate locales/en.json file, which
57-
can be automatically created the first time you run the app. Here's a completed English localization file
58-
for the previous example:
59-
```
78+
Configuration page strings are specified in a separate `locales/en.json` file, which can be automatically created the first time you run the app. Here's a completed English localization file for the previous example:
79+
80+
```json
6081
{
6182
"pages.mainPage.name": "Let There Be Light",
6283
"pages.mainPage.sections.sensors.name": "When this door or window opens or closes",
@@ -71,12 +92,13 @@ for the previous example:
7192

7293
To run the app in a webserver rather than a lambda replace the `exports.handle = ...` function with an HTTP server
7394
with the public key file specified:
74-
```
95+
96+
```javascript
7597
const express = require('express');
7698
const bodyParser = require('body-parser');
7799
const server = module.exports = express();
78-
require('@smartthings/smartapp');
79-
app.publicKey(`@${process.env.HOME}/smartthings_rsa.pub`)
100+
const smartapp = require('@smartthings/smartapp');
101+
smartapp.publicKey(`@${process.env.HOME}/smartthings_rsa.pub`)
80102
.configureI18n()
81103
.page('mainPage', (page) => {
82104
page.section('sensors', (section) => {
@@ -87,17 +109,16 @@ app.publicKey(`@${process.env.HOME}/smartthings_rsa.pub`)
87109
});
88110
})
89111
.updated(() => {
90-
app.api.devices.unsubscribeAll().then(() => {
91-
app.api.devices.subscribe(app.config.contactSensor, 'contactSensor', 'contact', 'openCloseHandler');
112+
smartapp.api.devices.unsubscribeAll().then(() => {
113+
smartapp.api.devices.subscribe(smartapp.config.contactSensor, 'contactSensor', 'contact', 'openCloseHandler');
92114
});
93115
})
94116
.subscribedEventHandler('openCloseHandler', (event) => {
95117
const value = event.value === 'open' ? 'on' : 'off';
96-
app.api.devices.sendCommands(app.config.lights, 'switch', value);
118+
smartapp.api.devices.sendCommands(smartapp.config.lights, 'switch', value);
97119
});
98-
99120
server.use(bodyParser.json());
100121
server.post('/', function(req, response) {
101-
app.handleHttpCallback(req, response);
122+
smartapp.handleHttpCallback(req, response);
102123
});
103-
```
124+
```

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
'use strict';
1+
'use strict'
22

3-
const SmartApp = require('./lib/smart-app');
3+
const SmartApp = require('./lib/smart-app')
44

55
module.exports = (function (options = {}) {
6-
return new SmartApp(options);
7-
})();
6+
return new SmartApp(options)
7+
})()
88

9-
module.exports.default = Object.assign({}, module.exports);
9+
module.exports.default = Object.assign({}, module.exports)

lib/api.js

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
'use strict';
1+
'use strict'
22

3-
const Client = require('./platform/client');
4-
const Apps = require('./platform/apps');
5-
const DeviceProfiles = require('./platform/deviceprofiles');
6-
const Devices = require('./platform/devices');
7-
const InstalledApps = require('./platform/installedapps');
8-
const Locations = require('./platform/locations');
9-
const Modes = require('./platform/modes');
10-
const Scenes = require('./platform/scenes');
11-
const Schedules = require('./platform/schedules');
12-
const Subscriptions = require('./platform/subscriptions');
3+
const Client = require('./platform/client')
4+
const Apps = require('./platform/apps')
5+
const DeviceProfiles = require('./platform/deviceprofiles')
6+
const Devices = require('./platform/devices')
7+
const InstalledApps = require('./platform/installedapps')
8+
const Locations = require('./platform/locations')
9+
const Modes = require('./platform/modes')
10+
const Scenes = require('./platform/scenes')
11+
const Schedules = require('./platform/schedules')
12+
const Subscriptions = require('./platform/subscriptions')
1313

1414
module.exports = class SmartThingsApi {
15-
16-
constructor(options) {
17-
this.client = new Client(options);
18-
this.apps = new Apps(this);
19-
this.deviceProfiles = new DeviceProfiles(this);
20-
this.devices = new Devices(this);
21-
this.installedApps = new InstalledApps(this);
22-
this.locations = new Locations(this);
23-
this.modes = new Modes(this);
24-
this.scenes = new Scenes(this);
25-
this.schedules = new Schedules(this);
26-
this.subscriptions = new Subscriptions(this);
27-
this.installedAppId = options.installedAppId;
28-
this.locationId = options.locationId;
29-
}
30-
};
15+
constructor(options) {
16+
this.client = new Client(options)
17+
this.apps = new Apps(this)
18+
this.deviceProfiles = new DeviceProfiles(this)
19+
this.devices = new Devices(this)
20+
this.installedApps = new InstalledApps(this)
21+
this.locations = new Locations(this)
22+
this.modes = new Modes(this)
23+
this.scenes = new Scenes(this)
24+
this.schedules = new Schedules(this)
25+
this.subscriptions = new Subscriptions(this)
26+
this.installedAppId = options.installedAppId
27+
this.locationId = options.locationId
28+
}
29+
}

lib/pages/boolean-setting.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
'use strict';
1+
'use strict'
22

3-
const SectionSetting = require('./section-setting.js');
3+
const SectionSetting = require('./section-setting.js')
44

55
module.exports = class BooleanSetting extends SectionSetting {
6+
constructor(section, id) {
7+
super(section, id)
8+
this._type = 'BOOLEAN'
9+
}
610

7-
constructor(section, id) {
8-
super(section, id);
9-
this._type = 'BOOLEAN';
10-
}
11-
12-
image(value) {
13-
this._image = value;
14-
return this;
15-
}
16-
17-
submitOnChange(value) {
18-
this._submitOnChange = value;
19-
return this;
20-
}
21-
22-
toJson() {
23-
let result = super.toJson();
24-
if (result.defaultValue === undefined && result.required) {
25-
result.defaultValue = 'false';
26-
}
27-
if (this._image) {
28-
result.image = this._image;
29-
}
30-
if (this._submitOnChange) {
31-
result.submitOnChange = this._submitOnChange;
32-
}
33-
return result;
34-
}
35-
};
11+
image(value) {
12+
this._image = value
13+
return this
14+
}
15+
16+
submitOnChange(value) {
17+
this._submitOnChange = value
18+
return this
19+
}
20+
21+
toJson() {
22+
const result = super.toJson()
23+
if (result.defaultValue === undefined && result.required) {
24+
result.defaultValue = 'false'
25+
}
26+
27+
if (this._image) {
28+
result.image = this._image
29+
}
30+
31+
if (this._submitOnChange) {
32+
result.submitOnChange = this._submitOnChange
33+
}
34+
35+
return result
36+
}
37+
}

0 commit comments

Comments
 (0)