From c22c960dc179f5ed74ae41a25178e6d2feed0bf9 Mon Sep 17 00:00:00 2001 From: "Peter J. Milanese" Date: Sun, 7 Feb 2021 15:32:18 -0500 Subject: [PATCH] feat(notification): add simple SmartThings switch activation (#1902) Co-authored-by: Jef LeCompte --- .eslintrc.json | 3 ++- docs/reference/notification.md | 9 +++++++ dotenv-example | 2 ++ package-lock.json | 39 ++++++++++++++++++++++++++++++ package.json | 1 + src/config.ts | 4 ++++ src/notification/notification.ts | 2 ++ src/notification/smartthings.ts | 41 ++++++++++++++++++++++++++++++++ 8 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/notification/smartthings.ts diff --git a/.eslintrc.json b/.eslintrc.json index f95bb333f0..00d52e39ec 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,3 +1,4 @@ { - "extends": "./node_modules/gts/" + "extends": "./node_modules/gts/", + "rules": { "prettier/prettier": ["error", { "endOfLine": "auto" }] } } diff --git a/docs/reference/notification.md b/docs/reference/notification.md index 9b96416e7b..afa4e48990 100644 --- a/docs/reference/notification.md +++ b/docs/reference/notification.md @@ -156,6 +156,15 @@ Generate token at [pushover.net/apps/build](https://pushover.net/apps/build). | `SLACK_CHANNEL` | Channel for posting | | `SLACK_TOKEN` | API token | +## SmartThings + +Generate token at [account.smartthings.com/tokens](https://account.smartthings.com/tokens). + +| Environment variable | Description | +|:---:|---| +| `SMARTTHINGS_TOKEN` | Access token | +| `SMARTTHINGS_SWITCH_LABEL` | Switch Label of switch to activate| + ## Telegram | Environment variable | Description | diff --git a/dotenv-example b/dotenv-example index e528f0495f..0d63bcea7e 100644 --- a/dotenv-example +++ b/dotenv-example @@ -94,6 +94,8 @@ SHOW_ONLY_MODELS= SHOW_ONLY_SERIES= SLACK_CHANNEL= SLACK_TOKEN= +SMARTTHINGS_TOKEN= +SMARTTHINGS_SWITCH_LABEL= SMTP_ADDRESS= SMTP_PORT= STORES= diff --git a/package-lock.json b/package-lock.json index da0e112698..9e1c2f21b1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -87,6 +87,45 @@ "integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==", "dev": true }, + "@bridgerakol/samsung-smart-api": { + "version": "2.8.1", + "resolved": "https://registry.npmjs.org/@bridgerakol/samsung-smart-api/-/samsung-smart-api-2.8.1.tgz", + "integrity": "sha512-Ue7o8GT/ph00kejX3h8w6kVh2LuRmIpLyhwZ8d0eJGcCNmn4mKGXJWZO5iMq8j1iqGozsmIa50lj79ZkDxTIRw==", + "requires": { + "axios": "^0.19.2" + }, + "dependencies": { + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "requires": { + "follow-redirects": "1.5.10" + } + }, + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + } + } + }, "@cliqz/adblocker": { "version": "1.19.0", "resolved": "https://registry.npmjs.org/@cliqz/adblocker/-/adblocker-1.19.0.tgz", diff --git a/package.json b/package.json index d4029e8810..01a2ccdc65 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ }, "homepage": "https://github.com/jef/streetmerchant#readme", "dependencies": { + "@bridgerakol/samsung-smart-api": "^2.8.1", "@doridian/puppeteer-page-proxy": "^1.2.11", "@jef/pushbullet": "^2.4.3", "@slack/web-api": "^6.0.0", diff --git a/src/config.ts b/src/config.ts index 025ad579db..3789adf5f7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -303,6 +303,10 @@ const notifications = { channel: envOrString(process.env.SLACK_CHANNEL), token: envOrString(process.env.SLACK_TOKEN), }, + smartthings: { + token: envOrString(process.env.SMARTTHINGS_TOKEN), + device: envOrString(process.env.SMARTTHINGS_SWITCH_LABEL), + }, soundPlayer: envOrString(process.env.SOUND_PLAYER), telegram: { accessToken: envOrString(process.env.TELEGRAM_ACCESS_TOKEN), diff --git a/src/notification/notification.ts b/src/notification/notification.ts index 2013fcb825..1e8550a538 100644 --- a/src/notification/notification.ts +++ b/src/notification/notification.ts @@ -15,6 +15,7 @@ import {sendTweet} from './twitter'; import {sendTwilioMessage} from './twilio'; import {sendTwitchMessage} from './twitch'; import {updateRedis} from './redis'; +import {activateSmartthingsSwitch} from './smartthings'; export function sendNotification(link: Link, store: Store) { // Priority @@ -24,6 +25,7 @@ export function sendNotification(link: Link, store: Store) { sendEmail(link, store); sendSms(link, store); // Non-priority + activateSmartthingsSwitch(); adjustPhilipsHueLights(); sendMqttMessage(link, store); sendPagerDutyNotification(link, store); diff --git a/src/notification/smartthings.ts b/src/notification/smartthings.ts new file mode 100644 index 0000000000..d68d163ab6 --- /dev/null +++ b/src/notification/smartthings.ts @@ -0,0 +1,41 @@ +import {SmartThings} from '@bridgerakol/samsung-smart-api'; +import {logger} from '../logger'; +import {config} from '../config'; + +const {smartthings} = config.notifications; + +export async function activateSmartthingsSwitch() { + if (!smartthings.token || !smartthings.device) { + return; + } + const st = new SmartThings(smartthings.token); + let match = false; + try { + await st.devices.getList().then(res => { + res.data.items.forEach( + async (item: {label: string; deviceId: string}) => { + if (smartthings.device === item.label) { + match = true; + const device_status = (await st.devices.getStatus(item.deviceId)) + .data.components.main.switch.switch.value; + if (device_status !== 'on') { + logger.debug(`Turning on ${smartthings.device}`); + st.devices.commands(item.deviceId, 'on'); + } + } + } + ); + }); + } catch (TypeError) { + logger.warn( + 'SmartThings : Problem getting data from hub, check SMARTTHINGS_TOKEN' + ); + return; + } + if (!match) { + logger.warn( + `SmartThings : No switch called ${smartthings.device}, check SMARTTHINGS_SWITCH_LABEL` + ); + return; + } +}