/
yandex2mqtt.template.js
320 lines (307 loc) · 9.45 KB
/
yandex2mqtt.template.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Yandex2mqtt template classes
Used by codegen updated on each Codegen call!
*/
class GenDevice {
data = {}
constructor(options) {
this.data = {}
this.data.id = options.id
this.data.type = options.type
this.data.name = options.name || 'Без названия'
this.data.description = options.description || ''
this.data.room = options.room || ''
this.data.mqtt = []
this.data.capabilities = []
this.data.properties = []
this.data.valueMapping = []
}
toConfig() {
return this.data
}
addMQTT(instance, set, state) {
//mqtt = {}
//mqtt.instance =
this.data.mqtt.push({
instance: instance,
...(set ? {set: 'eventbus/set/' + set} : {} ),
...(state ? { state: 'eventbus/state/' + state } : {} ),
})
}
addCapability(cap) {
this.data.capabilities.push(cap)
}
addProperty(prop) {
this.data.properties.push(prop)
}
addValueMapping(prop) {
this.data.valueMapping.push(prop)
}
}
const LIGHT = {
SW: 'sw', // Simple, on/off
DIM: 'dim', // on/off, dimmer
CT: 'ct', // on/off, dimmer, Color temperature
RGB: 'rgb', // on/off, dimmer, RGB
}
function LightGroup(options) {
if (!options.type) { options.type = 'devices.types.light' }
let dev = new GenDevice(options)
// Group lights can ON/OFF
dev.addMQTT('on', options.id + '/sw', options.id)
dev.addCapability({
type: 'devices.capabilities.on_off',
retrievable: true,
reportable: true,
state: {
instance: 'on',
value: false,
},
})
return dev.toConfig()
}
function Light(type, options) {
if (!options.type) { options.type = 'devices.types.light' }
if (typeof options.sw === 'undefined') { options.sw = '_sw' }
let dev = new GenDevice(options)
// Special suffux for buggy devices (use not real item, but proxy)
let suffix = options.proxy ? '_proxy' : ''
// All lights can ON/OFF
dev.addMQTT('on', options.id + options.sw + suffix + '/sw', options.id + options.sw + suffix)
dev.addCapability({
type: 'devices.capabilities.on_off',
retrievable: true,
reportable: true,
state: {
instance: 'on',
value: false,
},
})
// Device can DIM?
if ([LIGHT.DIM, LIGHT.CT, LIGHT.RGB].includes(type)) {
dev.addMQTT('brightness', options.id + '_dim/dim', options.id + '_dim')
dev.addCapability({
type: 'devices.capabilities.range',
retrievable: true,
reportable: true,
parameters: {
instance: 'brightness',
unit: 'unit.percent',
range: {
min: 1,
max: 100,
precision: 10
}
},
state: {
instance: 'brightness',
value: 0,
},
})
}
// Device can CT?
if ([LIGHT.CT].includes(type)) {
dev.addMQTT('temperature_k', options.id + '_ct/ct_k', options.id + '_ct/ct_k')
dev.addCapability({
type: 'devices.capabilities.color_setting',
retrievable: true,
reportable: true,
parameters: {
instance: 'temperature_k',
temperature_k: {
min: 2500,
max: 4000,
}
},
state: {
instance: 'temperature_k',
value: 0,
},
})
}
// Device can RGB?
if ([LIGHT.RGB].includes(type)) {
dev.addMQTT('rgb', options.id + '_color/rgb_int', null)
dev.addCapability({
type: 'devices.capabilities.color_setting',
retrievable: true,
reportable: true,
parameters: {
color_model: 'rgb',
},
state: {
instance: 'rgb',
value: 0,
},
})
}
return dev.toConfig()
}
function Thermostat(options) {
if (!options.type) { options.type = 'devices.types.thermostat' }
// Device settings
options.temp_off = options.temp_off || 5
options.temp_on = options.temp_on || 23
options.temp_on_min = options.temp_on_min || 15
let dev = new GenDevice(options)
// On/Off control
dev.addMQTT('on', options.id + '_thermostat/temperature', options.id + '_thermostat')
dev.addCapability({
type: 'devices.capabilities.on_off',
retrievable: true,
reportable: true,
})
// Temperature control
dev.addMQTT('temperature', options.id + '_thermostat/temperature', options.id + '_thermostat')
dev.addCapability({
type: 'devices.capabilities.range',
retrievable: true,
reportable: true,
parameters: {
instance: 'temperature',
unit: 'unit.temperature.celsius',
range: {
min: 5,
max: 30,
precision: 1
}
},
})
// Конвертация, для эмуляции ВКЛ/ВЫКЛ отопления
dev.addValueMapping({
type: 'on_off',
mapping: function (device, instance, value, y2m) {
// Кастомная функция конвертации
if (y2m) { // От Яндекс в MQTT
return value ? options.temp_on : options.temp_off
} else { // От MQTT в Яндекс
// Если температура больше 17°C -> репортим ВКЛ, иначе ВЫКЛ
return value > options.temp_on_min
}
}
})
return dev.toConfig()
}
function Sensor(options) {
if (!options.type) { options.type = 'devices.types.sensor.climate' }
let dev = new GenDevice(options)
// Temperature
dev.addMQTT('temperature', null, options.id + '_temperature')
dev.addMQTT('humidity', null, options.id + '_humidity')
dev.addProperty({
type: 'devices.properties.float',
retrievable: true,
reportable: true,
parameters: {
instance: 'temperature',
unit: 'unit.temperature.celsius',
},
state: {
instance: 'temperature',
value: 0,
},
})
dev.addProperty({
type: 'devices.properties.float',
retrievable: true,
reportable: true,
parameters: {
instance: 'humidity',
unit: 'unit.percent',
},
state: {
instance: 'humidity',
value: 0,
},
})
dev.addValueMapping({
type: 'float',
mapping: function (device, instance, value, y2m) {
if ('humidity' == instance) {
// Convert humidity from 0-1.0 to 0-100%
return Math.round(value * 100.0)
}
if ('co2_level' == instance) {
// Convert CO2 from 1/1M to ppm
return Math.round(value * 1000000.0)
}
return value
}
})
if (options.co2) {
dev.addMQTT('co2_level', null, options.id + '_co2')
dev.addProperty({
type: 'devices.properties.float',
retrievable: true,
reportable: true,
parameters: {
instance: 'co2_level',
unit: 'unit.ppm',
},
state: {
instance: 'co2_level',
value: 0,
},
})
}
return dev.toConfig()
}
// Жалюзи твёрдые
function Shutter(options) {
if (!options.type) { options.type = 'devices.types.openable.curtain' }
let dev = new GenDevice(options)
// Открой-закрой (конвертация функцией)
dev.addMQTT('on', options.id + '_pos/pos', options.id + '_pos')
dev.addCapability({
type: 'devices.capabilities.on_off',
retrievable: true,
reportable: true,
})
// Вычисление и усправление ON/OFF -> открой / закрой
dev.addValueMapping({
type: 'on_off',
mapping: function (device, instance, value, y2m) {
// Кастомная функция конвертации
// У меня % означают "насколько закрыто"
if (y2m) { // От Яндекс в MQTT
return value ? 0 : 100
} else { // От MQTT в Яндекс
// Закрыто менее чем на 90% -> открыто
return value < 90
}
}
})
// Положение
dev.addMQTT('open', options.id + '_pos/pos', options.id + '_pos')
dev.addCapability({
type: 'devices.capabilities.range',
retrievable: true,
reportable: true,
parameters: {
instance: 'open',
unit: 'unit.percent',
range: {
min: 0,
max: 100,
}
},
state: {
instance: 'open',
value: 0,
},
})
// Приведение к моему формату (0 - закрыто, 100 - открыто)
dev.addValueMapping({
type: 'range',
mapping: 'invert_percent',
})
return dev.toConfig()
}
module.exports = {
LIGHT: LIGHT,
LightGroup: LightGroup,
Light: Light,
Thermostat: Thermostat,
Sensor: Sensor,
Shutter: Shutter,
}