forked from jmaferreira/homebridge-garage-door-shelly1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (163 loc) · 5.54 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var Service, Characteristic
const packageJson = require('./package.json')
const request = require('request')
const jp = require('jsonpath')
module.exports = function (homebridge) {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-door-lock-shelly1', 'ShellyDoorLock', DoorLockOpener)
}
function DoorLockOpener(log, config) {
this.log = log
this.config = config
this.name = config.name
this.unlockURL = config.unlockURL
this.lockURL = config.lockURL
this.manufacturer = config.manufacturer || packageJson.author.name
this.serial = config.serial || packageJson.version
this.model = config.model || packageJson.name
this.firmware = config.firmware || packageJson.version
this.username = config.username || null
this.password = config.password || null
this.timeout = config.timeout || 3000
this.http_method = config.http_method || 'GET'
this.polling = config.polling || false
this.pollInterval = config.pollInterval || 120
this.statusURL = config.statusURL
this.statusKey = config.statusKey || '$.inputs[0].input'
this.statusValueUnlocked = config.statusValueUnlocked || '0'
this.statusValueLocked = config.statusValueLocked || '1'
if (this.username != null && this.password != null) {
this.auth = {
user: this.username,
pass: this.password,
}
}
this.service = new Service.LockMechanism(this.name)
}
DoorLockOpener.prototype = {
identify: function (callback) {
this.log('Identify requested!')
callback()
},
_httpRequest: function (url, body, method, callback) {
request(
{
url: url,
body: body,
method: this.http_method,
timeout: this.timeout,
rejectUnauthorized: false,
auth: this.auth,
},
function (error, response, body) {
callback(error, response, body)
},
)
},
_getStatus: function (callback) {
var url = this.statusURL
if (this.config.debug) {
this.log.debug('Getting status: %s', url)
}
this._httpRequest(
url,
'',
'GET',
function (error, response, responseBody) {
if (error) {
this.log.error('Error getting status: %s', error.message)
this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(3) // unknown
callback(error)
} else {
let statusValue = 0
if (this.statusKey) {
var originalStatusValue = jp
.query(typeof responseBody === 'string' ? JSON.parse(responseBody) : responseBody, this.statusKey, 1)
.pop()
if (new RegExp(this.statusValueUnlocked).test(originalStatusValue)) {
statusValue = 0
} else if (new RegExp(this.statusValueLocked).test(originalStatusValue)) {
statusValue = 1
}
if (this.config.debug) {
this.log.debug(
'Transformed status value from %s to %s (%s)',
originalStatusValue,
statusValue,
this.statusKey,
)
}
} else {
statusValue = responseBody
}
this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(statusValue)
this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(statusValue)
if (this.config.debug) {
this.log.debug('Updated lock state to: %s', statusValue)
}
callback()
}
}.bind(this),
)
},
setLockTargetState: function (value, callback) {
var url
this.log.debug('Setting LockTargetState to %s', value)
if (value === 1) {
url = this.lockURL
} else {
url = this.unlockURL
}
this._httpRequest(
url,
'',
this.http_method,
function (error, response, responseBody) {
if (error) {
this.log.warn('Error setting LockTargetState: %s', error.message)
callback(error)
} else {
if (value === 1) {
this.log('Locking')
this.lock()
} else {
this.log('Unlocking')
this.unlock()
}
callback()
}
}.bind(this),
)
},
unlock: function () {
this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(0)
this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(0)
},
lock: function () {
this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(1)
this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(1)
},
getServices: function () {
this.informationService = new Service.AccessoryInformation()
this.informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial)
.setCharacteristic(Characteristic.FirmwareRevision, this.firmware)
this.service.getCharacteristic(Characteristic.LockTargetState).on('set', this.setLockTargetState.bind(this))
if (this.polling) {
this._getStatus(function () {})
setInterval(
function () {
this._getStatus(function () {})
}.bind(this),
this.pollInterval * 1000,
)
} else {
this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(1)
this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(1)
}
return [this.informationService, this.service]
},
}