-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
130 lines (116 loc) · 4.91 KB
/
app.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
'use strict'
const Homey = require('homey')
class Remootio extends Homey.App {
/**
* onInit is called when the app is initialized.
*/
async onInit () {
this.log('Remootio has been initialized')
// add condition listeners (only applicable for drivers 'remootio-fg' and 'remootio-gf')
this.homey.flow.getConditionCard('garagedoor_closed_sub')
.registerRunListener(args => {
const { device } = args
const state = device.getGarageDoorState() === true
device.log(`${device.getName()} -- garagedoor_closed_sub -- ${state}`)
return state
})
// add action listeners (only applicable for drivers 'remootio-fg' and 'remootio-gf')
this.homey.flow.getActionCard('garagedoor_close_sub')
.registerRunListener(args => {
const { device } = args
const garageCapabilityId = device.getGarageDoorCapabilityId()
if (garageCapabilityId) {
device.log(`${device.getName()} -- garagedoor_close_sub called`)
device.triggerCapabilityListener(garageCapabilityId, true)
return true
} else {
device.log(`${device.getName()} -- garagedoor_close_sub -- Garagedoor capability not found`)
return false
}
})
this.homey.flow.getActionCard('garagedoor_open_sub')
.registerRunListener(args => {
const { device } = args
const garageCapabilityId = device.getGarageDoorCapabilityId()
if (garageCapabilityId) {
device.log(`${device.getName()} -- garagedoor_open_sub called`)
device.triggerCapabilityListener(garageCapabilityId, false)
return true
} else {
device.log(`${device.getName()} -- garagedoor_open_sub -- Garagedoor capability not found`)
return false
}
})
this.homey.flow.getActionCard('garagedoor_toggle_sub')
.registerRunListener(args => {
const { device } = args
const garageCapabilityId = device.getGarageDoorCapabilityId()
if (garageCapabilityId) {
device.log(`${device.getName()} -- garagedoor_toggle_sub called`)
device.triggerCapabilityListener(garageCapabilityId, !device.getGarageDoorState())
return true
} else {
device.log(`${device.getName()} -- garagedoor_toggle_sub -- Garagedoor capability not found`)
return false
}
})
this.homey.flow.getActionCard('garagedoor_toggle_free_sub')
.registerRunListener(args => {
const { device } = args
const id = device.getFreeRelayCapabilityId()
if (!id) {
device.log(`${device.getName()} -- garagedoor_toggle_free_sub -- CapabilityId not found for free relay output -- ${id}`)
return false
}
device.log(`${device.getName()} -- garagedoor_toggle_free_sub called`)
device.triggerCapabilityListener(id, !device.getFreeRelayState())
return true
})
// add action listeners (only applicable for driver remootio-device-api
this.homey.flow.getActionCard('garagedoor_close_if_open')
.registerRunListener(async args => {
const { device } = args
const { success, closed } = await device.getApiDeviceStatus()
if (!success) {
device.log(`${device.getName()} -- garagedoor_close_if_open failed to fetch status`)
return false
}
if (!closed) {
const garageCapabilityId = device.getGarageDoorCapabilityId()
if (garageCapabilityId) {
device.log(`${device.getName()} -- garagedoor_close_if_open called`)
device.triggerCapabilityListener(garageCapabilityId, false)
return true
} else {
device.log(`${device.getName()} -- garagedoor_close_if_open -- Garagedoor capability not found`)
return false
}
}
device.log(`${device.getName()} -- garagedoor_close_if_open did nothing since its already closed`)
return true
})
this.homey.flow.getActionCard('garagedoor_open_if_closed')
.registerRunListener(async args => {
const { device } = args
const { success, closed } = await device.getApiDeviceStatus()
if (!success) {
device.log(`${device.getName()} -- garagedoor_open_if_closed failed to fetch status`)
return false
}
if (closed) {
const garageCapabilityId = device.getGarageDoorCapabilityId()
if (garageCapabilityId) {
device.log(`${device.getName()} -- garagedoor_open_if_closed called`)
device.triggerCapabilityListener(garageCapabilityId, false)
return true
} else {
device.log(`${device.getName()} -- garagedoor_open_if_closed -- Garagedoor capability not found`)
return false
}
}
device.log(`${device.getName()} -- garagedoor_open_if_closed did nothing since its already open`)
return true
})
}
}
module.exports = Remootio