-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
185 lines (159 loc) · 5.47 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
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
var Service, Characteristic;
const fetch = require('node-fetch');
const cron = require('node-cron');
const url = require('url');
var hbapi;
var nodever = process.versions.node;
var tibsw_version = "0.3.4";
let setupOK = false;
let firstrun = true;
module.exports = function (homebridge){
Service = homebridge.hap.Service;
hbapi = homebridge;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-tibberswitch", "HomebridgeTibberswitch", myTS);
};
function myTS(log, config){
this.config = config;
this.log = log;
this.priceThreshold = config.threshold || 0;
this.percentageTheshold = config.percentage || 0;
this.currency = config.currency || 'cents';
if(typeof config.token === 'string' && config.token != ''){
setupOK = true;
}else{
this.log(`Token error - please check your config`);
}
/* Static config values */
this.token = config.token
this.homeNumber = config.home || 0;
this.GQLbody = {
"query": "{ \
viewer { \
homes { \
currentSubscription { \
priceInfo { \
today { \
total \
level \
startsAt\
} \
} \
} \
} \
} \
}"
}
/* Dynamic values based on config */
this.levels = [];
this.runAverage = true;
this.tbjson = {};
if((this.priceThreshold > 0 && this.percentageTheshold == 0)){
this.runAverage = false;
if(this.priceThreshold === 1){
this.levels.push('VERY_CHEAP');
}else if(this.priceThreshold === 2){
this.levels.push('VERY_CHEAP','CHEAP');
}else if(this.priceThreshold ===3){
this.levels.push('VERY_CHEAP','CHEAP','NORMAL');
}else if(this.priceThreshold === 4){
this.levels.push('VERY_CHEAP','CHEAP','NORMAL','EXPENSIVE');
}
}
}
myTS.prototype = {
getServices: function () {
async function populateJson(me) {
try{
const responses = await fetch("https://api.tibber.com/v1-beta/gql",{
method: "POST",
headers: {
"Authorization": "Bearer " + me.token,
"Content-Type": "application/json",
"User-Agent": "Homebridge/"+hbapi.serverVersion+" homebridge-tibberswitch/" + tibsw_version + " node/"+nodever
},
body:JSON.stringify(me.GQLbody)
})
me.tbjson = await responses.json()
await updateDevices(me, me.tbjson,true);
}catch(err){
me.log(`Could not fetch tibber values - ${err}`);
}
}
function updateDevices(me, priceJson,daily){
let allPrices = priceJson['data']['viewer']['homes'][me.homeNumber]['currentSubscription']['priceInfo']['today'];
if (allPrices === undefined){
allPrices = priceJson['data']['viewer']['homes'][0]['currentSubscription']['priceInfo']['today'];
me.log(`Home with number ${me.homeNumber} not found. Using default home.`);
}
let currentHour = new Date().getHours();
let price = allPrices[currentHour].total;
let priceLevel = allPrices[currentHour].level;
let lowprice = false;
let priceOre = Math.round(price * 100);
let percentPrice = 0;
if(me.runAverage){
let minPrice = 100000;
let maxPrice = 0;
let avgPrice = 0;
for(var i = 0; i < allPrices.length; i++){
if(allPrices[i].total * 100 < minPrice){
minPrice = Math.round(allPrices[i].total * 100);
}
if(allPrices[i].total * 100 > maxPrice){
maxPrice = Math.round(allPrices[i].total * 100);
}
avgPrice += allPrices[i].total;
}
avgPrice = avgPrice / allPrices.length * 100;
percentPrice = Math.round((priceOre / avgPrice) * 100);
if(daily){
me.log(`Daily prices fetched. Daily average is: ${Math.round(avgPrice)} ${me.currency}.`);
}
if(priceOre < avgPrice){
lowprice = true;
}
if (percentPrice < me.percentageTheshold) {
lowprice = true;
}
}else{
if(me.levels.includes(priceLevel)){
lowprice = true;
}
me.log(`Current price level rating: ${priceLevel}`);
}
me.motionService.getCharacteristic(Characteristic.MotionDetected).updateValue(lowprice);
me.log(`Current electricity price is ${priceOre} ${me.currency}.`);
me.log(`Current price percentage is ${percentPrice}% of the daily average`)
if(lowprice){
me.log("Price is below your desired threshold.");
}else{
me.log("Price is over your desired threshold.");
}
}
if(setupOK){
populateJson(this);
cron.schedule('5 0 1-23 * * *', () =>{
updateDevices(this, this.tbjson,false);
});
cron.schedule('0 0 * * *', () =>{
populateJson(this);
});
}
this.services = [];
/* Information Service */
let informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Tibber")
.setCharacteristic(Characteristic.Model, "Production")
.setCharacteristic(Characteristic.SerialNumber, tibsw_version);
this.services.push(informationService);
/* Motion sensor Service */
let motionService = new Service.MotionSensor("Electricity price low");
this.services.push(motionService);
this.informationService = informationService;
this.motionService = motionService;
motionService.setPrimaryService(true);
return this.services;
}
};