forked from tabrindle/homebridge-leviton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
160 lines (147 loc) · 4.68 KB
/
api.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const fetch = require('node-fetch')
const SockJS = require('sockjs-client')
const baseURL = 'https://my.leviton.com/api'
const toQueryString = (params) =>
Object.keys(params)
.map((key) => `${key}=${params[key]}`)
.join('&')
// returns a list of switches, given a residenceID
function getResidenceIotSwitches({ residenceID, token }) {
return fetch(`${baseURL}/Residences/${residenceID}/iotSwitches`, {
method: 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-Access-Token': token,
},
}).then((res) => res.json())
}
// gets state of a given switchID
function getIotSwitch({ switchID, token }) {
return fetch(`${baseURL}/IotSwitches/${switchID}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-Access-Token': token,
},
}).then((res) => res.json())
}
// updates the state of a given switch, especially power and brightness
// power is an integer 1-100, power is a string, 'ON' or 'OFF'
function putIotSwitch({ switchID, power, brightness, token }) {
const body = {}
if (brightness) body.brightness = brightness
if (power) body.power = power
return fetch(`${baseURL}/IotSwitches/${switchID}`, {
method: 'PUT',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-Access-Token': token,
},
}).then((res) => res.json())
}
// uses a personID/userId to get accountID
function getPersonResidentialPermissions({ personID, token }) {
return fetch(`${baseURL}/Person/${personID}/residentialPermissions`, {
method: 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-Access-Token': token,
},
}).then((res) => res.json())
}
// use accountID to get residenceIDs
function getResidentialAccounts({ accountID, token }) {
return fetch(`${baseURL}/ResidentialAccounts/${accountID}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-Access-Token': token,
},
}).then((res) => res.json())
}
function getResidentialAccountsV2({ residenceObjectID, token }) {
return fetch(`${baseURL}/ResidentialAccounts/${residenceObjectID}/residences`, {
method: 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-Access-Token': token,
},
}).then((res) => res.json())
}
// obtain a user token to use in X-Access-Token header on all requests
function postPersonLogin({ email, password }) {
const query = toQueryString({
include: 'user',
})
return fetch(`${baseURL}/Person/login?${query}`, {
method: 'POST',
body: JSON.stringify({
email,
loggedInVia: 'myLeviton',
password,
rememberMe: true,
}),
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
}).then((res) => res.json())
}
function subscribe(login, devices, callback, scope) {
const ws = new SockJS('https://my.leviton.com/socket', {
origin: 'https://my.leviton.com',
headers: {
'Sec-WebSocket-Key': 'J4AAFNBWV3zbd71kD72LMQ==',
'Sec-WebSocket-Extensions': 'permessage-deflate',
'Sec-WebSocket-Version': 13,
Accept: '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
Origin: 'https://my.leviton.com',
Connection: 'keep-alive, Upgrade',
Pragma: 'no-cache',
'Cache-Control': 'no-cache',
Upgrade: 'websocket',
},
})
ws.onclose = function onclose(ev) {
scope.log.error(`Socket connection closed: ${JSON.stringify(ev)}`)
}
ws.onopen = function onopen(ev) {
scope.log.debug(`Socket connection opened: ${JSON.stringify(ev)}`)
}
ws.onmessage = function onmessage(message) {
try {
var data = JSON.parse(message.data)
} catch (err) {
scope.log.error(`Received bad json: ${String(message.data)}`)
}
if (data.type === 'challenge') {
const response = [JSON.stringify({ token: login })]
ws.send(response)
}
if (data.type === 'status' && data.status === 'ready') {
devices.forEach((element) => {
ws.send([JSON.stringify({ type: 'subscribe', subscription: { modelName: 'IotSwitch', modelId: element.id } })])
})
}
if (data.type === 'notification' && data.notification.data.power) {
const payload = {
id: data.notification.modelId,
power: data.notification.data.power,
}
if (data.notification.data.brightness) payload.brightness = data.notification.data.brightness
callback(payload)
}
}
}
module.exports = {
getIotSwitch,
getPersonResidentialPermissions,
getResidenceIotSwitches,
getResidentialAccounts,
getResidentialAccountsV2,
postPersonLogin,
putIotSwitch,
subscribe,
}