forked from balmli/homey-smartpresence
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
194 lines (152 loc) · 7.11 KB
/
Copy pathapp.js
File metadata and controls
194 lines (152 loc) · 7.11 KB
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
"use strict";
const Homey = require("homey");
function formatLastSeen(timestamp, homey) {
const timezone = homey.clock.getTimezone();
const language = homey.i18n?.getLanguage?.();
const country = homey.i18n?.getCountry?.();
const locale = [language, country].filter(Boolean).join("-") || "en-US";
return new Intl.DateTimeFormat(locale, {
timeZone: timezone,
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
}).format(new Date(timestamp));
}
module.exports = class SmartPresenceApp extends Homey.App {
log(...args) {
const timestamp = new Date().toISOString();
console.log(`${timestamp} [SmartPresence log : `, ...args);
}
async onInit() {
try {
await this.initFlows();
this.log("SmartPresenceApp is running...");
} catch (err) {
this.log("onInit error", err);
}
}
async initFlows() {
this.firstGuestArrivedTrigger = this.homey.flow.getTriggerCard("first_guest_arrived");
this.firstHouseholdMemberArrivedTrigger = this.homey.flow.getTriggerCard("first_household_member_arrived");
this.firstKidArrivedTrigger = this.homey.flow.getTriggerCard("first_kid_arrived");
this.firstPersonEnteredTrigger = this.homey.flow.getTriggerCard("first_person_entered");
this.guestArrivedTrigger = this.homey.flow.getTriggerCard("guest_arrived");
this.guestLeftTrigger = this.homey.flow.getTriggerCard("guest_left");
this.householdMemberArrivedTrigger = this.homey.flow.getTriggerCard("household_member_arrived");
this.householdMemberLeftTrigger = this.homey.flow.getTriggerCard("household_member_left");
this.kidArrivedTrigger = this.homey.flow.getTriggerCard("kid_arrived");
this.kidLeftTrigger = this.homey.flow.getTriggerCard("kid_left");
this.lastGuestLeftTrigger = this.homey.flow.getTriggerCard("last_guest_left");
this.lastHouseholdMemberLeftTrigger = this.homey.flow.getTriggerCard("last_household_member_left");
this.lastKidLeftTrigger = this.homey.flow.getTriggerCard("last_kid_left");
this.lastPersonLeftTrigger = this.homey.flow.getTriggerCard("last_person_left");
this.someoneEnteredTrigger = this.homey.flow.getTriggerCard("someone_entered");
this.someoneLeftTrigger = this.homey.flow.getTriggerCard("someone_left");
this.userEnteredTrigger = this.homey.flow.getDeviceTriggerCard("user_entered");
this.userLeftTrigger = this.homey.flow.getDeviceTriggerCard("user_left");
this.homey.flow.getConditionCard("a_household_member_is_home").registerRunListener((args, state) => this.householdMemberIsHome(args, state));
this.homey.flow.getConditionCard("kids_at_home").registerRunListener((args, state) => this.kidsAtHome(args, state));
this.homey.flow.getConditionCard("having_guests").registerRunListener((args, state) => this.havingGuests(args, state));
this.homey.flow.getConditionCard("someone_at_home").registerRunListener((args, state) => this.someoneAtHome(args, state));
this.homey.flow.getConditionCard("user_at_home").registerRunListener((args, state) => args.device.userAtHome());
}
async householdMemberIsHome(args, state) {
return this.getPresenceStatus().filter((d) => d.present && !d.guest).length > 0;
}
async kidsAtHome(args, state) {
return this.getPresenceStatus().filter((d) => d.present && d.kid).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 = this.homey.drivers.getDriver("smart_presence");
const devices = driver.getDevices();
for (let device of devices) {
status.push({
id: device.getData().id,
present: device.getPresenceStatus(),
kid: device.isKid(),
guest: device.isGuest(),
lastSeen: device.getLastSeen(),
});
}
return status;
}
async deviceArrived(device) {
const currentPresenceStatus = this.getPresenceStatus();
const tokens = device.getFlowCardTokens();
const deviceId = device.getData().id;
const lastSeenFormatted = formatLastSeen(device.getLastSeen(), this.homey);
this.log(`Device ${device.getName()} Arrived. Previous last seen: ${lastSeenFormatted}`);
let isFirstPerson = true;
let isFirstHouseholdMember = device.isHouseHoldMember();
let isFirstKid = device.isKid();
let isFirstGuest = device.isGuest();
for (const status of currentPresenceStatus) {
if (status.id !== deviceId && status.present) {
isFirstPerson = false;
if (!status.guest) isFirstHouseholdMember = false;
if (status.kid) isFirstKid = false;
if (status.guest) isFirstGuest = false;
}
}
if (isFirstPerson) {
await this.homey.app.firstPersonEnteredTrigger.trigger(tokens, {}).catch(this.error);
this.log(`>>> Device ${device.getName()} Arrived as First Person. <<<`);
}
if (isFirstHouseholdMember) {
await this.homey.app.firstHouseholdMemberArrivedTrigger.trigger(tokens, {}).catch(this.error);
this.log(`>>> Device ${device.getName()} Arrived as First HouseHold Member. <<<`);
}
if (isFirstKid) {
await this.homey.app.firstKidArrivedTrigger.trigger(tokens, {}).catch(this.error);
this.log(`>>> Device ${device.getName()} Arrived as First Kid. <<<`);
}
if (isFirstGuest) {
await this.homey.app.firstGuestArrivedTrigger.trigger(tokens, {}).catch(this.error);
this.log(`>>> Device ${device.getName()} Arrived as First Guest. <<<`);
}
}
async deviceLeft(device, tokens) {
const currentPresenceStatus = this.getPresenceStatus();
const lastSeenFormatted = formatLastSeen(device.getLastSeen(), this.homey);
this.log(`Device ${device.getName()} Left. Last Seen: ${lastSeenFormatted}`);
let isLastPerson = true;
let isLastHouseholdMember = device.isHouseHoldMember();
let isLastKid = device.isKid();
let isLastGuest = device.isGuest();
for (const status of currentPresenceStatus) {
if (status.id !== device.getData().id && status.present) {
isLastPerson = false;
if (!status.guest) isLastHouseholdMember = false;
if (status.kid) isLastKid = false;
if (status.guest) isLastGuest = false;
}
}
if (isLastPerson) {
await this.homey.app.lastPersonLeftTrigger.trigger(tokens, {}).catch(this.error);
this.log(`>>> Device ${device.getName()} Left as Last Person <<<`);
}
if (isLastHouseholdMember) {
await this.homey.app.lastHouseholdMemberLeftTrigger.trigger(tokens, {}).catch(this.error);
this.log(`>>> Device ${device.getName()} Left as Last House Hold Member. <<<`);
}
if (isLastKid) {
await this.homey.app.lastKidLeftTrigger.trigger(tokens, {}).catch(this.error);
this.log(`>>> Device ${device.getName()} Left as Last Kid. <<<`);
}
if (isLastGuest) {
await this.homey.app.lastGuestLeftTrigger.trigger(tokens, {}).catch(this.error);
this.log(`>>> Device ${device.getName()} Left as Last Guest. <<<`);
}
}
};