node-red-contrib-ccu 4.12.0 → 4.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +50 -0
- package/README.md +38 -2
- package/nodes/ccu-connection.js +626 -48
- package/nodes/ccu-homeassistant.html +482 -48
- package/nodes/ccu-homeassistant.js +96 -4
- package/nodes/ccu-poll.js +4 -3
- package/nodes/ccu-program.js +1 -1
- package/nodes/ccu-sysvar.js +1 -1
- package/nodes/lib/hadiscovery.js +397 -16
- package/nodes/lib/lite.js +15 -2
- package/nodes/lib/logic.js +178 -0
- package/nodes/lib/status.js +31 -0
- package/nodes/locales/de/ccu-homeassistant.html +31 -3
- package/nodes/locales/de/ccu-poll.html +2 -0
- package/nodes/locales/de/ccu-program.html +5 -0
- package/nodes/locales/de/ccu-sysvar.html +7 -0
- package/nodes/locales/en-US/ccu-homeassistant.html +30 -3
- package/nodes/locales/en-US/ccu-poll.html +2 -0
- package/nodes/locales/en-US/ccu-program.html +5 -0
- package/nodes/locales/en-US/ccu-sysvar.html +7 -0
- package/package.json +1 -1
package/nodes/lib/hadiscovery.js
CHANGED
|
@@ -262,7 +262,9 @@ function ifaceOf(devices, address) {
|
|
|
262
262
|
/**
|
|
263
263
|
* HmIP actuators: a *_TRANSMITTER channel (state) followed by three *_VIRTUAL_RECEIVER channels
|
|
264
264
|
* (control). The first receiver becomes the control entity with the transmitter's state topic,
|
|
265
|
-
* the others are secondary (disabled by default).
|
|
265
|
+
* the others are secondary (disabled by default). `family` names the first channel of the group a
|
|
266
|
+
* channel belongs to (the transmitter, the first receiver of a block, the real dimmer) - one unit
|
|
267
|
+
* when channels become Home Assistant devices of their own (task 33).
|
|
266
268
|
*/
|
|
267
269
|
function resolveVirtualReceivers(channels) {
|
|
268
270
|
const families = [
|
|
@@ -272,6 +274,7 @@ function resolveVirtualReceivers(channels) {
|
|
|
272
274
|
];
|
|
273
275
|
for (const {transmitter, receiver} of families) {
|
|
274
276
|
let current = null;
|
|
277
|
+
let block = null;
|
|
275
278
|
let count = 0;
|
|
276
279
|
for (const ch of channels) {
|
|
277
280
|
if (ch.role === transmitter && /_TRANSMITTER$/.test(ch.TYPE)) {
|
|
@@ -283,6 +286,7 @@ function resolveVirtualReceivers(channels) {
|
|
|
283
286
|
if (ch.role === receiver && /_VIRTUAL_RECEIVER$/.test(ch.TYPE) && current) {
|
|
284
287
|
count += 1;
|
|
285
288
|
ch.stateChannel = current;
|
|
289
|
+
ch.family = current;
|
|
286
290
|
if (count === 1) {
|
|
287
291
|
current.transmitterFor = ch;
|
|
288
292
|
} else {
|
|
@@ -293,15 +297,26 @@ function resolveVirtualReceivers(channels) {
|
|
|
293
297
|
count += 1;
|
|
294
298
|
if (count > 1 && (count - 1) % 3 !== 0) {
|
|
295
299
|
ch.secondary = true;
|
|
300
|
+
ch.family = block;
|
|
301
|
+
} else {
|
|
302
|
+
block = ch;
|
|
296
303
|
}
|
|
297
304
|
}
|
|
298
305
|
}
|
|
299
306
|
}
|
|
300
307
|
|
|
301
308
|
// HM virtual dimmers (HM-LC-Dim1TPBU-FM ch 2/3) are secondaries of channel 1
|
|
309
|
+
let dimmer = null;
|
|
302
310
|
for (const ch of channels) {
|
|
311
|
+
if (ch.TYPE === 'DIMMER') {
|
|
312
|
+
dimmer = ch;
|
|
313
|
+
}
|
|
314
|
+
|
|
303
315
|
if (ch.TYPE === 'VIRTUAL_DIMMER') {
|
|
304
316
|
ch.secondary = true;
|
|
317
|
+
if (dimmer) {
|
|
318
|
+
ch.family = dimmer;
|
|
319
|
+
}
|
|
305
320
|
}
|
|
306
321
|
}
|
|
307
322
|
}
|
|
@@ -364,12 +379,16 @@ function deviceBlock(ctx, iface, device) {
|
|
|
364
379
|
const channels = (device.CHILDREN || [])
|
|
365
380
|
.map((address) => list[address])
|
|
366
381
|
.filter(Boolean)
|
|
367
|
-
.map((ch) =>
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
382
|
+
.map((ch) => {
|
|
383
|
+
const description = ctx.description(iface, ch.ADDRESS);
|
|
384
|
+
return {
|
|
385
|
+
...ch,
|
|
386
|
+
index: Number.parseInt(String(ch.ADDRESS).split(':')[1], 10),
|
|
387
|
+
name: ctx.channelName(ch.ADDRESS),
|
|
388
|
+
description: description || {},
|
|
389
|
+
described: Boolean(description),
|
|
390
|
+
};
|
|
391
|
+
})
|
|
373
392
|
.sort((a, b) => a.index - b.index);
|
|
374
393
|
for (const ch of channels) {
|
|
375
394
|
ch.role = channelRole(ch.TYPE, ch.description);
|
|
@@ -378,8 +397,13 @@ function deviceBlock(ctx, iface, device) {
|
|
|
378
397
|
resolveVirtualReceivers(channels);
|
|
379
398
|
|
|
380
399
|
const components = {};
|
|
400
|
+
/** component key → the channel it comes from and whether it is a role's (not a generic) entity */
|
|
401
|
+
const origins = {};
|
|
402
|
+
let current;
|
|
403
|
+
let inGeneric = false;
|
|
381
404
|
const add = (key, e) => {
|
|
382
405
|
components[key] = e;
|
|
406
|
+
origins[key] = {ch: current, role: !inGeneric};
|
|
383
407
|
};
|
|
384
408
|
|
|
385
409
|
const area = ctx.area === false ? undefined : suggestedArea(roomsOf, device.ADDRESS, channels);
|
|
@@ -387,6 +411,8 @@ function deviceBlock(ctx, iface, device) {
|
|
|
387
411
|
const maintenance = channels.find((ch) => ch.index === 0);
|
|
388
412
|
|
|
389
413
|
for (const ch of channels) {
|
|
414
|
+
current = ch;
|
|
415
|
+
inGeneric = false;
|
|
390
416
|
const consumed = new Set();
|
|
391
417
|
const stateCh = ch.stateChannel || ch;
|
|
392
418
|
const sst = (dp) => st(iface, stateCh.ADDRESS, dp);
|
|
@@ -416,6 +442,7 @@ function deviceBlock(ctx, iface, device) {
|
|
|
416
442
|
|
|
417
443
|
const fallback = ch.index === 0 ? 'Maintenance' : `Channel ${ch.index}`;
|
|
418
444
|
const label = labelOf(ch, deviceName, fallback);
|
|
445
|
+
ch.label = label;
|
|
419
446
|
const has = (dp) => Boolean(ch.description[dp]);
|
|
420
447
|
const key = (dp) => `${ch.index}_${dp}`;
|
|
421
448
|
const disabled = ch.secondary ? {en: false} : {};
|
|
@@ -799,6 +826,7 @@ function deviceBlock(ctx, iface, device) {
|
|
|
799
826
|
}
|
|
800
827
|
|
|
801
828
|
// generic layer: everything else in the channel's VALUES description
|
|
829
|
+
inGeneric = true;
|
|
802
830
|
for (const [dp, d] of Object.entries(ch.description)) {
|
|
803
831
|
if (consumed.has(dp) || NOT_ANNOUNCED.has(dp) || !d || typeof d !== 'object') {
|
|
804
832
|
continue;
|
|
@@ -940,6 +968,8 @@ function deviceBlock(ctx, iface, device) {
|
|
|
940
968
|
unreach = {iface, channel: maintenance.ADDRESS, datapoint: 'UNREACH', topic};
|
|
941
969
|
}
|
|
942
970
|
|
|
971
|
+
const placed = placeUnits(ctx, {id, device, deviceName, channels, components, origins, area, roomsOf});
|
|
972
|
+
|
|
943
973
|
return {
|
|
944
974
|
id,
|
|
945
975
|
device: compact({
|
|
@@ -949,16 +979,234 @@ function deviceBlock(ctx, iface, device) {
|
|
|
949
979
|
sw: device.FIRMWARE ? String(device.FIRMWARE) : undefined,
|
|
950
980
|
sa: area,
|
|
951
981
|
}),
|
|
952
|
-
components,
|
|
982
|
+
components: placed.parent,
|
|
983
|
+
children: placed.children,
|
|
984
|
+
own: placed.own,
|
|
985
|
+
all: components,
|
|
986
|
+
unknown: new Set(channels.filter((ch) => !ch.described).map((ch) => ch.index)),
|
|
987
|
+
units: [...placed.units.values()].map((unit) => ({
|
|
988
|
+
index: unit.head.index,
|
|
989
|
+
channels: unit.channels.map((ch) => ch.index),
|
|
990
|
+
function: unit.function,
|
|
991
|
+
place: placed.places.get(unit.head.index),
|
|
992
|
+
platforms: unit.keys.map((key) => ({p: components[key].p, en: components[key].en !== false})),
|
|
993
|
+
})),
|
|
953
994
|
availability,
|
|
954
995
|
unreach,
|
|
955
996
|
};
|
|
956
997
|
}
|
|
957
998
|
|
|
958
999
|
/**
|
|
959
|
-
*
|
|
1000
|
+
* Whether a configuration puts channels into child devices (which need Home Assistant 2026.11).
|
|
1001
|
+
* @param {string} channelDevices the node's model
|
|
1002
|
+
* @param {Object<string, {enabled?: boolean, split?: string, channels?: object}>} devices
|
|
1003
|
+
* @returns {boolean}
|
|
1004
|
+
*/
|
|
1005
|
+
function usesChildDevices(channelDevices, devices) {
|
|
1006
|
+
if (channelDevices === 'children') {
|
|
1007
|
+
return true;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
return Object.values(devices || {}).some(
|
|
1011
|
+
(entry) =>
|
|
1012
|
+
entry &&
|
|
1013
|
+
entry.enabled === true &&
|
|
1014
|
+
(entry.split === 'children' ||
|
|
1015
|
+
Object.values(entry.channels || {}).some((channel) => channel && channel.device === 'child')),
|
|
1016
|
+
);
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* The units of a device as the editor shows them (task 33): per unit its channels, whether it is a
|
|
1021
|
+
* function unit and the entities it yields - from the same code that builds the payload.
|
|
1022
|
+
* @param {DiscoveryContext} ctx devices, description, channelName, generic
|
|
1023
|
+
* @param {string} iface
|
|
1024
|
+
* @param {object} device
|
|
1025
|
+
* @returns {Array<{index: number, channels: number[], function: boolean, platforms: Array<{p: string, en: boolean}>}>}
|
|
1026
|
+
*/
|
|
1027
|
+
function deviceSummary(ctx, iface, device) {
|
|
1028
|
+
const block = deviceBlock(
|
|
1029
|
+
{...ctx, channelDevices: 'off', overrides: {}, statusTopicFor: () => '', setTopicFor: () => ''},
|
|
1030
|
+
iface,
|
|
1031
|
+
device,
|
|
1032
|
+
);
|
|
1033
|
+
return block ? block.units.map(({place: _place, ...unit}) => unit) : [];
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
/** the values of the node option `channelDevices` and of a device's `split` (task 33) */
|
|
1037
|
+
const MODELS = new Set(['off', 'children', 'devices']);
|
|
1038
|
+
/** where a unit goes: on the device, a child device in its payload, a device of its own */
|
|
1039
|
+
const PLACES = new Set(['parent', 'child', 'own']);
|
|
1040
|
+
|
|
1041
|
+
/**
|
|
1042
|
+
* The channels of a device grouped into units: a channel with the channels of its family (an HmIP
|
|
1043
|
+
* transmitter with its virtual receivers, a dimmer with its virtual dimmers). A unit is a
|
|
1044
|
+
* *function* unit when one of its components is a role's entity (switch, light, cover, event,
|
|
1045
|
+
* climate ...) rather than a generic datapoint; the maintenance channel `:0` never is.
|
|
1046
|
+
* @param {object[]} channels sorted by index, after resolveVirtualReceivers()
|
|
1047
|
+
* @param {Object<string, {ch: object, role: boolean}>} origins component key → its channel
|
|
1048
|
+
* @returns {Map<number, {head: object, channels: object[], keys: string[], function: boolean}>}
|
|
1049
|
+
*/
|
|
1050
|
+
function deviceUnits(channels, origins) {
|
|
1051
|
+
const units = new Map();
|
|
1052
|
+
for (const ch of channels) {
|
|
1053
|
+
const head = ch.family || ch;
|
|
1054
|
+
if (!units.has(head.index)) {
|
|
1055
|
+
units.set(head.index, {head, channels: [], keys: [], function: false});
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
units.get(head.index).channels.push(ch);
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
for (const [key, origin] of Object.entries(origins)) {
|
|
1062
|
+
const unit = units.get((origin.ch.family || origin.ch).index);
|
|
1063
|
+
unit.keys.push(key);
|
|
1064
|
+
if (origin.role && unit.head.index !== 0) {
|
|
1065
|
+
unit.function = true;
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
// a channel whose description is not there yet may well be a function: counted as one, so a
|
|
1070
|
+
// device does not fall back to one device while its metadata is incomplete
|
|
1071
|
+
for (const unit of units.values()) {
|
|
1072
|
+
unit.unknown = unit.head.index !== 0 && unit.channels.some((ch) => ch.described === false);
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
return units;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
/**
|
|
1079
|
+
* Where each unit of a device goes (task 33): `drop` (not published), `parent`, `child` or `own`.
|
|
1080
|
+
* A channel's own setting wins, then the device's `split`, then the node's `channelDevices`; a
|
|
1081
|
+
* model other than `off` moves the function units, and only on a device with two or more of them
|
|
1082
|
+
* (a plug or a contact stays one device). The maintenance channel stays on the device.
|
|
960
1083
|
* @param {DiscoveryContext} ctx
|
|
961
|
-
* @param {
|
|
1084
|
+
* @param {string} deviceAddress
|
|
1085
|
+
* @param {Map<number, object>} units
|
|
1086
|
+
* @returns {Map<number, 'drop' | 'parent' | 'child' | 'own'>}
|
|
1087
|
+
*/
|
|
1088
|
+
function placement(ctx, deviceAddress, units) {
|
|
1089
|
+
const config = (ctx.overrides && ctx.overrides[deviceAddress]) || {};
|
|
1090
|
+
const model = MODELS.has(config.split) ? config.split : MODELS.has(ctx.channelDevices) ? ctx.channelDevices : 'off';
|
|
1091
|
+
const functions = [...units.values()].filter((unit) => unit.function || unit.unknown).length;
|
|
1092
|
+
const places = new Map();
|
|
1093
|
+
for (const [index, unit] of units) {
|
|
1094
|
+
const own = (config.channels && config.channels[index]) || {};
|
|
1095
|
+
if (own.publish === false) {
|
|
1096
|
+
places.set(index, 'drop');
|
|
1097
|
+
} else if (index === 0) {
|
|
1098
|
+
places.set(index, 'parent');
|
|
1099
|
+
} else if (PLACES.has(own.device)) {
|
|
1100
|
+
places.set(index, own.device);
|
|
1101
|
+
} else if (model !== 'off' && functions >= 2 && unit.function) {
|
|
1102
|
+
places.set(index, model === 'children' ? 'child' : 'own');
|
|
1103
|
+
} else {
|
|
1104
|
+
places.set(index, 'parent');
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
return places;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
/** a unit's channel whose name is a name of its own (not the CCU's default `<device> <address>:<n>`) */
|
|
1112
|
+
function namedChannel(unit) {
|
|
1113
|
+
return [unit.head, ...unit.channels.filter((ch) => ch !== unit.head && !ch.secondary)].find(
|
|
1114
|
+
(ch) => ch.label && ch.label !== `Channel ${ch.index}` && !/^\d+$/.test(ch.label),
|
|
1115
|
+
);
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
/**
|
|
1119
|
+
* The name of an entity on a child or an own device: Home Assistant puts the device's name in
|
|
1120
|
+
* front, so the channel's part goes - the channel's main entity has no name of its own (`null`,
|
|
1121
|
+
* Home Assistant names it after the device), the others keep their datapoint or role label.
|
|
1122
|
+
* Secondary channels of a family keep their channel's label.
|
|
1123
|
+
*/
|
|
1124
|
+
function shortName(name, ch) {
|
|
1125
|
+
if (ch.secondary || typeof name !== 'string' || !ch.label) {
|
|
1126
|
+
return name;
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
if (name === ch.label) {
|
|
1130
|
+
return null;
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
if (name.startsWith(ch.label + ' ')) {
|
|
1134
|
+
const rest = name.slice(ch.label.length + 1);
|
|
1135
|
+
return rest[0].toUpperCase() + rest.slice(1);
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
return name;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
/**
|
|
1142
|
+
* Splits the components of a device into the device's own, its child devices (`cdevs`, Home
|
|
1143
|
+
* Assistant 2026.11) and devices of their own, by the units' placement. With the model `off` and
|
|
1144
|
+
* no channel settings every component stays on the device, in its order - the payload is the one
|
|
1145
|
+
* of before.
|
|
1146
|
+
*/
|
|
1147
|
+
function placeUnits(ctx, {id, device, deviceName, channels, components, origins, area, roomsOf}) {
|
|
1148
|
+
const units = deviceUnits(channels, origins);
|
|
1149
|
+
const places = placement(ctx, device.ADDRESS, units);
|
|
1150
|
+
const placeOf = (key) => places.get((origins[key].ch.family || origins[key].ch).index);
|
|
1151
|
+
const parent = {};
|
|
1152
|
+
for (const key of Object.keys(components)) {
|
|
1153
|
+
if (placeOf(key) === 'parent') {
|
|
1154
|
+
parent[key] = components[key];
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
const moved = (unit) => {
|
|
1159
|
+
const out = {};
|
|
1160
|
+
for (const key of Object.keys(components)) {
|
|
1161
|
+
if (unit.keys.includes(key)) {
|
|
1162
|
+
out[key] = {...components[key], name: shortName(components[key].name, origins[key].ch)};
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
return out;
|
|
1167
|
+
};
|
|
1168
|
+
|
|
1169
|
+
const children = [];
|
|
1170
|
+
const own = [];
|
|
1171
|
+
for (const [index, unit] of units) {
|
|
1172
|
+
const place = places.get(index);
|
|
1173
|
+
if ((place !== 'child' && place !== 'own') || unit.keys.length === 0) {
|
|
1174
|
+
continue;
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
const named = namedChannel(unit);
|
|
1178
|
+
if (place === 'child') {
|
|
1179
|
+
children.push({
|
|
1180
|
+
id: `${id}_${index}`,
|
|
1181
|
+
name: named ? named.label : `Channel ${index}`,
|
|
1182
|
+
components: moved(unit),
|
|
1183
|
+
});
|
|
1184
|
+
continue;
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
const rest = unit.channels.filter((ch) => ch !== unit.head);
|
|
1188
|
+
const ownArea = ctx.area === false ? undefined : suggestedArea(roomsOf, unit.head.ADDRESS, rest) || area;
|
|
1189
|
+
own.push({
|
|
1190
|
+
id: `${id}_${index}`,
|
|
1191
|
+
device: compact({
|
|
1192
|
+
name: named ? named.name || named.label : `${deviceName} Channel ${index}`,
|
|
1193
|
+
mf: 'eQ-3',
|
|
1194
|
+
mdl: device.TYPE,
|
|
1195
|
+
sw: device.FIRMWARE ? String(device.FIRMWARE) : undefined,
|
|
1196
|
+
sa: ownArea,
|
|
1197
|
+
via_device: id,
|
|
1198
|
+
}),
|
|
1199
|
+
components: moved(unit),
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
return {parent, children, own, units, places};
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
/**
|
|
1207
|
+
* The full discovery message of one device block (with its child devices, if any).
|
|
1208
|
+
* @param {DiscoveryContext} ctx
|
|
1209
|
+
* @param {{id: string, device: object, components: object, availability: object[], children?: object[]}} block
|
|
962
1210
|
* @returns {{topic: string, payload: object}}
|
|
963
1211
|
*/
|
|
964
1212
|
function devicePayload(ctx, block) {
|
|
@@ -970,10 +1218,32 @@ function devicePayload(ctx, block) {
|
|
|
970
1218
|
...(block.availability.length > 1 && {avty_mode: 'all'}),
|
|
971
1219
|
qos: 0,
|
|
972
1220
|
cmps: block.components,
|
|
1221
|
+
...(block.children &&
|
|
1222
|
+
block.children.length > 0 && {
|
|
1223
|
+
cdevs: block.children.map((child) => ({ids: [child.id], name: child.name, cmps: child.components})),
|
|
1224
|
+
}),
|
|
973
1225
|
};
|
|
974
1226
|
return {topic: discoveryTopic(ctx.prefix, block.id), payload};
|
|
975
1227
|
}
|
|
976
1228
|
|
|
1229
|
+
/**
|
|
1230
|
+
* The discovery messages of a device's channels that are devices of their own (`devices`): the
|
|
1231
|
+
* device's model, firmware and availability, `via_device` the device.
|
|
1232
|
+
* @param {DiscoveryContext} ctx
|
|
1233
|
+
* @param {object} block
|
|
1234
|
+
* @returns {Array<{topic: string, payload: object}>}
|
|
1235
|
+
*/
|
|
1236
|
+
function ownPayloads(ctx, block) {
|
|
1237
|
+
return (block.own || []).map((own) =>
|
|
1238
|
+
devicePayload(ctx, {
|
|
1239
|
+
id: own.id,
|
|
1240
|
+
device: own.device,
|
|
1241
|
+
components: own.components,
|
|
1242
|
+
availability: block.availability,
|
|
1243
|
+
}),
|
|
1244
|
+
);
|
|
1245
|
+
}
|
|
1246
|
+
|
|
977
1247
|
/**
|
|
978
1248
|
* Empty retained config: removes the device (and all its entities) from Home Assistant.
|
|
979
1249
|
* @param {string} prefix
|
|
@@ -984,39 +1254,145 @@ function removalMessage(prefix, address) {
|
|
|
984
1254
|
return {topic: discoveryTopic(prefix, discoveryId(address)), payload: ''};
|
|
985
1255
|
}
|
|
986
1256
|
|
|
1257
|
+
/** component key → platform of a discovery payload, its children's included */
|
|
1258
|
+
function keysOf(payload) {
|
|
1259
|
+
const keys = {};
|
|
1260
|
+
const take = (cmps) => {
|
|
1261
|
+
for (const [key, component] of Object.entries(cmps || {})) {
|
|
1262
|
+
if (component && Object.keys(component).length > 1) {
|
|
1263
|
+
keys[key] = component.p;
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
};
|
|
1267
|
+
|
|
1268
|
+
take(payload.cmps);
|
|
1269
|
+
for (const child of payload.cdevs || []) {
|
|
1270
|
+
take(child.cmps);
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
return keys;
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
/** the channel index a component key starts with (`<index>_<datapoint>`) */
|
|
1277
|
+
function channelOfKey(key) {
|
|
1278
|
+
return Number.parseInt(String(key).split('_')[0], 10);
|
|
1279
|
+
}
|
|
1280
|
+
|
|
987
1281
|
/**
|
|
988
1282
|
* Discovery messages for the selected devices.
|
|
1283
|
+
*
|
|
1284
|
+
* Removal (task 33): Home Assistant keeps a component that is left out of a device's payload, it
|
|
1285
|
+
* removes one that is sent with only its platform (`{p}`). `ctx.previous` holds, per device, what
|
|
1286
|
+
* the last run published (topic → component key → platform); a key published there and missing now
|
|
1287
|
+
* - a channel no longer published, or moved to a device of its own - is sent as such a stub once,
|
|
1288
|
+
* and a topic of the last run that has no payload now is cleared. Without a record for the device's
|
|
1289
|
+
* topic (the first run) every component of the device counts as published there. Components of
|
|
1290
|
+
* channels whose description is not known (yet) are neither removed nor forgotten.
|
|
989
1291
|
* @param {DiscoveryContext} ctx
|
|
990
|
-
* @returns {{messages: Array<{topic: string, payload: object}>,
|
|
991
|
-
*
|
|
992
|
-
*
|
|
1292
|
+
* @returns {{messages: Array<{topic: string, payload: object}>, clear: Array<{topic: string, payload: string}>,
|
|
1293
|
+
* published: Object<string, Object<string, Object<string, string>>>,
|
|
1294
|
+
* availability: Availability[], missing: string[], empty: string[]}}
|
|
1295
|
+
* messages: the device payloads (devices of their own after their device); clear: topics to
|
|
1296
|
+
* empty first; published: the record for the next run; availability: the UNREACH
|
|
1297
|
+
* datapoints the configs use as availability (B-38); missing: selected addresses not
|
|
1298
|
+
* found in the metadata; empty: devices without entities
|
|
993
1299
|
*/
|
|
994
1300
|
function discoveryMessages(ctx) {
|
|
995
1301
|
const messages = [];
|
|
1302
|
+
const clear = [];
|
|
1303
|
+
const published = {};
|
|
996
1304
|
const availability = [];
|
|
997
1305
|
const missing = [];
|
|
998
1306
|
const empty = [];
|
|
1307
|
+
const previous = ctx.previous || {};
|
|
999
1308
|
for (const address of ctx.selected || []) {
|
|
1000
1309
|
const iface = ifaceOf(ctx.devices, address);
|
|
1001
1310
|
const device = iface && ctx.devices[iface][address];
|
|
1002
1311
|
if (!device || device.PARENT) {
|
|
1003
1312
|
missing.push(address);
|
|
1313
|
+
if (previous[address]) {
|
|
1314
|
+
published[address] = previous[address];
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1004
1317
|
continue;
|
|
1005
1318
|
}
|
|
1006
1319
|
|
|
1007
1320
|
const block = deviceBlock(ctx, iface, device);
|
|
1008
1321
|
if (!block) {
|
|
1009
1322
|
empty.push(address);
|
|
1323
|
+
if (previous[address]) {
|
|
1324
|
+
published[address] = previous[address];
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1010
1327
|
continue;
|
|
1011
1328
|
}
|
|
1012
1329
|
|
|
1013
|
-
|
|
1014
|
-
|
|
1330
|
+
const before = previous[address] || {};
|
|
1331
|
+
const parentTopic = discoveryTopic(ctx.prefix, block.id);
|
|
1332
|
+
const all = {};
|
|
1333
|
+
for (const [key, component] of Object.entries(block.all)) {
|
|
1334
|
+
all[key] = component.p;
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
const record = {};
|
|
1338
|
+
const out = [];
|
|
1339
|
+
const parent = devicePayload(ctx, block);
|
|
1340
|
+
const candidates = [parent, ...ownPayloads(ctx, block)];
|
|
1341
|
+
for (const message of candidates) {
|
|
1342
|
+
const now = keysOf(message.payload);
|
|
1343
|
+
const last = before[message.topic] || (!previous[address] && message.topic === parentTopic ? all : {});
|
|
1344
|
+
const stubs = {};
|
|
1345
|
+
const kept = {...now};
|
|
1346
|
+
for (const [key, platform] of Object.entries(last)) {
|
|
1347
|
+
if (key in now) {
|
|
1348
|
+
continue;
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
if (block.unknown.has(channelOfKey(key))) {
|
|
1352
|
+
kept[key] = platform;
|
|
1353
|
+
} else if (platform) {
|
|
1354
|
+
stubs[key] = {p: platform};
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
if (Object.keys(now).length === 0 && Object.keys(kept).length === 0) {
|
|
1359
|
+
// nothing left on this topic: clear it when the last run published it
|
|
1360
|
+
if (Object.keys(last).length > 0) {
|
|
1361
|
+
clear.push({topic: message.topic, payload: ''});
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
continue;
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
if (Object.keys(stubs).length > 0) {
|
|
1368
|
+
message.payload.cmps = {...message.payload.cmps, ...stubs};
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
record[message.topic] = kept;
|
|
1372
|
+
out.push(message);
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
// topics of the last run this one has no payload for (a channel back on its device)
|
|
1376
|
+
for (const [topic, keys] of Object.entries(before)) {
|
|
1377
|
+
if (record[topic] || candidates.some((m) => m.topic === topic)) {
|
|
1378
|
+
continue;
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
if (Object.keys(keys).some((key) => block.unknown.has(channelOfKey(key)))) {
|
|
1382
|
+
record[topic] = keys;
|
|
1383
|
+
} else {
|
|
1384
|
+
clear.push({topic, payload: ''});
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
published[address] = record;
|
|
1389
|
+
messages.push(...out);
|
|
1390
|
+
if (block.unreach && out.length > 0) {
|
|
1015
1391
|
availability.push(block.unreach);
|
|
1016
1392
|
}
|
|
1017
1393
|
}
|
|
1018
1394
|
|
|
1019
|
-
return {messages, availability, missing, empty};
|
|
1395
|
+
return {messages, clear, published, availability, missing, empty};
|
|
1020
1396
|
}
|
|
1021
1397
|
|
|
1022
1398
|
module.exports = {
|
|
@@ -1031,6 +1407,11 @@ module.exports = {
|
|
|
1031
1407
|
suggestedArea,
|
|
1032
1408
|
deviceBlock,
|
|
1033
1409
|
devicePayload,
|
|
1410
|
+
ownPayloads,
|
|
1411
|
+
deviceUnits,
|
|
1412
|
+
placement,
|
|
1413
|
+
usesChildDevices,
|
|
1414
|
+
deviceSummary,
|
|
1034
1415
|
removalMessage,
|
|
1035
1416
|
discoveryMessages,
|
|
1036
1417
|
};
|
package/nodes/lib/lite.js
CHANGED
|
@@ -277,7 +277,8 @@ class LiteConnection {
|
|
|
277
277
|
* @param {string} options.token the credential
|
|
278
278
|
* @param {boolean} [options.insecure] accept any certificate
|
|
279
279
|
* @param {string} [options.fingerprint256] accept exactly this certificate (pinned at pairing; wins over insecure)
|
|
280
|
-
* @param {string[]} options.interfaces the interfaces
|
|
280
|
+
* @param {string[]} options.interfaces the interfaces the connection uses (the enabled ones lite-rpc carries;
|
|
281
|
+
* addon interfaces are added with follow()); the client itself follows all the system lists
|
|
281
282
|
* @param {function(string): string} options.initId the init id the connection's callback table expects per interface
|
|
282
283
|
* @param {function(string, *, Array, Function)} options.onCallback (method, err, params, answer): a callback server's call
|
|
283
284
|
* @param {function(string, string)} options.onInterface (iface, state): up | down | restarted | added | removed
|
|
@@ -312,6 +313,16 @@ class LiteConnection {
|
|
|
312
313
|
return this.interfaces.includes(iface);
|
|
313
314
|
}
|
|
314
315
|
|
|
316
|
+
/**
|
|
317
|
+
* task 32: an interface the connection uses from now on (an addon's, named by the system).
|
|
318
|
+
* @param {string} iface
|
|
319
|
+
*/
|
|
320
|
+
follow(iface) {
|
|
321
|
+
if (!this.interfaces.includes(iface)) {
|
|
322
|
+
this.interfaces.push(iface);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
315
326
|
/**
|
|
316
327
|
* Opens the client. Resolves once the first picture is loaded. occulite-client (0.4) does not
|
|
317
328
|
* try a failed connect again (its streams only reconnect once connected), so a system that
|
|
@@ -364,7 +375,9 @@ class LiteConnection {
|
|
|
364
375
|
url: this.options.url,
|
|
365
376
|
auth: {token: this.options.token},
|
|
366
377
|
transport: nodeTransport(trustOf(this.options)),
|
|
367
|
-
|
|
378
|
+
// task 32: no interface filter - the client follows every interface the system lists, so
|
|
379
|
+
// an addon's interface (Logic) is seen, also one installed later (a filter would hide its
|
|
380
|
+
// `interface added`); the connection takes what it uses (initId)
|
|
368
381
|
values: true,
|
|
369
382
|
meta: Boolean(this.options.meta),
|
|
370
383
|
devices: true,
|