-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathloxone-handler-base.js
186 lines (186 loc) · 7.24 KB
/
loxone-handler-base.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
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoxoneHandlerBase = void 0;
const sprintf_js_1 = require("sprintf-js");
class LoxoneHandlerBase {
constructor(adapter) {
this.adapter = adapter;
}
async loadSubControlsAsync(parentUuid, control) {
return await this.adapter.loadSubControlsAsync(parentUuid, control);
}
addStateChangeListener(id, listener, opts) {
this.adapter.addStateChangeListener(id, listener, opts);
}
addStateEventHandler(uuid, eventHandler, name) {
this.adapter.addStateEventHandler(uuid, eventHandler, name);
}
removeStateEventHandler(uuid, name) {
return this.adapter.removeStateEventHandler(uuid, name);
}
sendCommand(uuid, action) {
this.adapter.sendCommand(uuid, action);
}
async setStateAck(id, value) {
await this.adapter.setStateAck(id, value);
}
async setFormattedStateAck(id, value, format) {
value = (0, sprintf_js_1.sprintf)(format, value);
await this.setStateAck(id, value);
}
convertStateToInt(value) {
return this.adapter.convertStateToInt(value);
}
convertStateToFloat(value) {
if (typeof value === 'number') {
return value;
}
return !value ? 0 : parseFloat(value.toString());
}
convertStateToBoolean(value) {
if (!value) {
return false;
}
if (typeof value === 'boolean') {
return value;
}
value = value.toString();
return value !== '0' && value !== 'false';
}
getCachedStateValue(id) {
return this.adapter.getCachedStateValue(id);
}
async updateObjectAsync(id, obj) {
await this.adapter.updateObjectAsync(id, obj);
}
async updateStateObjectAsync(id, commonInfo, stateUuid, stateEventHandler) {
await this.adapter.updateStateObjectAsync(id, commonInfo, stateUuid, stateEventHandler);
}
async loadOtherControlStatesAsync(controlName, uuid, states, skipKeys) {
if (states === undefined) {
return;
}
for (const stateName in states) {
if (skipKeys.indexOf(stateName) !== -1) {
continue;
}
await this.createSimpleControlStateObjectAsync(controlName, uuid, states, stateName, 'string', 'text', {
desc: 'This state is currently unsupported by this adapter, you can only see the text representation of it.',
});
}
}
async createSimpleControlStateObjectAsync(controlName, uuid, states, name, type, role, commonExt) {
if (states !== undefined && states.hasOwnProperty(name)) {
let common = {
name: controlName + ': ' + name,
read: true,
write: false,
type: type,
role: role,
// TODO: re-add: smartIgnore: true,
};
if (commonExt && typeof commonExt === 'object') {
common = { ...common, ...commonExt };
}
await this.updateStateObjectAsync(uuid + '.' + this.normalizeName(name), common, states[name], (id, value) => {
switch (type) {
case 'number':
value = this.convertStateToFloat(value);
break;
case 'boolean':
value = this.convertStateToBoolean(value);
break;
default:
value = value === null || value === undefined ? '' : value.toString();
break;
}
return this.setStateAck(id, value);
});
}
}
async createBooleanControlStateObjectAsync(controlName, uuid, states, name, role, commonExt, converter) {
if (states !== undefined && states.hasOwnProperty(name)) {
let common = {
name: controlName + ': ' + name,
read: true,
write: false,
type: 'boolean',
role: role,
// TODO: re-add: smartIgnore: true,
};
if (commonExt && typeof commonExt === 'object') {
common = { ...common, ...commonExt };
}
const convert = converter || ((value) => value == 1);
await this.updateStateObjectAsync(uuid + '.' + this.normalizeName(name), common, states[name], async (name, value) => {
await this.setStateAck(name, convert(value));
});
}
}
async createListControlStateObjectAsync(controlName, uuid, states, name) {
if (states !== undefined && states.hasOwnProperty(name)) {
await this.updateStateObjectAsync(uuid + '.' + this.normalizeName(name), {
name: controlName + ': ' + name,
read: true,
write: false,
type: 'array',
role: 'list',
// TODO: re-add: smartIgnore: true,
}, states[name], async (name, value) => {
await this.setStateAck(name, !value ? '[]' : JSON.stringify(value.toString().split('|')));
});
}
}
async createPercentageControlStateObjectAsync(controlName, uuid, states, name, role, commonExt) {
if (states !== undefined && states.hasOwnProperty(name)) {
let common = {
name: controlName + ': ' + name,
read: true,
write: false,
type: 'number',
role: role,
unit: '%',
// TODO: re-add: smartIgnore: true,
};
if (commonExt && typeof commonExt === 'object') {
common = { ...common, ...commonExt };
}
await this.updateStateObjectAsync(uuid + '.' + this.normalizeName(name), common, states[name], async (name, value) => {
await this.setStateAck(name, Math.round(this.convertStateToFloat(value) * 100));
});
}
}
async createButtonCommandStateObjectAsync(controlName, uuid, name, commonExt) {
let common = {
name: controlName + ': ' + name,
read: false,
write: true,
type: 'boolean',
role: 'button',
// TODO: re-add: smartIgnore: true,
};
if (commonExt && typeof commonExt === 'object') {
common = { ...common, ...commonExt };
}
await this.updateStateObjectAsync(uuid + '.' + this.normalizeName(name), common, uuid);
}
async createNumberInputStateObjectAsync(controlName, uuid, name, role, commonExt) {
let common = {
name: controlName + ': ' + name,
read: false,
write: true,
type: 'number',
role: role,
// TODO: re-add: smartIgnore: true,
};
if (commonExt && typeof commonExt === 'object') {
common = { ...common, ...commonExt };
}
await this.updateStateObjectAsync(uuid + '.' + this.normalizeName(name), common, uuid);
}
normalizeName(name) {
return name.trim().replace(/[^\wäöüÄÖÜäöüéàèêçß]+/g, '_');
}
}
exports.LoxoneHandlerBase = LoxoneHandlerBase;
//# sourceMappingURL=loxone-handler-base.js.map