-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
71 lines (60 loc) · 2.4 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
const axios = require('axios');
const api = axios.create({})
var Service, Characteristic;
const DEF_MIN_LUX = 0.0001,
DEF_MAX_LUX = 10000;
const interval = 15 // Minutes
const PLUGIN_NAME = 'homebridge-comed-hourlybilling';
const ACCESSORY_NAME = 'ComEd Hourly Billing';
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory(PLUGIN_NAME, ACCESSORY_NAME, HourlyBilling);
}
class HourlyBilling {
constructor(log, config) {
this.log = log
this.config = config
this.service = new Service.LightSensor(this.config.name)
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "ComEd";
this.model = config["model"] || "Hourly Billing";
this.minLux = config["min_lux"] || DEF_MIN_LUX;
this.maxLux = config["max_lux"] || DEF_MAX_LUX;
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')
.catch(err => {
this.log.error('Error getting current billing rate %s',err)
})
if(hourlyData) {
this.log.info('Data from API', hourlyData.data[0].price);
if (hourlyData.data[0].price == null) {
// No price in hourlyData, return minimum allowed value
this.service.getCharacteristic(Characteristic.CurrentAmbientLightLevel).updateValue(DEF_MIN_LUX)
} else {
// Return positive value
this.service.getCharacteristic(Characteristic.CurrentAmbientLightLevel).updateValue( Math.abs(hourlyData.data[0].price, 1))
}
} else {
// No response hourlyData, return minimum allowed value
this.service.getCharacteristic(Characteristic.CurrentAmbientLightLevel).updateValue(DEF_MIN_LUX)
}
} catch (error) {
console.error(error)
}
this.timer = setTimeout(this.poll.bind(this), this.refreshInterval)
}
}