diff --git a/lib/Gateway.js b/lib/Gateway.js index 28495fffd5a..6d831352617 100755 --- a/lib/Gateway.js +++ b/lib/Gateway.js @@ -568,6 +568,16 @@ function deviceInfo (node, nodeName) { function getDiscoveryTopic (hassDevice, nodeName) { return `${hassDevice.type}/${nodeName}/${hassDevice.object_id}/config` } +/** + * Sanitize ids removing chars that could break discovery + * + * @param {String} id The id string + * @returns The sanitized id, lower cases and without spaces + */ +function sanitizeId (id) { + return id.replace(/\s/g, '_').toLocaleLowerCase() +} + /** * Calculate the correct template string to use for modes templates * based on gateway settings and mapped mode values @@ -1117,6 +1127,8 @@ Gateway.prototype.discoverDevice = function (node, hassDevice) { // Set device information using node info payload.device = deviceInfo.call(this, node, nodeName) + hassDevice.object_id = sanitizeId(hassDevice.object_id) + // Set a friendly name for this component payload.name = `${nodeName}_${hassDevice.object_id}` @@ -1608,6 +1620,8 @@ Gateway.prototype.discoverValue = function (node, vId) { cfg.object_id += '_' + valueId.endpoint } + cfg.object_id = sanitizeId(cfg.object_id) + // Set a friendly name for this component payload.name = `${nodeName}_${cfg.object_id}`