forked from balmli/homey-smartpresence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
153 lines (123 loc) · 5.39 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
const Homey = require('homey');
module.exports = class SmartPresenceApp extends Homey.App {
async onInit() {
try {
Homey.on('unload', () => this._onUninstall());
await this.initFlows();
this.log('SmartPresenceApp is running...');
} catch (err) {
this.log('onInit error', err);
}
}
async initFlows() {
this.firstGuestArrivedTrigger = new Homey.FlowCardTrigger('first_guest_arrived');
await this.firstGuestArrivedTrigger.register();
this.firstHouseholdMemberArrivedTrigger = new Homey.FlowCardTrigger('first_household_member_arrived');
await this.firstHouseholdMemberArrivedTrigger.register();
this.firstPersonEnteredTrigger = new Homey.FlowCardTrigger('first_person_entered');
await this.firstPersonEnteredTrigger.register();
this.guestArrivedTrigger = new Homey.FlowCardTrigger('guest_arrived');
await this.guestArrivedTrigger.register();
this.guestLeftTrigger = new Homey.FlowCardTrigger('guest_left');
await this.guestLeftTrigger.register();
this.householdMemberArrivedTrigger = new Homey.FlowCardTrigger('household_member_arrived');
await this.householdMemberArrivedTrigger.register();
this.householdMemberLeftTrigger = new Homey.FlowCardTrigger('household_member_left');
await this.householdMemberLeftTrigger.register();
this.lastGuestLeftTrigger = new Homey.FlowCardTrigger('last_guest_left');
await this.lastGuestLeftTrigger.register();
this.lastHouseholdMemberLeftTrigger = new Homey.FlowCardTrigger('last_household_member_left');
await this.lastHouseholdMemberLeftTrigger.register();
this.lastPersonLeftTrigger = new Homey.FlowCardTrigger('last_person_left');
await this.lastPersonLeftTrigger.register();
this.someoneEnteredTrigger = new Homey.FlowCardTrigger('someone_entered');
await this.someoneEnteredTrigger.register();
this.someoneLeftTrigger = new Homey.FlowCardTrigger('someone_left');
await this.someoneLeftTrigger.register();
this.userEnteredTrigger = new Homey.FlowCardTriggerDevice('user_entered');
await this.userEnteredTrigger.register();
this.userLeftTrigger = new Homey.FlowCardTriggerDevice('user_left');
await this.userLeftTrigger.register();
new Homey.FlowCardCondition('a_household_member_is_home')
.register()
.registerRunListener((args, state) => this.householdMemberIsHome(args, state));
new Homey.FlowCardCondition('having_guests')
.register()
.registerRunListener((args, state) => this.havingGuests(args, state));
new Homey.FlowCardCondition('someone_at_home')
.register()
.registerRunListener((args, state) => this.someoneAtHome(args, state));
new Homey.FlowCardCondition('user_at_home')
.register()
.registerRunListener((args, state) => args.device.userAtHome());
}
async householdMemberIsHome(args, state) {
return this.getPresenceStatus().filter(d => d.present && !d.guest).length > 0;
}
async havingGuests(args, state) {
return this.getPresenceStatus().filter(d => d.present && d.guest).length > 0;
}
async someoneAtHome(args, state) {
return this.getPresenceStatus().filter(d => d.present).length > 0;
}
getPresenceStatus() {
const status = [];
const driver = Homey.ManagerDrivers.getDriver('smart_presence');
const devices = driver.getDevices();
for (let device of devices) {
status.push({
id: device.getData().id,
present: device.getPresenceStatus(),
guest: device.isGuest(),
lastSeen: device.getLastSeen()
});
}
return status;
}
deviceArrived(device) {
const currentPrecenseStatus = this.getPresenceStatus();
this.log('deviceArrived', currentPrecenseStatus);
const deviceid = device.getData().id;
const presentAndNotSameDevice = currentPrecenseStatus.filter(d => d.id !== deviceid && d.present);
if (presentAndNotSameDevice.length === 0) {
Homey.app.firstPersonEnteredTrigger.trigger(device.getFlowCardTokens(), {});
}
if (device.isHouseHoldMember() && presentAndNotSameDevice.filter(d => !d.guest).length === 0) {
Homey.app.firstHouseholdMemberArrivedTrigger.trigger(device.getFlowCardTokens(), {});
}
if (device.isGuest() && presentAndNotSameDevice.filter(d => d.guest).length === 0) {
Homey.app.firstGuestArrivedTrigger.trigger(device.getFlowCardTokens(), {});
}
}
deviceLeft(device) {
const currentPrecenseStatus = this.getPresenceStatus();
this.log('deviceLeft', currentPrecenseStatus);
if (currentPrecenseStatus.filter(d => d.present).length === 0) {
Homey.app.lastPersonLeftTrigger.trigger(device.getFlowCardTokens(), {});
}
if (device.isHouseHoldMember() && currentPrecenseStatus.filter(d => d.present && !d.guest).length === 0) {
Homey.app.lastHouseholdMemberLeftTrigger.trigger(device.getFlowCardTokens(), {});
}
if (device.isGuest() && currentPrecenseStatus.filter(d => d.present && d.guest).length === 0) {
Homey.app.lastGuestLeftTrigger.trigger(device.getFlowCardTokens(), {});
}
}
_onUninstall() {
this._deleted = true;
try {
this._clearTimers();
} catch (err) {
this.log('_onUninstall error', err);
}
}
_clearTimers() {
const driver = Homey.ManagerDrivers.getDriver('smart_presence');
const devices = driver.getDevices();
for (let device of devices) {
if (device.clearScanTimer) {
device.clearScanTimer();
}
}
}
};