Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(notification): add simple SmartThings switch activation #1902

Merged
merged 22 commits into from
Feb 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
}
}