Skip to content

Commit

Permalink
fix(hass): discovery issues caused by spaces in topic (#99)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Hass entities ids will change
Co-authored-by: V Aretakis <[email protected]>
  • Loading branch information
robertsLando authored Dec 21, 2020
1 parent 1b5c880 commit 528a51c
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}`

Expand Down Expand Up @@ -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}`

Expand Down

0 comments on commit 528a51c

Please sign in to comment.