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 14
/
Copy pathswitch.js
136 lines (116 loc) · 3.73 KB
/
switch.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
import 'babel-polyfill';
import {debounce} from './../utils/debounce';
import {HomeWizardBaseAccessory} from './accessory';
export class HomeWizardSwitch extends HomeWizardBaseAccessory {
model = 'Switch';
setupServices() {
let service;
// Determine serviceType, default is lightbulb for backwards compatibility
let switchType = 'lightbulb';
if (this.config.switchTypes && this.config.switchTypes[this.name]) {
switchType = this.config.switchTypes[this.name];
}
switch (switchType) {
case 'switch':
service = new this.hap.Service.Switch();
break;
case 'fan':
service = new this.hap.Service.Fan();
break;
case 'outlet':
service = new this.hap.Service.Outlet();
service.getCharacteristic(this.hap.Characteristic.OutletInUse)
.on('get', () => {
return true;
});
break;
case 'lightbulb':
service = new this.hap.Service.Lightbulb();
if (this.hwObject.type === 'dimmer') {
service
.getCharacteristic(this.hap.Characteristic.Brightness)
.on('set', this.setBrightness.bind(this))
.on('get', this.getBrightness.bind(this));
}
break;
default:
this.log(`Unknown switchType: ${switchType} for: ${this.name}`);
break;
}
this.onChar = service.getCharacteristic(this.hap.Characteristic.On);
this.onChar
.on('set', this.setPowerState.bind(this))
.on('get', this.getPowerState.bind(this));
this.services.push(service);
}
setPowerState(state, callback) {
// To prevent dimmers from switching to dimming mode when they are already turned on
if (this.hwObject.type === 'dimmer' && state === true) {
callback(); // Calling callback right away might take to long?
this.getPowerState(currentState => {
if (currentState !== state) {
this._setPowerState(state);
}
});
} else {
this._setPowerState(state, callback);
}
}
_setPowerState(state, callback) {
const value = state ? 'on' : 'off';
const url = `sw/${this.id}/${value}`;
this.api.request({url}).then(() => {
this.log(`Switched ${this.name} to: ${value}`);
if (callback) {
callback();
}
}).catch(error => {
this.log(`Failed to switch ${this.name}`);
this.log(error);
if (callback) {
callback(error);
}
});
}
getPowerState(callback) {
this.api.getStatus(this.id, 'switches').then(sw => {
const state = sw.status === 'on' ? 1 : 0;
this.log(`Retrieved power state for: ${this.name} - ${state}`);
callback(null, state);
}).catch(error => {
this.log(`Failed to retrieve power state for: ${this.name}`);
this.log(error);
callback(error);
});
}
@debounce(500)
setBrightness(value, callback) {
this.api.request({url: `sw/dim/${this.id}/${value}`}).then(() => {
this.log(`Set brightness for: ${this.name} to: ${value}`);
callback();
}).catch(error => {
this.log(`Failed to set brightness for: ${this.name}`);
this.log(error);
callback(error);
});
}
getBrightness(callback) {
this.api.getStatus(this.id, 'switches').then(sw => {
this.log(`Retrieved brightness for: ${this.name} - ${sw.dimlevel}`);
callback(null, sw.dimlevel);
}).catch(error => {
this.log(`Failed to retrieve brightness for: ${this.name}`);
this.log(error);
callback(error);
});
}
identify(callback) {
this.log(`Identify ${this.name}...`);
const previous = this.onChar.value;
this.onChar.setValue(1 - previous);
setTimeout(function (me, value) {
me.onChar.setValue(value);
}, 1000, this, previous);
callback();
}
}