forked from jortgies/homebridge-rademacher-blinds
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathRademacherSunSensorAccessory.js
executable file
·78 lines (72 loc) · 3.57 KB
/
RademacherSunSensorAccessory.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
var tools = require("./tools.js");
var RademacherAccessory = require("./RademacherAccessory.js");
function RademacherSunSensorAccessory(log, debug, accessory, sensor, session)
{
RademacherAccessory.call(this, log, debug, accessory, sensor, session);
this.sensor = sensor;
this.services = [];
// Light sensor
this.currentSunState=this.sensor.readings.sun_detected ? 100000 : 0.0001;
var lightSensorService = this.accessory.getService(global.Service.LightSensor);
lightSensorService.getCharacteristic(global.Characteristic.CurrentAmbientLightLevel)
.setProps({ minValue: 0.0001, maxValue: 100000 })
.setValue(this.currentSunState)
.on("get", this.getCurrentSunState.bind(this));
this.services.push(lightSensorService);
// Switch (ambient light level characteristic of light sensor cannot yet be used as trigger in HomeKit)
var switchService = this.accessory.getService(global.Service.Switch);
switchService.getCharacteristic(global.Characteristic.On)
.setValue(this.sensor.readings.sun_detected ? true : false)
this.services.push(switchService);
setInterval(this.update.bind(this), 10000);
}
RademacherSunSensorAccessory.prototype = Object.create(RademacherAccessory.prototype);
RademacherSunSensorAccessory.prototype.getCurrentSunState = function(callback)
{
if (this.debug) this.log("%s [%s] - getCurrentSunState()", this.accessory.displayName, this.sensor.did);
callback(null,this.currentSunState);
var self = this;
this.session.get("/v4/devices?devtype=Sensor", 30000, function (err,body) {
if(err)
{
self.log("%s [%s] - getCurrentSunState(): error=%s", self.accessory.displayName, self.sensor.did,err);
return;
}
body.meters.forEach(function (data) {
if (data.did == self.sensor.did) {
const sun_detected=data.readings.sun_detected
self.currentSunState = sun_detected? 100000 : 0.0001;
if (self.debug) self.log("%s [%s] - getCurrentSunState(): sun_detected=%s, state=%s", self.accessory.displayName, self.sensor.did, sun_detected,self.currentSunState);
// Update LightSensor state
var lightSensorService = self.accessory.getService(global.Service.LightSensor);
lightSensorService.getCharacteristic(global.Characteristic.CurrentAmbientLightLevel).updateValue(self.currentSunState);
// Update Switch state
var switchService = self.accessory.getService(global.Service.Switch);
switchService.getCharacteristic(global.Characteristic.On).updateValue(sun_detected);
}
});
});
};
RademacherSunSensorAccessory.prototype.update = function() {
if (this.debug) this.log(`%s [%s] - update()`, this.accessory.displayName, this.sensor.did);
var self = this;
this.getCurrentSunState(function(err, sun_detected) {
if (err)
{
self.log(`%s [%s] - update().getCurrentSunState(): error=%s`, self.accessory.displayName, self.sensor.did, err);
}
else if (sun_detected===null)
{
self.log(`%s [%s] - update().getCurrentSunState(): got null state`, self.accessory.displayName, self.sensor.did);
}
else
{
if (self.debug) self.log(`%s [%s] - update().getCurrentSunState(): state=%s`, self.accessory.displayName, self.sensor.did, sun_detected);
}
}.bind(this));
};
RademacherSunSensorAccessory.prototype.getServices = function()
{
return this.services;
};
module.exports = RademacherSunSensorAccessory;