forked from nfarina/homebridge-dummy
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
158 lines (135 loc) · 4.44 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
var Service, Characteristic
module.exports = function (homebridge) {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-delay-switch', 'DelaySwitch', delaySwitch)
}
function delaySwitch(log, config, api) {
let UUIDGen = api.hap.uuid
this.log = log
this.name = config['name']
this.delay = config['delay'] || 0
this.delayUnit = config['delayUnit'] || 'ms'
this.debug = config.debug || false
this.sensorType = config['sensorType']
if (typeof this.sensorType === 'undefined')
this.sensorType = 'motion'
this.flipSensor = config['flipSensorState']
this.disableSensor = config['disableSensor'] || !config['sensorType'] || this.delay === 0
this.startOnReboot = config['startOnReboot'] || false
this.switchOn = false
this.sensorTriggered = 0
this.uuid = UUIDGen.generate(this.name)
switch (this.delayUnit) {
case 's':
this.delayTime = this.delay * 1000
break
case 'm':
this.delayTime = this.delay * 60 * 1000
break
case 'h':
this.delayTime = this.delay * 60 * 60 * 1000
break
case 'd':
this.delayTime = this.delay * 24 * 60 * 60 * 1000
break
default:
this.delayTime = this.delay
break
}
// define debug method to output debug logs when enabled in the config
this.log.easyDebug = (...content) => {
if (this.debug) {
this.log(content.reduce((previous, current) => {
return previous + ' ' + current
}))
} else
this.log.debug(content.reduce((previous, current) => {
return previous + ' ' + current
}))
}
this.getSensorState = () => {
const state = this.sensorTriggered
if (this.flipSensor && this.sensorType === 'motion')
return !state
if (this.sensorType === 'motion')
return !!state
if (this.flipSensor)
return state ^ 1
return state
}
}
delaySwitch.prototype.getServices = function () {
var informationService = new Service.AccessoryInformation()
informationService
.setCharacteristic(Characteristic.Manufacturer, 'Delay Switch')
.setCharacteristic(Characteristic.Model, `Delay-${this.delay}${this.delayUnit}`)
.setCharacteristic(Characteristic.SerialNumber, this.uuid)
this.switchService = new Service.Switch(this.name)
this.switchService.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this))
.updateValue(this.startOnReboot)
var services = [informationService, this.switchService]
if (!this.disableSensor) {
switch (this.sensorType) {
case 'contact':
this.sensorService = new Service.ContactSensor(this.name + ' Trigger')
this.sensorCharacteristic = Characteristic.ContactSensorState
break
case 'occupancy':
this.sensorService = new Service.OccupancySensor(this.name + ' Trigger')
this.sensorCharacteristic = Characteristic.OccupancyDetected
break
case 'leak':
this.sensorService = new Service.LeakSensor(this.name + ' Trigger')
this.sensorCharacteristic = Characteristic.LeakDetected
break
default:
this.sensorService = new Service.MotionSensor(this.name + ' Trigger')
this.sensorCharacteristic = Characteristic.MotionDetected
break
}
this.sensorService
.getCharacteristic(this.sensorCharacteristic)
.on('get', (callback) => {
callback(null, this.getSensorState())
})
services.push(this.sensorService)
}
return services
}
delaySwitch.prototype.setOn = function (value, callback) {
if (value === false) {
this.log.easyDebug('Stopping the Timer')
this.switchOn = false
clearTimeout(this.timer)
this.sensorTriggered = 0
if (!this.disableSensor)
this.sensorService.getCharacteristic(this.sensorCharacteristic).updateValue(this.getSensorState())
} else if (value === true) {
this.switchOn = true
clearTimeout(this.timer)
if (this.delay > 0) {
this.log.easyDebug('Starting the Timer')
this.timer = setTimeout(function () {
this.log.easyDebug('Time is Up!')
this.switchService.getCharacteristic(Characteristic.On).updateValue(false)
this.switchOn = false
if (!this.disableSensor) {
this.sensorTriggered = 1
this.sensorService.getCharacteristic(this.sensorCharacteristic).updateValue(this.getSensorState())
this.log.easyDebug('Triggering Sensor')
setTimeout(function () {
this.sensorTriggered = 0
this.sensorService.getCharacteristic(this.sensorCharacteristic).updateValue(this.getSensorState())
}.bind(this), 3000)
}
}.bind(this), this.delayTime)
}
}
callback()
}
delaySwitch.prototype.getOn = function (callback) {
callback(null, this.switchOn)
}