Skip to content

Commit

Permalink
fix!: Remove configuring group members through configuration.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Oct 14, 2024
1 parent f34f836 commit 5a699dd
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 79 deletions.
73 changes: 0 additions & 73 deletions lib/extension/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,79 +45,6 @@ export default class Groups extends Extension {
override async start(): Promise<void> {
this.eventBus.onStateChange(this, this.onStateChange);
this.eventBus.onMQTTMessage(this, this.onMQTTMessage);
await this.syncGroupsWithSettings();
}

private async syncGroupsWithSettings(): Promise<void> {
const settingsGroups = settings.getGroups();

const addRemoveFromGroup = async (
action: 'add' | 'remove',
deviceName: string,
groupName: string | number,
endpoint: zh.Endpoint,
group: Group,
): Promise<void> => {
try {
logger.info(`${action === 'add' ? 'Adding' : 'Removing'} '${deviceName}' to group '${groupName}'`);

if (action === 'remove') {
await endpoint.removeFromGroup(group.zh);
} else {
await endpoint.addToGroup(group.zh);
}
} catch (error) {
logger.error(`Failed to ${action} '${deviceName}' from '${groupName}'`);
logger.debug((error as Error).stack!);
}
};

for (const settingGroup of settingsGroups) {
const groupID = settingGroup.ID;
const zigbeeGroup = this.zigbee.groupsIterator((g) => g.groupID === groupID).next().value || this.zigbee.createGroup(groupID);
const settingsEndpoints: zh.Endpoint[] = [];

for (const d of settingGroup.devices) {
const parsed = this.zigbee.resolveEntityAndEndpoint(d);
const device = parsed.entity as Device;

if (!device) {
logger.error(`Cannot find '${d}' of group '${settingGroup.friendly_name}'`);
}

if (!parsed.endpoint) {
if (parsed.endpointID) {
logger.error(`Cannot find endpoint '${parsed.endpointID}' of device '${parsed.ID}'`);
}

continue;
}

// In settings but not in zigbee
if (!zigbeeGroup.zh.hasMember(parsed.endpoint)) {
await addRemoveFromGroup('add', device?.name, settingGroup.friendly_name, parsed.endpoint, zigbeeGroup);
}

settingsEndpoints.push(parsed.endpoint);
}

// In zigbee but not in settings
for (const endpoint of zigbeeGroup.zh.members) {
if (!settingsEndpoints.includes(endpoint)) {
const deviceName = settings.getDevice(endpoint.getDevice().ieeeAddr)!.friendly_name;

await addRemoveFromGroup('remove', deviceName, settingGroup.friendly_name, endpoint, zigbeeGroup);
}
}
}

for (const zigbeeGroup of this.zigbee.groupsIterator((zg) => !settingsGroups.some((sg) => sg.ID === zg.groupID))) {
for (const endpoint of zigbeeGroup.zh.members) {
const deviceName = settings.getDevice(endpoint.getDevice().ieeeAddr)!.friendly_name;

await addRemoveFromGroup('remove', deviceName, zigbeeGroup.ID, endpoint, zigbeeGroup);
}
}
}

@bind async onStateChange(data: eventdata.StateChange): Promise<void> {
Expand Down
6 changes: 0 additions & 6 deletions lib/util/settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -909,12 +909,6 @@
"retain": {
"type": "boolean"
},
"devices": {
"type": "array",
"items": {
"type": "string"
}
},
"optimistic": {
"type": "boolean"
},
Expand Down

0 comments on commit 5a699dd

Please sign in to comment.