Skip to content

Commit

Permalink
feat(notification): add simple SmartThings switch activation (#1902)
Browse files Browse the repository at this point in the history
Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
  • Loading branch information
pmilano1 and jef committed Feb 7, 2021
1 parent b20bd4a commit c22c960
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .eslintrc.json
@@ -1,3 +1,4 @@
{
"extends": "./node_modules/gts/"
"extends": "./node_modules/gts/",
"rules": { "prettier/prettier": ["error", { "endOfLine": "auto" }] }
}
9 changes: 9 additions & 0 deletions docs/reference/notification.md
Expand Up @@ -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 |
Expand Down
2 changes: 2 additions & 0 deletions dotenv-example
Expand Up @@ -94,6 +94,8 @@ SHOW_ONLY_MODELS=
SHOW_ONLY_SERIES=
SLACK_CHANNEL=
SLACK_TOKEN=
SMARTTHINGS_TOKEN=
SMARTTHINGS_SWITCH_LABEL=
SMTP_ADDRESS=
SMTP_PORT=
STORES=
Expand Down
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions src/notification/notification.ts
Expand Up @@ -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
Expand All @@ -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);
Expand Down
41 changes: 41 additions & 0 deletions 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;
}
}

0 comments on commit c22c960

Please sign in to comment.