-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (69 loc) · 2.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import 'dotenv/config';
import * as hap from 'hap-nodejs';
import { getDeviceByName, executeCommand } from './src/smartthingsApi.js';
import { generateMacAddress } from './src/network.js';
const tv = await getDeviceByName(process.env.SMARTTHINGS_DEVICE_NAME);
const deviceId = tv.deviceId;
const Accessory = hap.Accessory;
const Characteristic = hap.Characteristic;
const CharacteristicEventTypes = hap.CharacteristicEventTypes;
const Service = hap.Service;
const accessoryUuid = hap.uuid.generate('smartthings.tv');
const accessory = new Accessory(
process.env.SMARTTHINGS_DEVICE_NAME,
accessoryUuid
);
const powerService = new Service.Switch();
powerService.name = 'Power';
const muteService = new Service.Switch();
muteService.name = 'Mute';
muteService.subtype = 'audioMute';
let currentTVPower = true;
let commandTVPower = 'on';
let muteTV = false;
let commandMuteTV = 'unmuted';
const powerCharacteristic = powerService.getCharacteristic(Characteristic.On);
const muteCharacteristic = muteService.getCharacteristic(Characteristic.On);
powerCharacteristic.on(
CharacteristicEventTypes.SET,
async (value, callback) => {
console.log('Setting TV power state to: ' + value);
currentTVPower = value;
if (value == true) {
commandTVPower = 'on';
} else {
commandTVPower = 'off';
}
await executeCommand(deviceId, 'switch', 'main', commandTVPower, []);
callback();
}
);
muteCharacteristic.on(CharacteristicEventTypes.SET, async (value, callback) => {
console.log('Setting TV mute state to: ' + value);
muteTV = value;
if (value == true) {
commandMuteTV = 'mute';
} else {
commandMuteTV = 'unmute';
}
await executeCommand(deviceId, 'audioMute', 'main', commandMuteTV, []);
callback();
});
powerService.name = 'Power';
accessory.addService(powerService);
muteService.name = 'Mute';
accessory.addService(muteService);
let username = '';
if (process.env.MAC_ADDRESS === undefined) {
username = generateMacAddress();
} else {
username = process.env.MAC_ADDRESS;
}
accessory.publish({
username,
pincode: '141-91-495',
port: 458,
category: hap.Categories.TELEVISION,
});
console.log('Accessory ready!');
console.log(`Setup code is 141-91-495`);