forked from heisler3030/homebridge-spaberry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (92 loc) · 4.09 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
"use strict";
let Service, Characteristic
const fetch = require('node-fetch')
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-spaberry',
'spaberry', spacontrol);
};
function spacontrol(log, config, api) {
this.log = log;
this.config = config;
this.homebridge = api;
this.url = this.config.url;
this.debug = this.config.debug;
}
function _toCelsius(f) {
return (f - 32) * (5/9)
}
function _toFahrenheit(c) {
return Math.round((c * 9/5) + 32)
}
spacontrol.prototype = {
getServices: function() {
if (this.debug) this.log("spacontrol.getServices")
//if (!this.heater) return [];
this.infoService = new Service.AccessoryInformation()
this.infoService
.setCharacteristic(Characteristic.Manufacturer, 'SuperFly, Inc.')
.setCharacteristic(Characteristic. Model, 'Balboa 9800CP')
this.heater = new Service.Thermostat(this.config.name)
this.heater.setCharacteristic(Characteristic.TemperatureDisplayUnits, Characteristic.TemperatureDisplayUnits.FAHRENHEIT);
this.heater
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('set', this.setTargetHeatingCoolingState.bind(this))
.setProps({
maxValue: Characteristic.TargetHeatingCoolingState.HEAT
})
this.heater
.getCharacteristic(Characteristic.TargetTemperature)
.on('set', this.setTargetTemperature.bind(this))
.setProps({
minValue: _toCelsius(80),
maxValue: _toCelsius(104)
})
this._getSpaStatus(function () {})
setInterval(function () {
this._getSpaStatus(function () {})
}.bind(this), this.config.pollInterval * 1000)
return [this.infoService, this.heater];
},
_getSpaStatus: function(callback) {
if (this.debug) this.log("Getting Status from Spa...")
fetch(`${this.config.url}/json`)
.then(res => res.json())
.then(status => {
if (this.debug) this.log(status)
//status = json
//let status = {"display":"101F","setHeat":1,"mode":1,"heating":1,"blower":0,"pump":1,"jets":0,"light":0,"temperature":94,"setTemp":101}
// Update the relevant characteristics
this.heater.getCharacteristic(Characteristic.CurrentTemperature).updateValue(_toCelsius(status.temperature))
this.heater.getCharacteristic(Characteristic.TargetTemperature).updateValue(_toCelsius(status.setTemp))
this.heater.getCharacteristic(Characteristic.CurrentHeatingCoolingState).updateValue(status.mode) // 1 = HEAT, 0 = OFF
this.heater.getCharacteristic(Characteristic.TargetHeatingCoolingState).updateValue(status.mode)
callback()
})
.catch(e => this.log.error(`Error in _getSpaStatus: ${e}`))
},
setTargetHeatingCoolingState: function (value, callback) {
this.log.info(`Spa heat mode switched to ${value}`)
this.heater.getCharacteristic(Characteristic.TargetHeatingCoolingState).updateValue(value)
fetch(`${this.config.url}/mode`)
.then(res => {
this.heater.getCharacteristic(Characteristic.CurrentHeatingCoolingState).updateValue(value)
callback(null)
this._getSpaStatus(function () {})
})
.catch(e => this.log.error(`Error in setTargetHeatingCoolingState: ${e}`))
},
setTargetTemperature: function (value, callback) {
if (this.debug) this.log(`setTargetTemperature: ${value}`)
let newTemp = _toFahrenheit(value)
this.log.info(`Adjusting target temp to ${newTemp}`)
fetch(`${this.config.url}/change?temp=${newTemp}`)
.then(res => {
this.heater.getCharacteristic(Characteristic.TargetTemperature).updateValue(value)
callback(null)
this._getSpaStatus(function () {})
})
.catch(e => this.log.error(`Error in setTargetTemperature: ${e}`))
}
}