-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (90 loc) · 3.54 KB
/
index.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
"use strict";
let Service, Characteristic, api;
const TibberFeed = require("tibber-api").TibberFeed;
const PullTimer = require("homebridge-http-base").PullTimer;
const PackageJson = require('./package.json');
var IConfig = require("tibber-api").IConfig;
var currentPowerConsumption = 0;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
api = homebridge;
homebridge.registerAccessory(PackageJson.name, "TibberLightSensor", TibberLightSensor);
};
function TibberLightSensor(log, config) {
this.log = log;
this.name = config.name;
// Minimum Lux value.
this.minSensorValue = 0;
// Maximum Lux value.
this.maxSensorValue = 65536;
this.homebridgeService = new Service.LightSensor(this.name);
this.homebridgeService.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
.setProps({
minValue: this.minSensorValue,
maxValue: this.maxSensorValue
})
.on("get", this.getSensorValue.bind(this));
// Timer to automatically update light sensor value with latest received value from Tibber API.
if (config.pullInterval) {
this.pullTimer = new PullTimer(log, config.pullInterval, this.getSensorValue.bind(this), value => {
this.homebridgeService.setCharacteristic(Characteristic.CurrentAmbientLightLevel, value);
});
this.pullTimer.start();
}
startTibber(log, config);
}
function startTibber(log, config) {
// Tibber API Tokens
const token = config.apiToken;
const homeId = config.apiHomeId;
// Config object needed when instantiating TibberQuery.
IConfig = {
// Endpoint configuration.
apiEndpoint: {
apiKey: token,
feedUrl: 'wss://api.tibber.com/v1-beta/gql/subscriptions',
},
// Query configuration.
homeId: homeId,
timestamp: true,
power: true,
};
// Creates a new instance of TibberFeed with the desired configuration and timeout.
// The timeout is used for reconnection when no data is received within the specified time.
const tibberFeed = new TibberFeed(IConfig, 30000);
// Subscribe to "data" event.
tibberFeed.on('data', data => {
currentPowerConsumption = data.power;
});
// Subscribe to "connected" event.
tibberFeed.on('connected', message => {
log.info('Successfully connected to Tibber API!')
});
// Subscribe to "disconnected" event.
tibberFeed.on('disconnected', message => {
log.info('Disconnected from Tibber API. ' + message)
startTibber(log, config);
});
// Connect to Tibber data feed.
tibberFeed.connect();
}
TibberLightSensor.prototype = {
identify: function (callback) {
this.log.info("Identify requested");
callback(null);
},
getServices: function () {
const informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, PackageJson.author.name)
.setCharacteristic(Characteristic.Model, PackageJson.name)
.setCharacteristic(Characteristic.SerialNumber, '001')
.setCharacteristic(Characteristic.FirmwareRevision, PackageJson.version);
return [informationService, this.homebridgeService];
},
getSensorValue: function (callback) {
// this.log.info('Getting current power consumption: ' + currentPowerConsumption + ' W');
callback(null, currentPowerConsumption)
},
};