forked from anon1y4012/homebridge-energy-price
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
72 lines (62 loc) · 2.87 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
const axios = require('axios');
const api = axios.create({})
var Service, Characteristic;
const DEF_MIN_RATE = -10000,
DEF_MAX_RATE = 10000;
const interval = 5 // Minutes
const PLUGIN_NAME = 'homebridge-amber-price';
const ACCESSORY_NAME = 'Energy Price';
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory(PLUGIN_NAME, ACCESSORY_NAME, EnergyPrice);
}
class EnergyPrice {
constructor(log, config) {
this.log = log
this.config = config
this.service = new Service.TemperatureSensor(this.config.name)
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "Energy Price";
this.model = config["model"] || "Monitor";
this.apiKey = config["apiKey"] || "API Key";
this.apiId = config["apiId"] || "API ID";
this.minRate = config["min_rate"] || DEF_MIN_RATE;
this.maxRate = config["max_rate"] || DEF_MAX_RATE;
this.refreshInterval = config["refreshInterval"] === undefined ? (interval * 60000) : (config["refreshInterval"] * 60000)
this.timer = setTimeout(this.poll.bind(this), this.refreshInterval)
this.poll()
}
getServices () {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
return [informationService, this.service]
}
async poll() {
if(this.timer) clearTimeout(this.timer)
this.timer = null
try {
// const hourlyData = await api.get('https://hourlypricing.comed.com/api?type=currenthouraverage')
// this.log.info('https://api.amber.com.au/v1/sites/'+this.apiId+'/prices/current?next=0&previous=0&resolution=5');
// this.log.info('KEY='+this.apiKey);
const amberData = await api.get('https://api.amber.com.au/v1/sites/'+this.apiId+'/prices/current?next=0&previous=0&resolution=30', {headers: {'accept': 'application/json', 'Authorization': 'Bearer ' + this.apiKey}});
this.log.info('Data from API', amberData.data[0].perKwh);
if (amberData.data[0].perKwh == null) {
// No price in hourlyData, return maximum allowed value
this.service.getCharacteristic(Characteristic.CurrentTemperature).updateValue(DEF_MAX_RATE)
} else {
// Return positive value
this.service.getCharacteristic(Characteristic.CurrentTemperature).updateValue(amberData.data[0].perKwh, 1)
}
} catch (error) {
this.log.error('Error getting current 30m estimated price %s', error)
// No response hourlyData, return maximum allowed value
this.service.getCharacteristic(Characteristic.CurrentTemperature).updateValue(DEF_MAX_RATE)
}
this.timer = setTimeout(this.poll.bind(this), this.refreshInterval)
}
convertToFahrenheit(value) {
return (value-32)*5/9;
}
}