-
Notifications
You must be signed in to change notification settings - Fork 9
/
IPSymconExtension.js
157 lines (138 loc) · 5.8 KB
/
IPSymconExtension.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
/*
IPSymconExtension
Version: 4.4.5
*/
const ZigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
class IPSymconExtension {
constructor(zigbee, mqtt, state, publishEntityState, eventBus, settings, logger) {
this.zigbee = zigbee;
this.mqtt = mqtt;
this.state = state;
this.publishEntityState = publishEntityState;
this.eventBus = eventBus;
this.settings = settings;
this.logger = logger;
this.zigbeeHerdsmanConverters = ZigbeeHerdsmanConverters;
this.baseTopic = settings.get().mqtt.base_topic;
this.symconTopic = 'symcon';
this.eventBus.on('mqttMessage', this.onMQTTMessage.bind(this), this);
logger.info('Loaded IP-Symcon Extension');
}
async start() {
this.mqtt.subscribe(`${this.symconTopic}/${this.baseTopic}/#`);
}
async onMQTTMessage(data) {
const topicPrefix = `${this.symconTopic}/${this.baseTopic}`;
switch (data.topic) {
case `${topicPrefix}/getDevices`:
case `${this.baseTopic}/bridge/symcon/getDevices`:
let devices = [];
try {
for (const device of this.zigbee.devicesIterator(this.#deviceNotCoordinator)) {
devices = devices.concat(this.#createDevicePayload(device, false));
}
} catch (error) {
devices = this.zigbee.devices(false).map(device => this.#createDevicePayload(device, false));
}
this.logger.info('Symcon: publish devices list');
await this.#publishToMqtt('devices', devices);
break;
case `${topicPrefix}/getDevice`:
case `${this.baseTopic}/bridge/symcon/getDevice`:
if (data.message) {
const device = this.zigbee.resolveEntity(data.message);
const devices = this.#createDevicePayload(device, true);
this.logger.info('Symcon: getDevice');
await this.#publishToMqtt(`${device.name}/deviceInfo`, devices);
}
break;
case `${topicPrefix}/getGroups`:
case `${this.baseTopic}/bridge/symcon/getGroups`:
const groups = this.settings.getGroups();
await this.#publishToMqtt('groups', groups);
break;
case `${topicPrefix}/getGroup`:
case `${this.baseTopic}/bridge/symcon/getGroup`:
if (data.message) {
const groupExposes = this.#createGroupExposes(data.message);
await this.#publishToMqtt(`${data.message}/groupInfo`, groupExposes);
}
break;
default:
console.log('Unhandled MQTT topic');
}
}
async stop() {
this.eventBus.removeListeners(this);
}
#createDevicePayload(device, boolExposes) {
const definition = this.zigbeeHerdsmanConverters.findByDevice(device.zh);
let exposes;
if (boolExposes) {
exposes = device.exposes();
}
return {
ieeeAddr: device.ieeeAddr,
type: device.zh.type,
networkAddress: device.zh.networkAddress,
model: device.definition?.model ?? 'Unknown Model',
vendor: device.definition?.vendor ?? 'Unknown Vendor',
description: device.definition?.description ?? 'No description',
friendly_name: device.name,
manufacturerName: device.zh.manufacturerName,
powerSource: device.zh.powerSource,
modelID: device.zh.modelID,
exposes: exposes,
};
}
async #publishToMqtt(topicSuffix, payload) {
await this.mqtt.publish(`${topicSuffix}`, JSON.stringify(payload), { retain: false, qos: 0 }, `${this.symconTopic}/${this.baseTopic}`, false, false);
}
#createGroupExposes(groupName) {
const groupSupportedTypes = ['light', 'switch', 'lock', 'cover'];
const groups = this.settings.getGroups();
const groupExposes = {};
groupSupportedTypes.forEach(type => groupExposes[type] = { type, features: [] });
groups.forEach(group => {
if (group.friendly_name === groupName) {
this.#processGroupDevices(group, groupExposes);
}
});
return groupExposes;
}
#processGroupDevices(group, groupExposes) {
group.devices.forEach(deviceAddress => {
const device = this.zigbee.resolveEntity(deviceAddress.substring(0, deviceAddress.indexOf('/')));
this.#addDeviceExposesToGroup(device, groupExposes);
});
}
#addDeviceExposesToGroup(device, groupExposes) {
let exposes = [];
// Überprüfen, ob 'definition' vorhanden ist und Exposes hinzufügen
if (device.definition && device.definition.exposes) {
exposes = exposes.concat(device.definition.exposes);
}
// Überprüfen, ob '_definition' vorhanden ist und Exposes hinzufügen
if (device._definition && device._definition.exposes) {
exposes = exposes.concat(device._definition.exposes);
}
// Verarbeite alle gesammelten Exposes
exposes.forEach(expose => {
const type = expose.type;
if (groupExposes[type]) {
this.#processExposeFeatures(expose, groupExposes[type]);
}
});
}
#processExposeFeatures(expose, groupExposeType) {
expose.features.forEach(feature => {
if (!groupExposeType.features.some(f => f.property === feature.property)) {
groupExposeType.features.push(feature);
}
});
}
#deviceNotCoordinator(device) {
return device.type !== 'Coordinator';
}
}
module.exports = IPSymconExtension;