-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
124 lines (98 loc) · 4.51 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
116
117
118
119
120
121
122
123
124
'use strict';
const TibberFeed = require("tibber-api").TibberFeed;
const TibberQuery = require("tibber-api").TibberQuery;
var inherits = require('util').inherits;
var Service, Characteristic;
var FakeGatoHistoryService;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
FakeGatoHistoryService = require('fakegato-history')(homebridge);
homebridge.registerAccessory('homebridge-tibber', 'tibber-power-consumption', TibberPowerConsumptionAccessory);
};
function TibberPowerConsumptionAccessory(log, config) {
this.log = log;
this.name = config['name'];
this.options = {
active: true,
apiEndpoint: {
apiKey: config['apiKey'],
queryUrl: config['queryUrl'],
},
// Query configuration.
homeId: config['homeId'],
timestamp: true,
power: true,
accumulatedConsumption: true
};
this.powerConsumption = 0;
this.totalPowerConsumption = 0;
var EvePowerConsumption = function() {
Characteristic.call(this, 'Consumption', 'E863F10D-079E-48FF-8F27-9C2605A29F52');
this.setProps({
format: Characteristic.Formats.UINT16,
unit: 'watts',
maxValue: 1000000000,
minValue: 0,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(EvePowerConsumption, Characteristic);
var EveTotalPowerConsumption = function() {
Characteristic.call(this, 'Total Consumption', 'E863F10C-079E-48FF-8F27-9C2605A29F52');
this.setProps({
format: Characteristic.Formats.FLOAT, // Deviation from Eve Energy observed type
unit: 'kilowatthours',
maxValue: 1000000000,
minValue: 0,
minStep: 0.001,
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(EveTotalPowerConsumption, Characteristic);
this.loggingService = new FakeGatoHistoryService("energy", this, { storage: 'fs' });
var PowerMeterService = function(displayName, subtype) {
Service.call(this, displayName, '00000001-0000-1777-8000-775D67EC4377', subtype);
this.addCharacteristic(EvePowerConsumption);
this.addOptionalCharacteristic(EveTotalPowerConsumption);
};
inherits(PowerMeterService, Service);
this.service = new PowerMeterService(this.name);
this.service.getCharacteristic(EvePowerConsumption).on('get', this.getPowerConsumption.bind(this));
this.service.addCharacteristic(EveTotalPowerConsumption).on('get', this.getTotalPowerConsumption.bind(this));
var self = this;
const tibberQuery = new TibberQuery(self.options);
const tibberFeed = new TibberFeed(tibberQuery);
// Subscribe to "data" event.
tibberFeed.on('data', data => {
self.powerConsumption = parseFloat(data.power.toString());
self.service.getCharacteristic(EvePowerConsumption).setValue(self.powerConsumption, undefined, undefined);
self.loggingService.addEntry({time: Math.round(new Date().valueOf() / 1000), power: self.powerConsumption});
self.totalPowerConsumption = parseFloat(data.accumulatedConsumption.toString());
self.service.getCharacteristic(EveTotalPowerConsumption).setValue(self.totalPowerConsumption, undefined, undefined);
});
tibberFeed.on('connected', data => {
self.log("Connected, " + data);
});
tibberFeed.on('disconnected', data => {
self.log("Disconnected, " + data);
});
// Connect to Tibber data feed
tibberFeed.connect();
}
TibberPowerConsumptionAccessory.prototype.getPowerConsumption = function (callback) {
callback(null, this.powerConsumption);
};
TibberPowerConsumptionAccessory.prototype.getTotalPowerConsumption = function (callback) {
callback(null, this.totalPowerConsumption);
};
TibberPowerConsumptionAccessory.prototype.getServices = function () {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'Neskvern')
.setCharacteristic(Characteristic.Model, 'HomeBridge Tibber')
.setCharacteristic(Characteristic.SerialNumber, '000')
return [this.service, informationService, this.loggingService];
};