Skip to content

Commit

Permalink
Merge pull request #56 from L2jLiga/feature/hostname-configuration
Browse files Browse the repository at this point in the history
feat: added option to specify unique postfix
  • Loading branch information
Luligu authored Aug 28, 2024
2 parents 4917f8b + fa1e908 commit 53697e9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ The unregisterOnShutdown option allows to remove from the bridge all z2m devices

These are the default vules:

```
```json
{
"name": "matterbridge-zigbee2mqtt",
"type": "DynamicPlatform",
Expand All @@ -137,13 +137,14 @@ These are the default vules:
"lightList": [],
"outletList": [],
"featureBlackList": [],
"deviceFeatureBlackList": {}
"deviceFeatureBlackList": {},
"postfixHostname": true
}
```

If you want to exclude "device_temperature" for all the devices, add to the config

```
```json
{
...
"featureBlackList": ["device_temperature"]
Expand All @@ -154,7 +155,7 @@ If you want to exclude "device_temperature" for all the devices, add to the conf
If you want to exclude "temperature" and "humidity" for the device "My motion sensor" and
"device_temperature" only for the device "My climate sensor", add to the config

```
```json
{
...
"deviceFeatureBlackList": {
Expand All @@ -165,6 +166,18 @@ If you want to exclude "temperature" and "humidity" for the device "My motion se
}
```

By default matterbridge uses hostname in order to make entities unique, however in some cases
you may not want this behavior, for example when deploying matterbridge in kubernets cluster.
You can use "postfixHostname" boolean flag to disable this behavior:

```json
{
...
"postfixHostname": false
...
}
```

From the release 1.2.14 of Matterbridge you can edit the config file directly in the frontend.

You can edit the config file manually if you prefer:
Expand Down
7 changes: 6 additions & 1 deletion matterbridge-zigbee2mqtt.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@
"description": "Unregister all devices on shutdown (development only)",
"type": "boolean",
"default": false
},
"postfixHostname": {
"description": "Unique postfix added to each device identifier to avoid collision with other automation system",
"type": "boolean",
"default": true
}
}
}
}
20 changes: 16 additions & 4 deletions src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { Payload, PayloadValue } from './payloadTypes.js';

export class ZigbeeEntity extends EventEmitter {
public log: AnsiLogger;
public serial = '';
protected platform: ZigbeePlatform;
public device: BridgeDevice | undefined;
public group: BridgeGroup | undefined;
Expand Down Expand Up @@ -373,6 +374,12 @@ export class ZigbeeGroup extends ZigbeeEntity {
constructor(platform: ZigbeePlatform, group: BridgeGroup) {
super(platform, group);

if (this.platform.postfixHostname) {
this.serial = `group-${group.id}_${hostname}`.slice(-1, 32);
} else {
this.serial = `group-${group.id}`.slice(-1, 32);
}

// TODO Add the group scanning for real groups. This cover only automations
let useState = false;
let useBrightness = false;
Expand Down Expand Up @@ -549,6 +556,11 @@ export class ZigbeeDevice extends ZigbeeEntity {
constructor(platform: ZigbeePlatform, device: BridgeDevice) {
super(platform, device);

this.serial = `${device.ieee_address}`;
if (this.platform.postfixHostname) {
this.serial = `${this.serial}_${hostname}`.slice(-1, 32);
}

if (device.friendly_name === 'Coordinator' || (device.model_id === 'ti.router' && device.manufacturer === 'TexasInstruments') || (device.model_id.startsWith('SLZB-') && device.manufacturer === 'SMLIGHT')) {
this.bridgedDevice = new BridgedBaseDevice(this, [DeviceTypes.DOOR_LOCK], [Identify.Cluster.id, DoorLock.Cluster.id]);
this.bridgedDevice.addFixedLabel('type', 'lock');
Expand Down Expand Up @@ -930,11 +942,11 @@ export class BridgedBaseDevice extends MatterbridgeDevice {

// Add BridgedDeviceBasicInformation cluster
if (entity.isDevice && entity.device && entity.device.friendly_name === 'Coordinator') {
this.addBridgedDeviceBasicInformationCluster(entity.device.friendly_name, 'zigbee2MQTT', 'Coordinator', entity.device.ieee_address);
this.addBridgedDeviceBasicInformationCluster(entity.device.friendly_name, 'zigbee2MQTT', 'Coordinator', entity.serial);
} else if (entity.isDevice && entity.device) {
this.addBridgedDeviceBasicInformationCluster(entity.device.friendly_name, entity.device.definition ? entity.device.definition.vendor : entity.device.manufacturer, entity.device.definition ? entity.device.definition.model : entity.device.model_id, entity.device.ieee_address);
this.addBridgedDeviceBasicInformationCluster(entity.device.friendly_name, entity.device.definition ? entity.device.definition.vendor : entity.device.manufacturer, entity.device.definition ? entity.device.definition.model : entity.device.model_id, entity.serial);
} else if (entity.isGroup && entity.group) {
this.addBridgedDeviceBasicInformationCluster(entity.group.friendly_name, 'zigbee2MQTT', 'Group', `group-${entity.group.id}`);
this.addBridgedDeviceBasicInformationCluster(entity.group.friendly_name, 'zigbee2MQTT', 'Group', entity.serial);
}

// Add BridgedDevice device type
Expand All @@ -961,7 +973,7 @@ export class BridgedBaseDevice extends MatterbridgeDevice {
* @param deviceSerial Serial of the device
*/
protected addBridgedDeviceBasicInformationCluster(deviceName: string, vendorName: string, productName: string, deviceSerial: string) {
this.createDefaultBridgedDeviceBasicInformationClusterServer(deviceName.slice(0, 32), (deviceSerial + '_' + hostname).slice(0, 32), 0xfff1, vendorName.slice(0, 32), productName.slice(0, 32));
this.createDefaultBridgedDeviceBasicInformationClusterServer(deviceName.slice(0, 32), deviceSerial, 0xfff1, vendorName.slice(0, 32), productName.slice(0, 32));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class ZigbeePlatform extends MatterbridgeDynamicPlatform {
public switchList: string[] = [];
public featureBlackList: string[] = [];
public deviceFeatureBlackList: DeviceFeatureBlackList = {};
public postfixHostname = true;

// zigbee2Mqtt
public debugEnabled: boolean;
Expand Down Expand Up @@ -93,6 +94,8 @@ export class ZigbeePlatform extends MatterbridgeDynamicPlatform {
if (config.outletList) this.outletList = config.outletList as string[];
if (config.featureBlackList) this.featureBlackList = config.featureBlackList as string[];
if (config.deviceFeatureBlackList) this.deviceFeatureBlackList = config.deviceFeatureBlackList as DeviceFeatureBlackList;
this.postfixHostname = (config.postfixHostname as boolean) ?? true;

// Save back to create a default plugin config.json
config.host = this.mqttHost;
config.port = this.mqttPort;
Expand All @@ -102,6 +105,7 @@ export class ZigbeePlatform extends MatterbridgeDynamicPlatform {
config.password = this.mqttPassword;
config.whiteList = this.whiteList;
config.blackList = this.blackList;
config.postfixHostname = this.postfixHostname;

if (config.type === 'MatterbridgeExtension') {
this.z2m = new Zigbee2MQTT(this.mqttHost, this.mqttPort, this.mqttTopic, this.mqttUsername, this.mqttPassword, this.mqttProtocol, this.debugEnabled);
Expand Down

0 comments on commit 53697e9

Please sign in to comment.