homebridge-tasmota-control 1.6.1-beta.0 → 1.6.1-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/sensors.js +190 -311
package/package.json
CHANGED
package/src/sensors.js
CHANGED
|
@@ -94,14 +94,14 @@ class Sensors extends EventEmitter {
|
|
|
94
94
|
return obj;
|
|
95
95
|
}, {});
|
|
96
96
|
|
|
97
|
+
let i = 0;
|
|
97
98
|
for (const [key, value] of Object.entries(sensorData)) {
|
|
98
99
|
const sensorData = value;
|
|
99
100
|
|
|
100
|
-
//sensor
|
|
101
101
|
const obj = {
|
|
102
102
|
name: key,
|
|
103
103
|
time: statusSns.Time,
|
|
104
|
-
tempUnit: statusSns.TempUnit,
|
|
104
|
+
tempUnit: statusSns.TempUnit === 'C' ? '°C' : statusSns.TempUnit,
|
|
105
105
|
pressureUnit: statusSns.PressureUnit,
|
|
106
106
|
temperature: sensorData.Temperature,
|
|
107
107
|
referenceTemperature: sensorData.ReferenceTemperature,
|
|
@@ -115,12 +115,8 @@ class Sensors extends EventEmitter {
|
|
|
115
115
|
ambientLight: sensorData.Ambient,
|
|
116
116
|
motion: sensorData.Motion
|
|
117
117
|
};
|
|
118
|
-
if (obj.tempUnit === 'C') {
|
|
119
|
-
obj.tempUnit = '°C';
|
|
120
|
-
}
|
|
121
118
|
|
|
122
|
-
|
|
123
|
-
const obj1 = {
|
|
119
|
+
const energy = {
|
|
124
120
|
power: sensorData.Power,
|
|
125
121
|
apparentPower: sensorData.ApparentPower,
|
|
126
122
|
reactivePower: sensorData.ReactivePower,
|
|
@@ -135,73 +131,66 @@ class Sensors extends EventEmitter {
|
|
|
135
131
|
frequency: sensorData.Frequency,
|
|
136
132
|
load: sensorData.Load,
|
|
137
133
|
};
|
|
138
|
-
|
|
134
|
+
|
|
135
|
+
const sensor = key === 'ENERGY' ? { ...obj, ...energy } : obj;
|
|
139
136
|
const debug1 = this.enableDebugMode ? this.emit('debug', `Sensor: ${JSON.stringify(sensor, null, 2)}`) : false;
|
|
140
137
|
|
|
141
|
-
//push to array
|
|
142
138
|
this.sensors.push(sensor);
|
|
143
|
-
}
|
|
144
|
-
this.sensorsCount = this.sensors.length;
|
|
145
|
-
|
|
146
|
-
//update characteristics
|
|
147
|
-
if (this.sensorsCount > 0) {
|
|
148
|
-
for (let i = 0; i < this.sensorsCount; i++) {
|
|
149
|
-
const sensor = this.sensors[i];
|
|
150
139
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
//temperature
|
|
159
|
-
update(this.sensorTemperatureServices, 'CurrentTemperature', sensor.temperature);
|
|
160
|
-
update(this.sensorReferenceTemperatureServices, 'CurrentTemperature', sensor.referenceTemperature);
|
|
161
|
-
update(this.sensorObjTemperatureServices, 'CurrentTemperature', sensor.objTemperature);
|
|
162
|
-
update(this.sensorAmbTemperatureServices, 'CurrentTemperature', sensor.ambTemperature);
|
|
163
|
-
update(this.sensorDewPointTemperatureServices, 'CurrentTemperature', sensor.dewPointTemperature);
|
|
164
|
-
|
|
165
|
-
//humidity
|
|
166
|
-
update(this.sensorHumidityServices, 'CurrentRelativeHumidity', sensor.humidity);
|
|
167
|
-
|
|
168
|
-
//co₂
|
|
169
|
-
const co2Service = this.sensorCarbonDioxydeServices?.[i];
|
|
170
|
-
update(this.sensorCarbonDioxydeServices, 'CarbonDioxideDetected', sensor.carbonDioxyde > 1000);
|
|
171
|
-
update(this.sensorCarbonDioxydeServices, 'CarbonDioxideLevel', sensor.carbonDioxyde);
|
|
172
|
-
update(this.sensorCarbonDioxydeServices, 'CarbonDioxidePeakLevel', sensor.carbonDioxyde);
|
|
173
|
-
|
|
174
|
-
//ambient light
|
|
175
|
-
update(this.sensorAmbientLightServices, 'CurrentAmbientLightLevel', sensor.ambientLight);
|
|
176
|
-
|
|
177
|
-
//motion
|
|
178
|
-
update(this.sensorMotionServices, 'MotionDetected', sensor.motion);
|
|
179
|
-
|
|
180
|
-
//energy
|
|
181
|
-
const energyFields = {
|
|
182
|
-
Power: 'power',
|
|
183
|
-
ApparentPower: 'apparentpower',
|
|
184
|
-
ReactivePower: 'reactivepower',
|
|
185
|
-
EnergyToday: 'energytoday',
|
|
186
|
-
EnergyLastDay: 'energylastday',
|
|
187
|
-
EnergyLifetime: 'energylifetime',
|
|
188
|
-
Current: 'current',
|
|
189
|
-
Voltage: 'voltage',
|
|
190
|
-
Factor: 'factor',
|
|
191
|
-
Frequency: 'frequency',
|
|
192
|
-
ReadingTime: 'readingtime'
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
const energyService = this.sensorEnergyServices?.[i]?.Characteristic;
|
|
196
|
-
for (const [charName, sensorKey] of Object.entries(energyFields)) {
|
|
197
|
-
const value = sensor[sensorKey];
|
|
198
|
-
if (energyService?.[charName] && Characteristic?.[charName] !== undefined) {
|
|
199
|
-
energyService[charName].updateCharacteristic(Characteristic[charName], value);
|
|
200
|
-
}
|
|
140
|
+
//update characteristics
|
|
141
|
+
const update = (servicesArray, expectedDisplayName, charName, value) => {
|
|
142
|
+
const service = servicesArray?.[i];
|
|
143
|
+
const characteristic = service?.Characteristic?.[charName];
|
|
144
|
+
if (service?.displayName === expectedDisplayName && characteristic && Characteristic?.[charName]) {
|
|
145
|
+
characteristic.updateCharacteristic(Characteristic[charName], value);
|
|
201
146
|
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const updates = [
|
|
150
|
+
// Temperature
|
|
151
|
+
{ service: 'sensorTemperatureService', char: 'CurrentTemperature', key: 'temperature' },
|
|
152
|
+
{ service: 'sensorReferenceTemperatureService', char: 'CurrentTemperature', key: 'referenceTemperature' },
|
|
153
|
+
{ service: 'sensorObjTemperatureService', char: 'CurrentTemperature', key: 'objTemperature' },
|
|
154
|
+
{ service: 'sensorAmbTemperatureService', char: 'CurrentTemperature', key: 'ambTemperature' },
|
|
155
|
+
{ service: 'sensorDewPointTemperatureService', char: 'CurrentTemperature', key: 'dewPointTemperature' },
|
|
156
|
+
|
|
157
|
+
// Humidity
|
|
158
|
+
{ service: 'sensorHumidityService', char: 'CurrentRelativeHumidity', key: 'humidity' },
|
|
159
|
+
|
|
160
|
+
// CO₂
|
|
161
|
+
{ service: 'sensorCarbonDioxydeService', char: 'CarbonDioxideDetected', key: 'carbonDioxyde', transform: v => v > 1000 },
|
|
162
|
+
{ service: 'sensorCarbonDioxydeService', char: 'CarbonDioxideLevel', key: 'carbonDioxyde' },
|
|
163
|
+
{ service: 'sensorCarbonDioxydeService', char: 'CarbonDioxidePeakLevel', key: 'carbonDioxyde' },
|
|
164
|
+
|
|
165
|
+
// Ambient Light
|
|
166
|
+
{ service: 'sensorAmbientLightService', char: 'CurrentAmbientLightLevel', key: 'ambientLight' },
|
|
167
|
+
|
|
168
|
+
// Motion
|
|
169
|
+
{ service: 'sensorMotionService', char: 'MotionDetected', key: 'motion' },
|
|
170
|
+
|
|
171
|
+
// Energy
|
|
172
|
+
{ service: 'sensorEnergyService', char: 'Power', key: 'power' },
|
|
173
|
+
{ service: 'sensorEnergyService', char: 'ApparentPower', key: 'apparentPower' },
|
|
174
|
+
{ service: 'sensorEnergyService', char: 'ReactivePower', key: 'reactivePower' },
|
|
175
|
+
{ service: 'sensorEnergyService', char: 'EnergyToday', key: 'energyToday' },
|
|
176
|
+
{ service: 'sensorEnergyService', char: 'EnergyLastDay', key: 'energyLastDay' },
|
|
177
|
+
{ service: 'sensorEnergyService', char: 'EnergyLifetime', key: 'energyLifetime' },
|
|
178
|
+
{ service: 'sensorEnergyService', char: 'Current', key: 'current' },
|
|
179
|
+
{ service: 'sensorEnergyService', char: 'Voltage', key: 'voltage' },
|
|
180
|
+
{ service: 'sensorEnergyService', char: 'Factor', key: 'factor' },
|
|
181
|
+
{ service: 'sensorEnergyService', char: 'Frequency', key: 'frequency' },
|
|
182
|
+
{ service: 'sensorEnergyService', char: 'ReadingTime', key: 'time' },
|
|
183
|
+
];
|
|
184
|
+
|
|
185
|
+
for (const { service, char, key, transform } of updates) {
|
|
186
|
+
const value = transform ? transform(sensor[key]) : sensor[key];
|
|
187
|
+
update(this.sensorServices, `${service}${i}`, char, value);
|
|
202
188
|
}
|
|
189
|
+
|
|
190
|
+
i++;
|
|
203
191
|
}
|
|
204
192
|
}
|
|
193
|
+
this.sensorsCount = this.sensors.length;
|
|
205
194
|
|
|
206
195
|
return true;
|
|
207
196
|
} catch (error) {
|
|
@@ -251,6 +240,14 @@ class Sensors extends EventEmitter {
|
|
|
251
240
|
return;
|
|
252
241
|
}
|
|
253
242
|
|
|
243
|
+
createSensorService({ accessory, serviceType, serviceName, characteristicType, getValue }) {
|
|
244
|
+
const service = accessory.addService(serviceType, serviceName, serviceName);
|
|
245
|
+
service.addOptionalCharacteristic(Characteristic.ConfiguredName);
|
|
246
|
+
service.setCharacteristic(Characteristic.ConfiguredName, serviceName);
|
|
247
|
+
service.getCharacteristic(characteristicType).onGet(getValue);
|
|
248
|
+
return service;
|
|
249
|
+
}
|
|
250
|
+
|
|
254
251
|
//prepare accessory
|
|
255
252
|
async prepareAccessory() {
|
|
256
253
|
const debug = this.enableDebugMode ? this.emit('debug', `Prepare Accessory`) : false;
|
|
@@ -274,276 +271,158 @@ class Sensors extends EventEmitter {
|
|
|
274
271
|
//Prepare services
|
|
275
272
|
if (this.sensorsCount > 0) {
|
|
276
273
|
const debug = this.enableDebugMode ? this.emit('debug', `Prepare Sensor Services`) : false;
|
|
277
|
-
this.
|
|
278
|
-
|
|
279
|
-
this.sensorObjTemperatureServices = [];
|
|
280
|
-
this.sensorAmbTemperatureServices = [];
|
|
281
|
-
this.sensorDewPointTemperatureServices = [];
|
|
282
|
-
this.sensorHumidityServices = [];
|
|
283
|
-
this.sensorCarbonDioxydeServices = [];
|
|
284
|
-
this.sensorAmbientLightServices = [];
|
|
285
|
-
this.sensorMotionServices = [];
|
|
286
|
-
this.sensorEnergyServices = [];
|
|
287
|
-
|
|
288
|
-
//temperature
|
|
274
|
+
this.sensorServices = [];
|
|
275
|
+
|
|
289
276
|
let i = 0;
|
|
290
277
|
for (const sensor of this.sensors) {
|
|
291
278
|
const sensorName = sensor.name;
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
});
|
|
320
|
-
this.sensorReferenceTemperatureServices.push(sensorReferenceTemperatureService);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
//object temperature
|
|
324
|
-
if (sensor.objTemperature) {
|
|
325
|
-
const debug = this.enableDebugMode ? this.emit('debug', `Prepare Obj Temperature Sensor Services`) : false;
|
|
326
|
-
const serviceName = this.sensorsNamePrefix ? `${accessoryName} ${sensorName} Obj Temperature` : `${sensorName} Obj Temperature`;
|
|
327
|
-
const sensorObjTemperatureService = accessory.addService(Service.TemperatureSensor, serviceName, `Obj Temperature Sensor ${i}`);
|
|
328
|
-
sensorObjTemperatureService.addOptionalCharacteristic(Characteristic.ConfiguredName);
|
|
329
|
-
sensorObjTemperatureService.setCharacteristic(Characteristic.ConfiguredName, serviceName);
|
|
330
|
-
sensorObjTemperatureService.getCharacteristic(Characteristic.CurrentTemperature)
|
|
331
|
-
.onGet(async () => {
|
|
332
|
-
const value = sensor.objTemperature;
|
|
333
|
-
const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} obj temperature: ${value} °${sensor.tempUnit}`);
|
|
334
|
-
return value;
|
|
335
|
-
});
|
|
336
|
-
this.sensorObjTemperatureServices.push(sensorObjTemperatureService);
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
//ambient temperature
|
|
340
|
-
if (sensor.ambTemperature) {
|
|
341
|
-
const debug = this.enableDebugMode ? this.emit('debug', `Prepare Amb Temperature Sensor Services`) : false;
|
|
342
|
-
const serviceName = this.sensorsNamePrefix ? `${accessoryName} ${sensorName} Amb Temperature` : `${sensorName} Amb Temperature`;
|
|
343
|
-
const sensorAmbTemperatureService = accessory.addService(Service.TemperatureSensor, serviceName, `Amb Temperature Sensor ${i}`);
|
|
344
|
-
sensorAmbTemperatureService.addOptionalCharacteristic(Characteristic.ConfiguredName);
|
|
345
|
-
sensorAmbTemperatureService.setCharacteristic(Characteristic.ConfiguredName, serviceName);
|
|
346
|
-
sensorAmbTemperatureService.getCharacteristic(Characteristic.CurrentTemperature)
|
|
347
|
-
.onGet(async () => {
|
|
348
|
-
const value = sensor.ambTemperature;
|
|
349
|
-
const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} amb temperature: ${value} °${sensor.tempUnit}`);
|
|
350
|
-
return value;
|
|
351
|
-
});
|
|
352
|
-
this.sensorAmbTemperatureServices.push(sensorAmbTemperatureService);
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
//dew point temperature
|
|
356
|
-
if (sensor.dewPointTemperature) {
|
|
357
|
-
const debug = this.enableDebugMode ? this.emit('debug', `Prepare Dew Point Temperature Sensor Services`) : false;
|
|
358
|
-
const serviceName = this.sensorsNamePrefix ? `${accessoryName} ${sensorName} Dew Point` : `${sensorName} Dew Point`;
|
|
359
|
-
const sensorDewPointTemperatureService = accessory.addService(Service.TemperatureSensor, serviceName, `Dew Point Temperature Sensor ${i}`);
|
|
360
|
-
sensorDewPointTemperatureService.addOptionalCharacteristic(Characteristic.ConfiguredName);
|
|
361
|
-
sensorDewPointTemperatureService.setCharacteristic(Characteristic.ConfiguredName, serviceName);
|
|
362
|
-
sensorDewPointTemperatureService.getCharacteristic(Characteristic.CurrentTemperature)
|
|
363
|
-
.onGet(async () => {
|
|
364
|
-
const value = sensor.dewPointTemperature;
|
|
365
|
-
const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} dew point: ${value} °${sensor.tempUnit}`);
|
|
366
|
-
return value;
|
|
279
|
+
const service = {};
|
|
280
|
+
|
|
281
|
+
const fullName = (suffix) =>
|
|
282
|
+
this.sensorsNamePrefix ? `${accessoryName} ${sensorName} ${suffix}` : `${sensorName} ${suffix}`;
|
|
283
|
+
|
|
284
|
+
// Temperature Variants
|
|
285
|
+
const tempTypes = [
|
|
286
|
+
{ key: 'temperature', label: 'Temperature' },
|
|
287
|
+
{ key: 'referenceTemperature', label: 'Reference Temperature' },
|
|
288
|
+
{ key: 'objTemperature', label: 'Obj Temperature' },
|
|
289
|
+
{ key: 'ambTemperature', label: 'Amb Temperature' },
|
|
290
|
+
{ key: 'dewPointTemperature', label: 'Dew Point' }
|
|
291
|
+
];
|
|
292
|
+
for (const { key, label } of tempTypes) {
|
|
293
|
+
if (sensor[key] !== undefined) {
|
|
294
|
+
if (this.enableDebugMode) this.emit('debug', `Prepare ${label} Sensor Services`);
|
|
295
|
+
const value = sensor[key];
|
|
296
|
+
const name = fullName(label);
|
|
297
|
+
const temperatureService = this.createSensorService({
|
|
298
|
+
accessory,
|
|
299
|
+
serviceType: Service.TemperatureSensor,
|
|
300
|
+
serviceName: name,
|
|
301
|
+
characteristicType: Characteristic.CurrentTemperature,
|
|
302
|
+
getValue: async () => {
|
|
303
|
+
if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} ${label.toLowerCase()}: ${value} °${sensor.tempUnit}`);
|
|
304
|
+
return value;
|
|
305
|
+
}
|
|
367
306
|
});
|
|
368
|
-
|
|
307
|
+
service[`sensor${label.replace(/\s/g, '')}Service`] = temperatureService;
|
|
308
|
+
}
|
|
369
309
|
}
|
|
370
310
|
|
|
371
|
-
//
|
|
372
|
-
if (sensor.humidity) {
|
|
373
|
-
|
|
374
|
-
const
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
.
|
|
311
|
+
// Humidity
|
|
312
|
+
if (sensor.humidity !== undefined) {
|
|
313
|
+
if (this.enableDebugMode) this.emit('debug', `Prepare Humidity Sensor Services`);
|
|
314
|
+
const name = fullName('Humidity');
|
|
315
|
+
const humidityService = this.createSensorService({
|
|
316
|
+
accessory,
|
|
317
|
+
serviceType: Service.HumiditySensor,
|
|
318
|
+
serviceName: name,
|
|
319
|
+
characteristicType: Characteristic.CurrentRelativeHumidity,
|
|
320
|
+
getValue: async () => {
|
|
380
321
|
const value = sensor.humidity;
|
|
381
|
-
|
|
322
|
+
if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} humidity: ${value} %`);
|
|
382
323
|
return value;
|
|
383
|
-
}
|
|
384
|
-
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
service.sensorHumidityService = humidityService;
|
|
385
327
|
}
|
|
386
328
|
|
|
387
|
-
//
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
sensorCarbonDioxydeService
|
|
411
|
-
.onGet(async () => {
|
|
412
|
-
const value = sensor.carbonDioxyde;
|
|
413
|
-
const logInfo = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} carbon dioxyde peak level: ${value} ppm`);
|
|
414
|
-
return value;
|
|
415
|
-
});
|
|
416
|
-
this.sensorCarbonDioxydeServices.push(sensorCarbonDioxydeService);
|
|
329
|
+
// Carbon Dioxide
|
|
330
|
+
if (sensor.carbonDioxyde !== undefined) {
|
|
331
|
+
if (this.enableDebugMode) this.emit('debug', `Prepare Carbon Dioxyde Sensor Services`);
|
|
332
|
+
const name = fullName('Carbon Dioxyde');
|
|
333
|
+
const co2Service = accessory.addService(Service.CarbonDioxideSensor, name, `sensorCarbonDioxydeService${i}`);
|
|
334
|
+
co2Service.addOptionalCharacteristic(Characteristic.ConfiguredName);
|
|
335
|
+
co2Service.setCharacteristic(Characteristic.ConfiguredName, name);
|
|
336
|
+
|
|
337
|
+
co2Service.getCharacteristic(Characteristic.CarbonDioxideDetected).onGet(async () => {
|
|
338
|
+
const detected = sensor.carbonDioxyde > 1000;
|
|
339
|
+
if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} carbon dioxyde detected: ${detected ? 'Yes' : 'No'}`);
|
|
340
|
+
return detected;
|
|
341
|
+
});
|
|
342
|
+
co2Service.getCharacteristic(Characteristic.CarbonDioxideLevel).onGet(async () => {
|
|
343
|
+
const value = sensor.carbonDioxyde;
|
|
344
|
+
if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} carbon dioxyde level: ${value} ppm`);
|
|
345
|
+
return value;
|
|
346
|
+
});
|
|
347
|
+
co2Service.getCharacteristic(Characteristic.CarbonDioxidePeakLevel).onGet(async () => {
|
|
348
|
+
const value = sensor.carbonDioxyde;
|
|
349
|
+
if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} carbon dioxyde peak level: ${value} ppm`);
|
|
350
|
+
return value;
|
|
351
|
+
});
|
|
352
|
+
service.sensorCarbonDioxydeService = co2Service;
|
|
417
353
|
}
|
|
418
354
|
|
|
419
|
-
//
|
|
420
|
-
if (sensor.ambientLight) {
|
|
421
|
-
|
|
422
|
-
const
|
|
423
|
-
const
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
.
|
|
355
|
+
// Ambient Light
|
|
356
|
+
if (sensor.ambientLight !== undefined) {
|
|
357
|
+
if (this.enableDebugMode) this.emit('debug', `Prepare Ambient Light Sensor Services`);
|
|
358
|
+
const name = fullName('Ambient Light');
|
|
359
|
+
const lightService = this.createSensorService({
|
|
360
|
+
accessory,
|
|
361
|
+
serviceType: Service.LightSensor,
|
|
362
|
+
serviceName: name,
|
|
363
|
+
characteristicType: Characteristic.CurrentAmbientLightLevel,
|
|
364
|
+
getValue: async () => {
|
|
428
365
|
const value = sensor.ambientLight;
|
|
429
|
-
|
|
366
|
+
if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} ambient light: ${value} lx`);
|
|
430
367
|
return value;
|
|
431
|
-
}
|
|
432
|
-
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
service.sensorAmbientLightService = lightService;
|
|
433
371
|
}
|
|
434
372
|
|
|
435
|
-
//
|
|
436
|
-
if (sensor.motion) {
|
|
437
|
-
|
|
438
|
-
const
|
|
439
|
-
const
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
.
|
|
373
|
+
// Motion
|
|
374
|
+
if (sensor.motion !== undefined) {
|
|
375
|
+
if (this.enableDebugMode) this.emit('debug', `Prepare Motion Sensor Services`);
|
|
376
|
+
const name = fullName('Motion');
|
|
377
|
+
const motionService = this.createSensorService({
|
|
378
|
+
accessory,
|
|
379
|
+
serviceType: Service.MotionSensor,
|
|
380
|
+
serviceName: name,
|
|
381
|
+
characteristicType: Characteristic.MotionDetected,
|
|
382
|
+
getValue: async () => {
|
|
444
383
|
const state = sensor.motion;
|
|
445
|
-
|
|
384
|
+
if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} motion: ${state ? 'ON' : 'OFF'}`);
|
|
446
385
|
return state;
|
|
447
|
-
}
|
|
448
|
-
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
service.sensorMotionService = motionService;
|
|
449
389
|
}
|
|
450
390
|
|
|
451
|
-
//
|
|
391
|
+
// Energy Sensor
|
|
452
392
|
if (sensor.name === 'ENERGY') {
|
|
453
|
-
|
|
454
|
-
const
|
|
455
|
-
const energyService = accessory.addService(Service.PowerAndEnergyService,
|
|
456
|
-
energyService.setCharacteristic(Characteristic.ConfiguredName,
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
const info = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} reactive power: ${value} VAr`);
|
|
478
|
-
return value;
|
|
479
|
-
});
|
|
480
|
-
}
|
|
481
|
-
if (sensor.energyToday) {
|
|
482
|
-
energyService.getCharacteristic(Characteristic.EnergyToday)
|
|
483
|
-
.onGet(async () => {
|
|
484
|
-
const value = sensor.energyToday;
|
|
485
|
-
const info = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} energy today: ${value} kWh`);
|
|
486
|
-
return value;
|
|
487
|
-
});
|
|
488
|
-
}
|
|
489
|
-
if (sensor.energyLastDay) {
|
|
490
|
-
energyService.getCharacteristic(Characteristic.EnergyLastDay)
|
|
491
|
-
.onGet(async () => {
|
|
492
|
-
const value = sensor.energyLastDay;
|
|
493
|
-
const info = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} energy last day: ${value} kWh`);
|
|
494
|
-
return value;
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
if (sensor.energyLifetime) {
|
|
498
|
-
energyService.getCharacteristic(Characteristic.EnergyLifetime)
|
|
499
|
-
.onGet(async () => {
|
|
500
|
-
const value = sensor.energyLifetime;
|
|
501
|
-
const info = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} energy lifetime: ${value} kWh`);
|
|
502
|
-
return value;
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
if (sensor.current) {
|
|
506
|
-
energyService.getCharacteristic(Characteristic.Current)
|
|
507
|
-
.onGet(async () => {
|
|
508
|
-
const value = sensor.current;
|
|
509
|
-
const info = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} current: ${value} A`);
|
|
510
|
-
return value;
|
|
511
|
-
});
|
|
512
|
-
}
|
|
513
|
-
if (sensor.voltage) {
|
|
514
|
-
energyService.getCharacteristic(Characteristic.Voltage)
|
|
515
|
-
.onGet(async () => {
|
|
516
|
-
const value = sensor.voltage;
|
|
517
|
-
const info = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} voltage: ${value} V`);
|
|
518
|
-
return value;
|
|
519
|
-
});
|
|
520
|
-
}
|
|
521
|
-
if (sensor.factor) {
|
|
522
|
-
energyService.getCharacteristic(Characteristic.Factor)
|
|
523
|
-
.onGet(async () => {
|
|
524
|
-
const value = sensor.factor;
|
|
525
|
-
const info = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} power factor: ${value} cos φ`);
|
|
526
|
-
return value;
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
if (sensor.frequency) {
|
|
530
|
-
energyService.getCharacteristic(Characteristic.Freqency)
|
|
531
|
-
.onGet(async () => {
|
|
532
|
-
const value = sensor.frequency;
|
|
533
|
-
const info = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} frequency: ${value} Hz`);
|
|
534
|
-
return value;
|
|
535
|
-
});
|
|
536
|
-
}
|
|
537
|
-
if (sensor.time) {
|
|
538
|
-
energyService.getCharacteristic(Characteristic.ReadingTime)
|
|
539
|
-
.onGet(async () => {
|
|
540
|
-
const value = sensor.time;
|
|
541
|
-
const info = this.disableLogInfo ? false : this.emit('info', `sensor: ${sensorName} last report: ${value}`);
|
|
393
|
+
if (this.enableDebugMode) this.emit('debug', `Prepare Power And Energy Service`);
|
|
394
|
+
const name = this.sensorsNamePrefix ? `${accessoryName} ${sensorName}` : sensorName;
|
|
395
|
+
const energyService = accessory.addService(Service.PowerAndEnergyService, name, `sensorEnergyService${i}`);
|
|
396
|
+
energyService.setCharacteristic(Characteristic.ConfiguredName, name);
|
|
397
|
+
|
|
398
|
+
const energyFields = [
|
|
399
|
+
{ key: 'power', type: Characteristic.Power, label: 'power', unit: 'W' },
|
|
400
|
+
{ key: 'apparentPower', type: Characteristic.ApparentPower, label: 'apparent power', unit: 'VA' },
|
|
401
|
+
{ key: 'reactivePower', type: Characteristic.ReactivePower, label: 'reactive power', unit: 'VAr' },
|
|
402
|
+
{ key: 'energyToday', type: Characteristic.EnergyToday, label: 'energy today', unit: 'kWh' },
|
|
403
|
+
{ key: 'energyLastDay', type: Characteristic.EnergyLastDay, label: 'energy last day', unit: 'kWh' },
|
|
404
|
+
{ key: 'energyLifetime', type: Characteristic.EnergyLifetime, label: 'energy lifetime', unit: 'kWh' },
|
|
405
|
+
{ key: 'current', type: Characteristic.Current, label: 'current', unit: 'A' },
|
|
406
|
+
{ key: 'voltage', type: Characteristic.Voltage, label: 'voltage', unit: 'V' },
|
|
407
|
+
{ key: 'factor', type: Characteristic.Factor, label: 'power factor', unit: 'cos φ' },
|
|
408
|
+
{ key: 'frequency', type: Characteristic.Freqency, label: 'frequency', unit: 'Hz' },
|
|
409
|
+
{ key: 'time', type: Characteristic.ReadingTime, label: 'last report', unit: '' }
|
|
410
|
+
];
|
|
411
|
+
|
|
412
|
+
for (const { key, type, label, unit } of energyFields) {
|
|
413
|
+
if (sensor[key] !== undefined) {
|
|
414
|
+
energyService.getCharacteristic(type).onGet(async () => {
|
|
415
|
+
const value = sensor[key];
|
|
416
|
+
if (!this.disableLogInfo) this.emit('info', `sensor: ${sensorName} ${label}: ${value} ${unit}`);
|
|
542
417
|
return value;
|
|
543
418
|
});
|
|
419
|
+
}
|
|
544
420
|
}
|
|
545
|
-
|
|
421
|
+
|
|
422
|
+
service.sensorEnergyService = energyService;
|
|
546
423
|
}
|
|
424
|
+
|
|
425
|
+
this.sensorServices.push(service);
|
|
547
426
|
i++;
|
|
548
427
|
}
|
|
549
428
|
}
|