-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathir-action-learncode.js
More file actions
196 lines (175 loc) · 4.97 KB
/
Copy pathir-action-learncode.js
File metadata and controls
196 lines (175 loc) · 4.97 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
const {Utils, Action} = require('gateway-addon');
const IrConstants = require('./constants');
const util = require('util');
const STATE_TEXT_FORMAT = 'Learning %s';
class IRActionLearncode extends Action {
constructor(id, device, input) {
super(id, device, IrConstants.IR_ACTRION_LEARN, input);
this.targets = this.getLearnTargets(input.target);
}
getLearnTargets(target) {
const LearnTargets = {
[IrConstants.IR_DEVICE_TYPE_THERMOSTAT] : IrConstants.LEARN_TARGET_THERMOSTAT,
[IrConstants.IR_DEVICE_TYPE_DIMMABLE_LIGHT]: IrConstants.LEARN_TARGET_DIMMABLE_LIGHT,
[IrConstants.IR_DEVICE_TYPE_ON_OFF_LIGHT] : IrConstants.LEARN_TARGET_ON_OFF_LIGHT,
[IrConstants.IR_DEVICE_TYPE_ON_OF_SWITCH] : IrConstants.LEARN_TARGET_ON_OF_SWITCH,
};
if (target === IrConstants.LEARN_TARGET_ALL) {
return LearnTargets[this.device.irtype];
} else {
return [target];
}
}
/**
* Start performing the action.
* @returns a promise which resolves when the action has been finish
* or rejects when the action has been failed.
*/
async performAction() {
try {
for (let i = 0; i < this.targets.length; i++) {
const target = this.targets[i];
switch (target) {
case IrConstants.LEARN_TARGET_ON_OFF:
await this.learnOnOff();
break;
case IrConstants.LEARN_TARGET_BRIGHTNESS_UP_DOWN:
await this.learnBrightnessUpDown();
break;
case IrConstants.LEARN_TARGET_HEAT:
await this.learnHeat();
break;
case IrConstants.LEARN_TARGET_COOL:
await this.learnCool();
break;
case IrConstants.LEARN_TARGET_OFF:
await this.learnOff();
break;
default:
// do nothing
break;
}
}
} catch (err) {
throw new Error(err);
}
}
learnCode(target) {
const promise = this.device.adapter.learnCode();
this.status = util.format(STATE_TEXT_FORMAT, target);
this.device.actionNotify(this);
return promise;
}
async learnOnOff() {
const targetOn = 'on';
const targetOff = 'off';
const ir = {};
ir.on = await this.learnCode(targetOn)
.catch((err) => {
throw new Error(err);
});
ir.off = await this.learnCode(targetOff)
.catch((err) => {
throw new Error(err);
});
const property = this.device.findProperty('on');
property.setIrCode(ir);
property.onLevel = this.input.onLevel;
}
async learnBrightnessUpDown() {
const targetUp = 'brightness up';
const targetDown = 'brightness down';
const ir = {};
ir.levelUp = await this.learnCode(targetUp)
.catch((err) => {
throw new Error(err);
});
ir.levelDown = await this.learnCode(targetDown)
.catch((err) => {
throw new Error(err);
});
const property = this.device.findProperty('level');
property.setIrCode(ir);
property.levelStep = this.input.levelStep;
}
async learnHeatCool(mode, min, max) {
const ir = {
[mode]: [],
};
for (let i = min; i <= max; i++) {
const target = `${mode} ${i}℃`;
const code = await this.learnCode(target)
.catch((err) => {
throw new Error(err);
});
ir[mode].push(code);
}
const property = this.device.findProperty('mode');
property.setIrCode(ir);
const range = {min, max};
property[mode] = range;
}
async learnHeat() {
const max = this.input.MaxHeatTemperature;
const min = this.input.MinHeatTemperature;
await this.learnHeatCool('heat', min, max)
.catch((err) => {
throw new Error(err);
});
}
async learnCool() {
const max = this.input.MaxCoolTemperature;
const min = this.input.MinCoolTemperature;
await this.learnHeatCool('cool', min, max)
.catch((err) => {
throw new Error(err);
});
}
async learnOff() {
const target = 'off';
const ir = {};
ir.off = await this.learnCode(target)
.catch((err) => {
throw new Error(err);
});
const property = this.device.findProperty('mode');
property.setIrCode(ir);
}
isCompleted() {
if (this.status === 'completed') {
return;
}
if (this.status.startsWith('failed:')) {
return;
}
}
/**
* Called when fail performing the action.
*/
fail(reason) {
this.status = `failed: ${reason}`;
this.timeCompleted = Utils.timestamp();
this.device.actionNotify(this);
}
/**
* Cancel performing the action.
* @returns a promise which resolves when the action has been canceled.
*/
cancel() {
return new Promise((resolve, _reject) => {
this.status = 'canceled';
this.device.actionNotify(this);
resolve();
});
}
/**
* Called when finish performing the action.
*/
finish() {
this.status = 'completed';
this.timeCompleted = Utils.timestamp();
this.device.actionNotify(this);
}
}
module.exports = IRActionLearncode;