This repository has been archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
thermometer.js
75 lines (61 loc) · 2.48 KB
/
thermometer.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
import 'babel-polyfill';
import {HomeWizardBaseAccessory} from './accessory';
export class HomeWizardThermometer extends HomeWizardBaseAccessory {
model = 'Thermometer';
setupServices() {
// Temperature service
const temperatureSensorService = new this.hap.Service.TemperatureSensor();
const characteristic = temperatureSensorService.getCharacteristic(this.hap.Characteristic.CurrentTemperature);
// Make sure negative temps are working...
characteristic.props.minValue = -50;
characteristic.on('get', this.getTemperature.bind(this));
// Humidity service
const humiditySensorService = new this.hap.Service.HumiditySensor();
humiditySensorService
.getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
.on('get', this.getHumidity.bind(this));
// Add battery status to services
temperatureSensorService
.addCharacteristic(new this.hap.Characteristic.StatusLowBattery()) //eslint-disable-line
.on('get', this.getLowBatteryStatus.bind(this));
humiditySensorService
.addCharacteristic(new this.hap.Characteristic.StatusLowBattery()) //eslint-disable-line
.on('get', this.getLowBatteryStatus.bind(this));
// Add services
this.services.push(temperatureSensorService);
this.services.push(humiditySensorService);
}
getHumidity(callback) {
this.api.getStatus(this.id, 'thermometers').then(currentValues => {
this.log(`Retrieved humidity for: ${this.name} its ${currentValues.hu} %`);
callback(null, currentValues.hu);
}).catch(error => {
this.log(`Failed to retrieve humidity for: ${this.name}`);
this.log(error);
callback(error);
});
}
getTemperature(callback) {
this.api.getStatus(this.id, 'thermometers').then(currentValues => {
this.log(`Retrieved temperature for: ${this.name} its ${currentValues.te} degrees`);
callback(null, currentValues.te);
}).catch(error => {
this.log(`Failed to retrieve temperature for: ${this.name}`);
this.log(error);
callback(error);
});
}
getLowBatteryStatus(callback) {
this.api.getSensors(this.id, 'thermometers').then(thermometer => {
const lowBattery = thermometer.lowBattery === 'yes';
if (lowBattery) {
this.log(`Retrieved low battery level for: ${this.name}`);
}
callback(null, lowBattery);
}).catch(error => {
this.log(`Failed to retrieve battery state for: ${this.name}`);
this.log(error);
callback(error);
});
}
}