mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-07-02 18:11:36 +00:00
d83085ea7f
* Update zigbee-herdsman and zigbee-shepherd-converters. * Force Aqara S2 Lock endvices (#1764) * Start on zigbee-herdsman controller refactor. * More updates. * Cleanup zapp. * updates. * Propagate adapter disconnected event. * Updates. * Initial refactor to zigbee-herdsman. * Refactor deviceReceive to zigbee-herdsman. * Rename * Refactor deviceConfigure. * Finish bridge config. * Refactor availability. * Active homeassistant extension and more refactors. * Refactor groups. * Enable soft reset. * Activate group membership * Start on tests. * Enable reporting. * Add more controller tests. * Add more tests * Fix linting error. * Data en deviceReceive tests. * Move to zigbee-herdsman-converters. * More device publish tests. * Cleanup dependencies. * Bring device publish coverage to 100. * Bring home assistant test coverage to 100. * Device configure tests. * Attempt to fix tests. * Another attempt. * Another one. * Another one. * Another. * Add wait. * Longer wait. * Debug. * Update dependencies. * Another. * Begin on availability tests. * Improve availability tests. * Complete deviceAvailability tests. * Device bind tests. * More tests. * Begin networkmap refactors. * start on networkmap tests. * Network map tests. * Add utils tests. * Logger tests. * Settings and logger tests. * Ignore some stuff for coverage and add todos. * Add remaining missing tests. * Enforce 100% test coverage. * Start on groups test and refactor entityPublish to resolveEntity * Remove joinPathStorage, not used anymore as group information is stored into zigbee-herdsman database. * Fix linting issues. * Improve tests. * Add groups. * fix group membership. * Group: log names. * Convert MQTT message to string by default. * Fix group name. * Updates. * Revert configuration.yaml. * Add new line. * Fixes. * Updates. * Fix tests. * Ignore soft reset extension.
130 lines
5.3 KiB
JavaScript
130 lines
5.3 KiB
JavaScript
const settings = require('../util/settings');
|
|
const logger = require('../util/logger');
|
|
|
|
const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/group/(.+)/(remove|add|remove_all)$`);
|
|
const topicRegexRemoveAll = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/group/remove_all$`);
|
|
|
|
class Groups {
|
|
constructor(zigbee, mqtt, state, publishEntityState) {
|
|
this.zigbee = zigbee;
|
|
this.mqtt = mqtt;
|
|
this.state = state;
|
|
this.publishEntityState = publishEntityState;
|
|
}
|
|
|
|
onMQTTConnected() {
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/group/+/remove`);
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/group/+/add`);
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/group/+/remove_all`);
|
|
this.mqtt.subscribe(`${settings.get().mqtt.base_topic}/bridge/group/remove_all`);
|
|
}
|
|
|
|
async onZigbeeStarted() {
|
|
await this.syncGroupsWithSettings();
|
|
}
|
|
|
|
async syncGroupsWithSettings() {
|
|
const settingsGroups = settings.getGroups();
|
|
const zigbeeGroups = await this.zigbee.getGroups({});
|
|
for (const settingGroup of settingsGroups) {
|
|
const groupID = settingGroup.ID;
|
|
const zigbeeGroup = zigbeeGroups.find((g) => g.groupID === groupID) ||
|
|
(await this.zigbee.createGroup(groupID));
|
|
const settingsEntity = (await Promise.all(settingGroup.devices.map(async (d) => {
|
|
const entity = await this.zigbee.resolveEntity(d);
|
|
if (!entity) logger.error(`Cannot find '${d}' of group '${settingGroup.friendlyName}'`);
|
|
return entity;
|
|
}))).filter((e) => e != null);
|
|
|
|
// In settings but not in zigbee
|
|
for (const entity of settingsEntity) {
|
|
if (!zigbeeGroup.hasMember(entity.endpoint)) {
|
|
logger.info(`Adding '${entity.name}' to group '${settingGroup.friendlyName}'`);
|
|
await entity.endpoint.addToGroup(zigbeeGroup);
|
|
}
|
|
}
|
|
|
|
// In zigbee but not in settings
|
|
for (const endpoint of zigbeeGroup.getMembers()) {
|
|
if (!settingsEntity.find((e) => e.endpoint === endpoint)) {
|
|
const deviceSettings = settings.getDevice(endpoint.deviceIeeeAddress);
|
|
logger.info(`Removing '${deviceSettings.friendlyName}' from group '${settingGroup.friendlyName}'`);
|
|
await endpoint.removeFromGroup(zigbeeGroup);
|
|
}
|
|
}
|
|
}
|
|
|
|
// eslint-disable-next-line
|
|
for (const zigbeeGroup of zigbeeGroups) {
|
|
if (!settingsGroups.find((g) => g.ID === zigbeeGroup.groupID)) {
|
|
for (const endpoint of zigbeeGroup.getMembers()) {
|
|
const deviceSettings = settings.getDevice(endpoint.deviceIeeeAddress);
|
|
logger.info(`Removing '${deviceSettings.friendlyName}' from group ${zigbeeGroup.groupID}`);
|
|
await endpoint.removeFromGroup(zigbeeGroup);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async onMQTTMessage(topic, message) {
|
|
let type;
|
|
let group;
|
|
const topicMatch = topic.match(topicRegex);
|
|
if (topicMatch) {
|
|
group = await this.zigbee.resolveEntity(topicMatch[1]);
|
|
type = topicMatch[2];
|
|
|
|
if (!group || group.type !== 'group') {
|
|
logger.error(`Group '${topicMatch[1]}' does not exist`);
|
|
return;
|
|
}
|
|
} else if (topic.match(topicRegexRemoveAll)) {
|
|
type = 'remove_all';
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
const entity = await this.zigbee.resolveEntity(message);
|
|
if (!entity || !entity.type === 'device') {
|
|
logger.error(`Device '${message}' does not exist`);
|
|
return;
|
|
}
|
|
|
|
const keys = [
|
|
`${entity.device.ieeeAddr}/${entity.endpoint.ID}`,
|
|
`${entity.name}/${entity.endpoint.ID}`,
|
|
];
|
|
|
|
if (entity.endpointName) {
|
|
keys.push(`${entity.device.ieeeAddr}/${entity.endpointName}`);
|
|
keys.push(`${entity.name}/${entity.endpointName}`);
|
|
}
|
|
|
|
if (entity.isDefaultEndpoint) {
|
|
keys.push(entity.name);
|
|
keys.push(entity.device.ieeeAddr);
|
|
}
|
|
|
|
if (type === 'add') {
|
|
logger.info(`Adding '${entity.name}' to '${group.name}'`);
|
|
await entity.endpoint.addToGroup(group.group);
|
|
settings.addDeviceToGroup(group.settings.ID, keys);
|
|
this.mqtt.log({type: 'add', message: {device: entity.name, group: group.name}});
|
|
} else if (type === 'remove') {
|
|
logger.info(`Removing '${entity.name}' from '${group.name}'`);
|
|
await entity.endpoint.removeFromGroup(group.group);
|
|
settings.removeDeviceFromGroup(group.settings.ID, keys);
|
|
this.mqtt.log({type: 'remove', message: {device: entity.name, group: group.name}});
|
|
} else { // remove_all
|
|
logger.info(`Removing '${entity.name}' from all groups`);
|
|
await entity.endpoint.removeFromAllGroups();
|
|
for (const settingsGroup of settings.getGroups()) {
|
|
settings.removeDeviceFromGroup(settingsGroup.ID, keys);
|
|
this.mqtt.log({type: 'remove_all', message: {device: entity.name}});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = Groups;
|