-
Notifications
You must be signed in to change notification settings - Fork 0
/
hass.js
308 lines (236 loc) · 7.23 KB
/
hass.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// https://www.npmjs.com/package/homeassistant
let homeAssistant;
async function fireKeypadEvent(keypadData) {
const eventName = 'keypad_entry';
try {
log.lib('Firing Home Assistant event', eventName);
const eventFireReturn = await homeAssistant.events.fire(eventName, keypadData);
log.lib('Fired Home Assistant event', eventName);
return eventFireReturn;
}
catch (homeAssistantEventFireError) {
log.error('fireKeypadEvent() homeAssistantEventFireError', homeAssistantEventFireError);
return false;
}
}
async function getHassData() {
const hassData = {
config : null,
discoveryInfo : null,
status : null,
};
try {
hassData.config = await homeAssistant.config();
}
catch (configError) {
console.dir({ configError });
}
try {
hassData.discoveryInfo = await homeAssistant.discoveryInfo();
}
catch (discoveryInfoError) {
console.dir({ discoveryInfoError });
}
try {
hassData.status = await homeAssistant.status();
}
catch (statusError) {
console.dir({ statusError });
}
return hassData;
}
async function getState(entityId) {
try {
const entityIdParts = entityId.split('.');
const domain = entityIdParts[0];
const entity = entityIdParts[1];
log.lib(`Getting state for ${domain}.${entity}`);
const state = homeAssistant.states.get(domain, entity);
return state;
}
catch (homeAssistantGetStateError) {
log.error('getState() homeAssistantGetStateError', homeAssistantGetStateError);
return false;
}
}
function generateDeviceRegistryEntry(remote64, entryType) {
const deviceStatus = status.xbee.nodes64[remote64];
const entryTypeData = {
uniqueId : {
deviceName : deviceStatus.name.toLowerCase().replace(/\s+/g, '').replace('\'', ''),
nodeName : entryType.toLowerCase().replace(/\s+/g, '').replace('\'', ''),
},
name : '',
deviceClass : '',
icon : '',
unitOfMeasurement : '',
};
let entityType = 'sensor';
switch (entryType) {
case 'activePower' : {
entryTypeData.name = 'active power';
entryTypeData.deviceClass = 'power';
entryTypeData.unitOfMeasurement = 'W';
break;
}
case 'batteryLevel' : {
entryTypeData.name = 'battery level';
entryTypeData.deviceClass = 'battery';
entryTypeData.unitOfMeasurement = '%';
break;
}
case 'batteryVoltage' : {
entryTypeData.name = 'battery voltage';
entryTypeData.deviceClass = 'voltage';
entryTypeData.unitOfMeasurement = 'V';
break;
}
case 'buttonState' : {
entityType = 'binary_sensor';
entryTypeData.name = 'button';
break;
}
case 'contactState' : {
entityType = 'binary_sensor';
entryTypeData.name = 'door';
entryTypeData.deviceClass = 'door';
break;
}
case 'motionState' : {
entityType = 'binary_sensor';
entryTypeData.name = 'motion';
entryTypeData.deviceClass = 'motion';
break;
}
case 'motionValue' : {
entryTypeData.name = 'motion value';
entryTypeData.icon = 'mdi:motion-sensor';
entryTypeData.unitOfMeasurement = '%';
break;
}
case 'rssi' : {
entryTypeData.name = entryType.toUpperCase();
entryTypeData.deviceClass = 'signal_strength';
entryTypeData.unitOfMeasurement = 'dBm';
break;
}
case 'switchState' : {
entityType = 'switch';
entryTypeData.name = 'switch';
entryTypeData.icon = 'mdi:power-socket-us';
break;
}
case 'temperature' : {
entryTypeData.name = entryType;
entryTypeData.deviceClass = entryType;
entryTypeData.unitOfMeasurement = '°C';
break;
}
} // switch (entryType)
const deviceRegistryEntry = {
unique_id : `${remote64}-${entryTypeData.uniqueId.deviceName}-${entryTypeData.uniqueId.nodeName}`,
name : `${deviceStatus.name} ${entryTypeData.name}`,
availability_topic : `tele/${remote64}/LWT`,
state_topic : `stat/${remote64}/${entryType}`,
icon : entryTypeData.icon,
unit_of_measurement : entryTypeData.unitOfMeasurement,
device_class : entryTypeData.deviceClass,
state_class : 'measurement',
device : {
identifiers : remote64,
// connections : [
// [ 'remote64', status.xbee.self.addr64 ],
// ],
manufacturer : deviceStatus.make,
model : deviceStatus.model,
name : deviceStatus.name,
sw_version : deviceStatus.buildDate,
},
};
if (entryType === 'switchState') {
deviceRegistryEntry.command_topic = `cmnd/${remote64}/${entryType}`;
}
if (deviceRegistryEntry.name.includes('button button')) {
deviceRegistryEntry.name = deviceRegistryEntry.name.replace('button button', 'button');
}
if (deviceRegistryEntry.name.includes('motion sensor motion')) {
deviceRegistryEntry.name = deviceRegistryEntry.name.replace('motion sensor motion', 'motion');
}
if (deviceRegistryEntry.name.includes('door sensor door')) {
deviceRegistryEntry.name = deviceRegistryEntry.name.replace('door sensor door', 'door');
}
if (deviceRegistryEntry.device_class === '') delete deviceRegistryEntry.device_class;
if (deviceRegistryEntry.icon === '') delete deviceRegistryEntry.icon;
if (deviceRegistryEntry.unit_of_measurement === '') delete deviceRegistryEntry.unit_of_measurement;
if (entityType !== 'sensor') delete deviceRegistryEntry.state_class;
mqtt.pub(`homeassistant/${entityType}/${deviceRegistryEntry.unique_id}/config`, deviceRegistryEntry, true);
return deviceRegistryEntry;
} // generateDeviceRegistryEntry(remote64, entryType)
async function updateDeviceRegistry() {
for await (const remote64 of Object.keys(status.xbee.nodes64)) {
const device = status.xbee.nodes64[remote64];
generateDeviceRegistryEntry(remote64, 'rssi');
switch (device.model) {
case 'Button Device' :
case 'Contact Sensor Device' :
case 'PIR Device' : {
generateDeviceRegistryEntry(remote64, 'batteryLevel');
generateDeviceRegistryEntry(remote64, 'batteryVoltage');
generateDeviceRegistryEntry(remote64, 'temperature');
}
}
switch (device.model) {
case 'Button Device' : {
generateDeviceRegistryEntry(remote64, 'buttonState');
break;
}
case 'Contact Sensor Device' : {
generateDeviceRegistryEntry(remote64, 'contactState');
break;
}
case 'KeyPad Device' : {
break;
}
case 'Keyfob Device' : {
break;
}
case 'PIR Device' : {
generateDeviceRegistryEntry(remote64, 'motionValue');
generateDeviceRegistryEntry(remote64, 'motionState');
break;
}
case 'SmartPlug2.5' : {
generateDeviceRegistryEntry(remote64, 'activePower');
generateDeviceRegistryEntry(remote64, 'switchState');
break;
}
}
} // for await (const remote64 of Object.keys(status.xbee.nodes64))
} // async updateDeviceRegistry()
async function init() {
log.lib('Initializing');
try {
log.lib('Connecting to Home Assistant');
homeAssistant = new (require('homeassistant'))(config.homeassistant);
log.lib('Connected to Home Assistant');
}
catch (homeAssistantConnectError) {
log.error('init() homeAssistantConnectError', homeAssistantConnectError);
return false;
}
log.lib('Initialized');
} // async init()
async function term() {
log.lib('Terminating');
log.lib('Terminated');
} // async term()
module.exports = {
fireKeypadEvent,
getHassData,
getState,
generateDeviceRegistryEntry,
updateDeviceRegistry,
// Start/stop functions
init,
term,
};