matterbridge-mqtt 0.0.2 → 0.0.3
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 +7 -5
- package/README.md +81 -2
- package/dist/module.js +154 -10
- package/dist/mqtt.js +145 -0
- package/matterbridge-mqtt-local.config.json +20 -0
- package/matterbridge-mqtt-local.schema.json +115 -0
- package/matterbridge-mqtt-private.config.json +20 -0
- package/matterbridge-mqtt-private.schema.json +115 -0
- package/matterbridge-mqtt.config.json +20 -0
- package/matterbridge-mqtt.schema.json +115 -0
- package/npm-shrinkwrap.json +550 -27
- package/package.json +20 -8
- package/dist/module.d.ts +0 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
+
<!-- eslint-disable markdown/no-missing-label-refs -->
|
|
2
|
+
|
|
1
3
|
# <img src="https://matterbridge.io/assets/matterbridge.svg" alt="Matterbridge Logo" width="64px" height="64px"> Matterbridge mqtt plugin changelog
|
|
2
4
|
|
|
3
5
|
All notable changes to this project will be documented in this file.
|
|
4
6
|
|
|
5
7
|
If you like this project and find it useful, please consider giving it a star on GitHub at https://github.com/Luligu/matterbridge-mqtt and sponsoring it.
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
<a href="https://www.buymeacoffee.com/luligugithub"><img src="https://matterbridge.io/assets/bmc-button.svg" alt="Buy me a coffee" width="120"></a>
|
|
10
|
+
|
|
11
|
+
## [0.0.3] - Dev branch
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
- Initial commit (repository reset).
|
|
10
14
|
|
|
11
|
-
<a href="https://www.buymeacoffee.com/luligugithub">
|
|
12
|
-
<img src="https://matterbridge.io/assets/bmc-button.svg" alt="Buy me a coffee" width="120">
|
|
13
|
-
</a>
|
|
15
|
+
<a href="https://www.buymeacoffee.com/luligugithub"><img src="https://matterbridge.io/assets/bmc-button.svg" alt="Buy me a coffee" width="80"></a>
|
|
14
16
|
|
|
15
17
|
<!-- Commented out section
|
|
16
18
|
## [1.1.2] - 2024-03-08
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
<!-- eslint-disable markdown/no-multiple-h1 -->
|
|
2
|
+
|
|
1
3
|
# <img src="https://matterbridge.io/assets/matterbridge.svg" alt="Matterbridge Logo" width="64px" height="64px"> Matterbridge mqtt plugin
|
|
2
4
|
|
|
3
5
|
[](https://www.npmjs.com/package/matterbridge-mqtt)
|
|
@@ -5,7 +7,15 @@
|
|
|
5
7
|
[](https://hub.docker.com/r/luligu/matterbridge)
|
|
6
8
|
[](https://hub.docker.com/r/luligu/matterbridge)
|
|
7
9
|

|
|
8
|
-

|
|
11
|
+
[](https://codecov.io/gh/Luligu/matterbridge-mqtt)
|
|
12
|
+
[](https://oxc.rs/docs/guide/usage/formatter.html)
|
|
13
|
+
[](https://oxc.rs/docs/guide/usage/linter.html)
|
|
14
|
+
[](https://www.typescriptlang.org/)
|
|
15
|
+
[](https://github.com/microsoft/typescript-go)
|
|
16
|
+
[](https://nodejs.org/)
|
|
17
|
+
[](https://matterbridge.io)
|
|
18
|
+

|
|
9
19
|
|
|
10
20
|
[](https://www.npmjs.com/package/matterbridge)
|
|
11
21
|
[](https://www.npmjs.com/package/matter-history)
|
|
@@ -14,4 +24,73 @@
|
|
|
14
24
|
|
|
15
25
|
---
|
|
16
26
|
|
|
17
|
-
|
|
27
|
+
# Features
|
|
28
|
+
|
|
29
|
+
The **matterbridge-mqtt** plugin bridges any MQTT-capable device into the Matter ecosystem through Matterbridge, without requiring device-specific firmware or custom integrations.
|
|
30
|
+
|
|
31
|
+
- **Zero-code device onboarding** — send a single retained `config` message and Matterbridge automatically creates and registers the corresponding Matter endpoint.
|
|
32
|
+
- **Broad device-type support** — lights (on/off, dimmable, colour-temperature, extended colour), plugs and outlets, mounted switches, sensors (contact, temperature, humidity, pressure, flow, light, occupancy, smoke/CO, air quality, rain, soil, water freeze/leak), pump, water valve, power source, and electrical sensor.
|
|
33
|
+
- **Live state sync** — retained `state` messages drive cluster attribute updates so controllers always see the current device state, even after a restart.
|
|
34
|
+
- **Matter command forwarding** — every Matter command (e.g. `on`, `off`, `moveToLevel`) is published back to the device's `command` topic as a structured JSON payload.
|
|
35
|
+
- **Flexible MQTT connectivity** — configurable broker host, port, protocol version (3/4/5), credentials, client ID, and full TLS support (CA certificate, client certificate/key, `rejectUnauthorized`).
|
|
36
|
+
- **Topic-based multi-device management** — a single broker connection and a single base topic handle an arbitrary number of devices; each device is identified by its `deviceId` path segment.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
# How it works
|
|
41
|
+
|
|
42
|
+
Topic: matterbridge
|
|
43
|
+
|
|
44
|
+
## Publish
|
|
45
|
+
|
|
46
|
+
Each device shall publish with retain:
|
|
47
|
+
|
|
48
|
+
### config
|
|
49
|
+
|
|
50
|
+
- **matterbridge/deviceid/config/root**
|
|
51
|
+
|
|
52
|
+
only the fixed and optional attributes shall be published here
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
const config = {
|
|
56
|
+
deviceTypes: ['Dimmable Light'],
|
|
57
|
+
clusters: { BridgedDeviceBasicInformation: { NodeLabel: 'Light 1', SerialNumber: 'xxx-yyy-xxx' }, LevelControl: { onLevel: 128 } },
|
|
58
|
+
};
|
|
59
|
+
publish('matterbridge/light1/config/root', JSON.stringify(config));
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### state
|
|
63
|
+
|
|
64
|
+
- **matterbridge/deviceid/state/root**
|
|
65
|
+
|
|
66
|
+
only the current attributes shall be published here
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
const state = { OnOff: { onOff: false }, LevelControl: { currentLevel: 254 } };
|
|
70
|
+
publish('matterbridge/deviceid/state/root', JSON.stringify(state));
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Subscribe
|
|
74
|
+
|
|
75
|
+
Each device shall subscribe:
|
|
76
|
+
|
|
77
|
+
### command
|
|
78
|
+
|
|
79
|
+
- **matterbridge/deviceid/command/root**
|
|
80
|
+
|
|
81
|
+
It will receive all Matter commands.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
Topic: 'matterbridge/deviceid/command/root'
|
|
85
|
+
Payload: "{"cluster":"OnOff","command":"on","request": MatterRequest | undefined }"
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
# Todo
|
|
91
|
+
|
|
92
|
+
- [ ] Command handler
|
|
93
|
+
- [ ] Attributes handler in config
|
|
94
|
+
- [ ] Add Chapter 8. Entry Control Device Types
|
|
95
|
+
- [ ] Add Chapter 9. HVAC Device Types
|
|
96
|
+
- [ ] Add composed device types
|
package/dist/module.js
CHANGED
|
@@ -1,27 +1,171 @@
|
|
|
1
|
-
import { MatterbridgeDynamicPlatform } from 'matterbridge';
|
|
1
|
+
import { airQualitySensor, bridgedNode, colorTemperatureLight, contactSensor, dimmableLight, dimmableMountedSwitch, dimmableOutlet, electricalSensor, extendedColorLight, flowSensor, humiditySensor, lightSensor, MatterbridgeDynamicPlatform, MatterbridgeEndpoint, occupancySensor, onOffLight, onOffMountedSwitch, onOffOutlet, powerSource, pressureSensor, pumpDevice, rainSensor, smokeCoAlarm, soilSensor, temperatureSensor, waterFreezeDetector, waterLeakDetector, waterValve, } from 'matterbridge';
|
|
2
|
+
import { debugStringify, info, warn } from 'matterbridge/logger';
|
|
3
|
+
import { fireAndForget, inspectError } from 'matterbridge/utils';
|
|
4
|
+
import { MqttService } from './mqtt.js';
|
|
2
5
|
export default function initializePlugin(matterbridge, log, config) {
|
|
3
|
-
return new
|
|
6
|
+
return new MqttPlatform(matterbridge, log, config);
|
|
4
7
|
}
|
|
5
|
-
export class
|
|
8
|
+
export class MqttPlatform extends MatterbridgeDynamicPlatform {
|
|
9
|
+
config;
|
|
10
|
+
mqtt;
|
|
11
|
+
state = new Map();
|
|
6
12
|
constructor(matterbridge, log, config) {
|
|
7
13
|
super(matterbridge, log, config);
|
|
8
|
-
|
|
9
|
-
|
|
14
|
+
this.config = config;
|
|
15
|
+
if (typeof this.verifyMatterbridgeVersion !== 'function' || !this.verifyMatterbridgeVersion('3.8.0')) {
|
|
16
|
+
throw new Error(`This plugin requires Matterbridge version >= "3.8.0". Please update Matterbridge to the latest version in the frontend.`);
|
|
10
17
|
}
|
|
11
|
-
this.log.info(
|
|
12
|
-
this.
|
|
18
|
+
this.log.info(`Initializing platform: ${this.config.name}`);
|
|
19
|
+
this.mqtt = new MqttService(this.config);
|
|
20
|
+
this.log.info(`Finished initializing platform: ${this.config.name}`);
|
|
13
21
|
}
|
|
14
22
|
async onStart(reason) {
|
|
15
|
-
this.log.info(
|
|
23
|
+
this.log.info(`onStart called with reason: ${reason}`);
|
|
24
|
+
await this.ready;
|
|
25
|
+
this.mqtt.on('connect', () => {
|
|
26
|
+
this.log.info('MQTT connected');
|
|
27
|
+
void this.mqtt.subscribe(`${this.config.topic}/#`);
|
|
28
|
+
});
|
|
29
|
+
this.mqtt.on('subscribed', (topic) => {
|
|
30
|
+
this.log.info(`MQTT subscribed to topic: ${topic}`);
|
|
31
|
+
});
|
|
32
|
+
this.mqtt.on('published', (topic, payload) => {
|
|
33
|
+
this.log.info(`MQTT published to topic: ${topic} with payload: ${payload}`);
|
|
34
|
+
});
|
|
35
|
+
this.mqtt.on('close', () => {
|
|
36
|
+
this.log.info('MQTT connection closed');
|
|
37
|
+
});
|
|
38
|
+
this.mqtt.on('reconnect', () => {
|
|
39
|
+
this.log.debug('MQTT reconnecting');
|
|
40
|
+
});
|
|
41
|
+
this.mqtt.on('error', (error) => {
|
|
42
|
+
this.log.error(`MQTT error: ${error.message}`);
|
|
43
|
+
});
|
|
44
|
+
this.mqtt.on('message', (topic, payload) => {
|
|
45
|
+
this.mqttMessageHandler(topic, payload);
|
|
46
|
+
});
|
|
47
|
+
await this.mqtt.connect();
|
|
16
48
|
}
|
|
17
49
|
async onConfigure() {
|
|
18
50
|
await super.onConfigure();
|
|
19
51
|
this.log.info('onConfigure called');
|
|
52
|
+
for (const [topic, payload] of this.state.entries()) {
|
|
53
|
+
await this.updateHandler(topic, payload);
|
|
54
|
+
}
|
|
20
55
|
}
|
|
21
56
|
async onShutdown(reason) {
|
|
22
57
|
await super.onShutdown(reason);
|
|
23
|
-
this.log.info(
|
|
24
|
-
|
|
58
|
+
this.log.info(`onShutdown called with reason: ${reason}`);
|
|
59
|
+
await this.mqtt.close();
|
|
60
|
+
if (this.config.unregisterOnShutdown)
|
|
25
61
|
await this.unregisterAllDevices();
|
|
26
62
|
}
|
|
63
|
+
mqttTopicParser(topic) {
|
|
64
|
+
const regex = new RegExp(`^${this.config.topic}/([^/]+)/([^/]+)/([^/]+)$`);
|
|
65
|
+
const match = topic.match(regex);
|
|
66
|
+
if (!match) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const [, deviceId, subTopic, endpointName] = match;
|
|
70
|
+
return { deviceId, subTopic, endpointName };
|
|
71
|
+
}
|
|
72
|
+
mqttMessageHandler(topic, payload) {
|
|
73
|
+
try {
|
|
74
|
+
const message = JSON.parse(payload);
|
|
75
|
+
this.log.debug(`MQTT message on '${topic}': ${debugStringify(message)}`);
|
|
76
|
+
const parsed = this.mqttTopicParser(topic);
|
|
77
|
+
if (!parsed) {
|
|
78
|
+
this.log.warn(`Received MQTT message on topic '${topic}' that does not match expected format. Ignoring.`);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const { deviceId, subTopic, endpointName } = parsed;
|
|
82
|
+
if (subTopic === 'config') {
|
|
83
|
+
this.log.info(`Received ${info.bgMagenta.black.bold ` config `} message for device ${info.bgCyan.black.bold ` ${deviceId} `} endpoint ${info.bgGreen.black.bold ` ${endpointName} `}`);
|
|
84
|
+
this.createrDevice(deviceId, endpointName, message);
|
|
85
|
+
}
|
|
86
|
+
else if (subTopic === 'state') {
|
|
87
|
+
this.log.info(`Received ${info.bgMagenta.black.bold ` state `} message for device ${info.bgCyan.black.bold ` ${deviceId} `} endpoint ${info.bgGreen.black.bold ` ${endpointName} `}`);
|
|
88
|
+
this.state.set(topic, payload);
|
|
89
|
+
if (this.isConfigured)
|
|
90
|
+
fireAndForget(this.updateHandler(topic, payload), this.log, `Failed to handle state update for device ${deviceId} on endpoint ${endpointName}`);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
this.log.warn(`Received MQTT message with unrecognized subTopic ${warn.magenta.bold `${subTopic}`} on topic ${warn.success.bold `${topic}`}. Ignoring.`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
inspectError(this.log, `Failed to parse MQTT message on '${topic}' with payload ${payload.replaceAll('\n', '')}`, error);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
createrDevice(deviceId, endpointName, jsonPayload) {
|
|
101
|
+
this.log.debug(`Creating device with ID ${deviceId} and endpoint ${endpointName} based on config: ${debugStringify(jsonPayload)}`);
|
|
102
|
+
const deviceTypes = [];
|
|
103
|
+
const supportedDeviceTypes = [
|
|
104
|
+
{ name: 'Power Source', deviceType: powerSource },
|
|
105
|
+
{ name: 'Electrical Sensor', deviceType: electricalSensor },
|
|
106
|
+
{ name: 'OnOff Light', deviceType: onOffLight },
|
|
107
|
+
{ name: 'Dimmable Light', deviceType: dimmableLight },
|
|
108
|
+
{ name: 'Color Temperature Light', deviceType: colorTemperatureLight },
|
|
109
|
+
{ name: 'Extended Color Light', deviceType: extendedColorLight },
|
|
110
|
+
{ name: 'OnOff Plugin Unit', deviceType: onOffOutlet },
|
|
111
|
+
{ name: 'Dimmable PlugIn Unit', deviceType: dimmableOutlet },
|
|
112
|
+
{ name: 'Mounted OnOff Control', deviceType: onOffMountedSwitch },
|
|
113
|
+
{ name: 'Mounted Dimmable Load Control', deviceType: dimmableMountedSwitch },
|
|
114
|
+
{ name: 'Pump', deviceType: pumpDevice },
|
|
115
|
+
{ name: 'Water Valve', deviceType: waterValve },
|
|
116
|
+
{ name: 'Contact Sensor', deviceType: contactSensor },
|
|
117
|
+
{ name: 'Light Sensor', deviceType: lightSensor },
|
|
118
|
+
{ name: 'Occupancy Sensor', deviceType: occupancySensor },
|
|
119
|
+
{ name: 'Temperature Sensor', deviceType: temperatureSensor },
|
|
120
|
+
{ name: 'Pressure Sensor', deviceType: pressureSensor },
|
|
121
|
+
{ name: 'Flow Sensor', deviceType: flowSensor },
|
|
122
|
+
{ name: 'Humidity Sensor', deviceType: humiditySensor },
|
|
123
|
+
{ name: 'Smoke CO Alarm', deviceType: smokeCoAlarm },
|
|
124
|
+
{ name: 'Air Quality Sensor', deviceType: airQualitySensor },
|
|
125
|
+
{ name: 'Water Freeze Detector', deviceType: waterFreezeDetector },
|
|
126
|
+
{ name: 'Water Leak Detector', deviceType: waterLeakDetector },
|
|
127
|
+
{ name: 'Rain Sensor', deviceType: rainSensor },
|
|
128
|
+
{ name: 'Soil Sensor', deviceType: soilSensor },
|
|
129
|
+
];
|
|
130
|
+
for (const name of jsonPayload.deviceTypes) {
|
|
131
|
+
const found = supportedDeviceTypes.find((dt) => dt.name === name);
|
|
132
|
+
if (found) {
|
|
133
|
+
deviceTypes.push(found.deviceType);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
this.log.warn(`Device type '${name}' is not supported. Skipping.`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
deviceTypes.push(bridgedNode);
|
|
140
|
+
const info = jsonPayload.clusters.BridgedDeviceBasicInformation || {};
|
|
141
|
+
const device = new MatterbridgeEndpoint(deviceTypes, {
|
|
142
|
+
id: deviceId,
|
|
143
|
+
})
|
|
144
|
+
.createDefaultBridgedDeviceBasicInformationClusterServer(info.NodeLabel ?? deviceId, info.SerialNumber ?? deviceId, info.VendorId, info.VendorName, info.ProductName, info.SoftwareVersion, info.SoftwareVersionString, info.HardwareVersion, info.HardwareVersionString)
|
|
145
|
+
.addRequiredClusters();
|
|
146
|
+
fireAndForget(this.registerDevice(device), this.log, `Failed to register device ${deviceId}`);
|
|
147
|
+
}
|
|
148
|
+
async updateHandler(topic, payload) {
|
|
149
|
+
const { deviceId, subTopic, endpointName } = this.mqttTopicParser(topic) ?? {};
|
|
150
|
+
if (!deviceId || !subTopic || !endpointName) {
|
|
151
|
+
this.log.warn(`Skipping MQTT message with topic '${topic}' during onConfigure because it does not match expected format.`);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const device = this.getDeviceById(deviceId);
|
|
155
|
+
if (!device) {
|
|
156
|
+
this.log.warn(`Skipping MQTT message with topic '${topic}' during onConfigure because device with ID '${deviceId}' is not registered.`);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (endpointName === 'root') {
|
|
160
|
+
const parsedPayload = JSON.parse(payload);
|
|
161
|
+
for (const cluster of Object.keys(parsedPayload)) {
|
|
162
|
+
device.log.debug(`Setting cluster '${cluster}' for device '${deviceId}' with payload: ${debugStringify(parsedPayload[cluster])}`);
|
|
163
|
+
if (!device.hasClusterServer(cluster)) {
|
|
164
|
+
device.log.warn(`Device '${deviceId}' does not have cluster '${cluster}' defined. Skipping cluster configuration for this cluster.`);
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
await device.setCluster(cluster, parsedPayload[cluster]);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
27
171
|
}
|
package/dist/mqtt.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { AnsiLogger, debugStringify, sc, error, debug, warn, info } from 'matterbridge/logger';
|
|
4
|
+
import { inspectError } from 'matterbridge/utils';
|
|
5
|
+
import { connectAsync } from 'mqtt';
|
|
6
|
+
export class MqttService extends EventEmitter {
|
|
7
|
+
config;
|
|
8
|
+
log = new AnsiLogger({
|
|
9
|
+
logName: 'MQTT',
|
|
10
|
+
logNameColor: sc,
|
|
11
|
+
logTimestampFormat: 4,
|
|
12
|
+
logLevel: "debug",
|
|
13
|
+
});
|
|
14
|
+
client;
|
|
15
|
+
constructor(config) {
|
|
16
|
+
super();
|
|
17
|
+
this.config = config;
|
|
18
|
+
}
|
|
19
|
+
buildClientOptions() {
|
|
20
|
+
const options = {
|
|
21
|
+
port: this.config.port,
|
|
22
|
+
protocolVersion: this.config.protocolVersion,
|
|
23
|
+
rejectUnauthorized: this.config.rejectUnauthorized,
|
|
24
|
+
};
|
|
25
|
+
if (this.config.clientId)
|
|
26
|
+
options.clientId = this.config.clientId;
|
|
27
|
+
if (this.config.username)
|
|
28
|
+
options.username = this.config.username;
|
|
29
|
+
if (this.config.password)
|
|
30
|
+
options.password = this.config.password;
|
|
31
|
+
if (this.config.ca) {
|
|
32
|
+
try {
|
|
33
|
+
options.ca = readFileSync(this.config.ca);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
throw new Error(`MqttService: failed to read CA certificate '${this.config.ca}': ${error?.toString()}`, { cause: error });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (this.config.cert) {
|
|
40
|
+
try {
|
|
41
|
+
options.cert = readFileSync(this.config.cert);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
throw new Error(`MqttService: failed to read client certificate '${this.config.cert}': ${error?.toString()}`, { cause: error });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (this.config.key) {
|
|
48
|
+
try {
|
|
49
|
+
options.key = readFileSync(this.config.key);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
throw new Error(`MqttService: failed to read client private key '${this.config.key}': ${error?.toString()}`, { cause: error });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return options;
|
|
56
|
+
}
|
|
57
|
+
async connect() {
|
|
58
|
+
if (this.client?.connected) {
|
|
59
|
+
this.log.debug('MqttService: already connected, ignoring connect()');
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
const options = this.buildClientOptions();
|
|
63
|
+
const brokerUrl = `${this.config.host}:${this.config.port}`;
|
|
64
|
+
this.log.debug(`MqttService: connecting to ${debug.bold.success `${brokerUrl}`} with options: ${debugStringify({ ...options, password: options.password ? '****' : undefined, ca: options.ca ? '****' : undefined, cert: options.cert ? '****' : undefined, key: options.key ? '****' : undefined })}`);
|
|
65
|
+
let client;
|
|
66
|
+
try {
|
|
67
|
+
client = await connectAsync(this.config.host, options);
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
inspectError(this.log, `MqttService: failed to connect to ${error.bold.success `${brokerUrl}`}`, err);
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
this.client = client;
|
|
74
|
+
this.log.info(`MqttService: connected to ${info.bold.success `${brokerUrl}`}`);
|
|
75
|
+
this.emit('connect');
|
|
76
|
+
client.on('connect', () => {
|
|
77
|
+
this.log.info(`MqttService: connected to ${info.bold.success `${brokerUrl}`}`);
|
|
78
|
+
this.emit('connect');
|
|
79
|
+
});
|
|
80
|
+
client.on('close', () => {
|
|
81
|
+
this.log.info(`MqttService: connection to ${info.bold.success `${brokerUrl}`} closed`);
|
|
82
|
+
this.emit('close');
|
|
83
|
+
});
|
|
84
|
+
client.on('reconnect', () => {
|
|
85
|
+
this.log.debug(`MqttService: reconnecting to ${debug.bold.success `${brokerUrl}`}`);
|
|
86
|
+
this.emit('reconnect');
|
|
87
|
+
});
|
|
88
|
+
client.on('error', (err) => {
|
|
89
|
+
inspectError(this.log, `MqttService: error on ${error.bold.success `${brokerUrl}`}`, err);
|
|
90
|
+
this.emit('error', err);
|
|
91
|
+
});
|
|
92
|
+
client.on('message', (topic, payload) => {
|
|
93
|
+
this.log.debug(`MqttService: message on ${debug.bold.success `${topic}`}: "${payload.toString().replaceAll('\n', '')}"`);
|
|
94
|
+
this.emit('message', topic, payload.toString());
|
|
95
|
+
});
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
async close() {
|
|
99
|
+
if (!this.client) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
await this.client.endAsync();
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
inspectError(this.log, `MqttService: error closing connection to ${error.bold.success `${this.config.host}:${this.config.port}`}`, err);
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
this.client = undefined;
|
|
110
|
+
this.log.debug('MqttService: client closed');
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
async subscribe(topic) {
|
|
114
|
+
if (!this.client?.connected) {
|
|
115
|
+
this.log.warn(`MqttService: cannot subscribe to '${warn.bold.success `${topic}`}': not connected`);
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
await this.client.subscribeAsync(topic);
|
|
120
|
+
this.log.debug(`MqttService: subscribed to '${debug.bold.success `${topic}`}'`);
|
|
121
|
+
this.emit('subscribed', topic);
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
inspectError(this.log, `MqttService: subscribe error on '${error.bold.success `${topic}`}'`, err);
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async publish(topic, payload, options) {
|
|
130
|
+
if (!this.client?.connected) {
|
|
131
|
+
this.log.warn(`MqttService: cannot publish to '${warn.bold.success `${topic}`}': not connected`);
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
await this.client.publishAsync(topic, payload, options);
|
|
136
|
+
this.log.debug(`MqttService: published to '${debug.bold.success `${topic}`}': ${debug.bold.success `${payload}`}`);
|
|
137
|
+
this.emit('published', topic, payload);
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
inspectError(this.log, `MqttService: publish error on '${error.bold.success `${topic}`}'`, err);
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "matterbridge-mqtt",
|
|
3
|
+
"type": "DynamicPlatform",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"host": "mqtt://localhost",
|
|
6
|
+
"port": 1883,
|
|
7
|
+
"protocolVersion": 5,
|
|
8
|
+
"username": "",
|
|
9
|
+
"password": "",
|
|
10
|
+
"clientId": "",
|
|
11
|
+
"ca": "",
|
|
12
|
+
"rejectUnauthorized": true,
|
|
13
|
+
"cert": "",
|
|
14
|
+
"key": "",
|
|
15
|
+
"topic": "matterbridge",
|
|
16
|
+
"whiteList": [],
|
|
17
|
+
"blackList": [],
|
|
18
|
+
"debug": false,
|
|
19
|
+
"unregisterOnShutdown": false
|
|
20
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
{
|
|
2
|
+
"title": "Matterbridge mqtt plugin",
|
|
3
|
+
"description": "matterbridge-mqtt v. 1.0.0 by https://github.com/Luligu",
|
|
4
|
+
"type": "object",
|
|
5
|
+
"required": ["host", "port", "topic"],
|
|
6
|
+
"properties": {
|
|
7
|
+
"name": {
|
|
8
|
+
"title": "Plugin Name",
|
|
9
|
+
"description": "Plugin name",
|
|
10
|
+
"type": "string",
|
|
11
|
+
"readOnly": true,
|
|
12
|
+
"ui:widget": "hidden"
|
|
13
|
+
},
|
|
14
|
+
"type": {
|
|
15
|
+
"title": "Plugin Type",
|
|
16
|
+
"description": "Plugin type",
|
|
17
|
+
"type": "string",
|
|
18
|
+
"readOnly": true,
|
|
19
|
+
"ui:widget": "hidden"
|
|
20
|
+
},
|
|
21
|
+
"host": {
|
|
22
|
+
"title": "MQTT Host",
|
|
23
|
+
"description": "MQTT server host (IP address or hostname with mqtt:// or mqtts:// or ws:// or wss:// prefix) or mqtt+unix://<SOCKET_PATH> for Unix socket on Linux (e.g. mqtt+unix:///var/run/mosquitto/mqtt.sock). For secure connections, use the mqtts:// or wss:// prefix and ensure your certificates are configured. If you use a hostname, make sure that the hostname is resolvable by the system running matterbridge.",
|
|
24
|
+
"type": "string",
|
|
25
|
+
"default": "mqtt://localhost"
|
|
26
|
+
},
|
|
27
|
+
"port": {
|
|
28
|
+
"title": "MQTT Port",
|
|
29
|
+
"description": "MQTT server port (i.e. 1883 for mqtt://, 8883 for mqtts://, 9001 for ws:// or 9443 for wss://). For Unix socket, this value is ignored.",
|
|
30
|
+
"type": "number",
|
|
31
|
+
"default": 1883
|
|
32
|
+
},
|
|
33
|
+
"protocolVersion": {
|
|
34
|
+
"title": "MQTT Protocol Version",
|
|
35
|
+
"description": "MQTT server protocol version 3, 4 or 5 (default 5). Version 5 is recommended for most modern MQTT brokers.",
|
|
36
|
+
"type": "number",
|
|
37
|
+
"default": 5
|
|
38
|
+
},
|
|
39
|
+
"username": {
|
|
40
|
+
"title": "MQTT Username",
|
|
41
|
+
"description": "MQTT server authentication user. Only required if the MQTT server requires authentication (i.e allow_anonymous false)",
|
|
42
|
+
"type": "string"
|
|
43
|
+
},
|
|
44
|
+
"password": {
|
|
45
|
+
"title": "MQTT Password",
|
|
46
|
+
"description": "MQTT server authentication password. Only required if the MQTT server requires authentication (i.e allow_anonymous false)",
|
|
47
|
+
"type": "string",
|
|
48
|
+
"ui:widget": "password"
|
|
49
|
+
},
|
|
50
|
+
"clientId": {
|
|
51
|
+
"title": "MQTT Client ID",
|
|
52
|
+
"description": "MQTT clientId identifier. If not set, a random clientId will be generated.",
|
|
53
|
+
"type": "string"
|
|
54
|
+
},
|
|
55
|
+
"ca": {
|
|
56
|
+
"title": "MQTT CA Certificate",
|
|
57
|
+
"description": "Absolute path to the SSL/TLS CA certificate used to sign both the server and client certificates (PEM format). Required for secure connections (i.e. mqtts://) with self-signed certificates.",
|
|
58
|
+
"type": "string"
|
|
59
|
+
},
|
|
60
|
+
"rejectUnauthorized": {
|
|
61
|
+
"title": "MQTT Reject Unauthorized",
|
|
62
|
+
"description": "Reject unauthorized MQTT server. Only used for secure connections (i.e. mqtts://).",
|
|
63
|
+
"type": "boolean",
|
|
64
|
+
"default": true
|
|
65
|
+
},
|
|
66
|
+
"cert": {
|
|
67
|
+
"title": "MQTT Client Certificate",
|
|
68
|
+
"description": "Absolute path to the SSL/TLS MQTT client certificate (PEM format). Only required if the MQTT server requires a client certificate for authentication (i.e. require_certificate true).",
|
|
69
|
+
"type": "string"
|
|
70
|
+
},
|
|
71
|
+
"key": {
|
|
72
|
+
"title": "MQTT Client Private Key",
|
|
73
|
+
"description": "Absolute path to the SSL/TLS MQTT client private key (PEM format). Only required if the MQTT server requires a client certificate for authentication (i.e. require_certificate true).",
|
|
74
|
+
"type": "string"
|
|
75
|
+
},
|
|
76
|
+
"topic": {
|
|
77
|
+
"title": "MQTT Topic",
|
|
78
|
+
"description": "MQTT base topic for MQTT messages.",
|
|
79
|
+
"type": "string",
|
|
80
|
+
"default": "matterbridge"
|
|
81
|
+
},
|
|
82
|
+
"whiteList": {
|
|
83
|
+
"title": "White List",
|
|
84
|
+
"description": "Only the devices in the list will be exposed. If the list is empty, all the devices will be exposed.",
|
|
85
|
+
"type": "array",
|
|
86
|
+
"items": {
|
|
87
|
+
"type": "string"
|
|
88
|
+
},
|
|
89
|
+
"uniqueItems": true,
|
|
90
|
+
"selectFrom": "name"
|
|
91
|
+
},
|
|
92
|
+
"blackList": {
|
|
93
|
+
"title": "Black List",
|
|
94
|
+
"description": "The devices in the list will not be exposed. If the list is empty, no devices will be excluded.",
|
|
95
|
+
"type": "array",
|
|
96
|
+
"items": {
|
|
97
|
+
"type": "string"
|
|
98
|
+
},
|
|
99
|
+
"uniqueItems": true,
|
|
100
|
+
"selectFrom": "name"
|
|
101
|
+
},
|
|
102
|
+
"debug": {
|
|
103
|
+
"title": "Enable Debug",
|
|
104
|
+
"description": "Enable the debug for the plugin (development only)",
|
|
105
|
+
"type": "boolean",
|
|
106
|
+
"default": false
|
|
107
|
+
},
|
|
108
|
+
"unregisterOnShutdown": {
|
|
109
|
+
"title": "Unregister On Shutdown",
|
|
110
|
+
"description": "Unregister all devices on shutdown (development only)",
|
|
111
|
+
"type": "boolean",
|
|
112
|
+
"default": false
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "matterbridge-mqtt",
|
|
3
|
+
"type": "DynamicPlatform",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"host": "mqtt://localhost",
|
|
6
|
+
"port": 1883,
|
|
7
|
+
"protocolVersion": 5,
|
|
8
|
+
"username": "",
|
|
9
|
+
"password": "",
|
|
10
|
+
"clientId": "",
|
|
11
|
+
"ca": "",
|
|
12
|
+
"rejectUnauthorized": true,
|
|
13
|
+
"cert": "",
|
|
14
|
+
"key": "",
|
|
15
|
+
"topic": "matterbridge",
|
|
16
|
+
"whiteList": [],
|
|
17
|
+
"blackList": [],
|
|
18
|
+
"debug": false,
|
|
19
|
+
"unregisterOnShutdown": false
|
|
20
|
+
}
|