From 255e9b3327d4c350adc61ad63adc944c95e011dc Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Tue, 9 Mar 2021 13:38:46 +0100 Subject: [PATCH] feat(hass): added `manual discovery` setting (#851) * feat(hass): added `manual discovery` setting With this setting discovery payload will not be sent on startup but users will be able to send them manually from the UI. Useful when using mqtt discovery paired with zwavejs integration to discovery only some entities. Fixes #819 * docs: manual discovery * fix: undefined error and added tooltips on ui * fix: lint issues --- app.js | 10 +- docs/usage/setup.md | 1 + lib/Gateway.js | 51 +++--- src/components/Settings.vue | 10 +- src/components/nodes-table/HomeAssistant.vue | 159 ++++++++++++++----- types/index.d.ts | 3 +- 6 files changed, 160 insertions(+), 74 deletions(-) diff --git a/app.js b/app.js index 498b6fdd41a..058f178a8e8 100644 --- a/app.js +++ b/app.js @@ -421,10 +421,16 @@ function setupSocket (server) { try { switch (data.apiName) { case 'delete': - res = gw.publishDiscovery(data.device, data.nodeId, true, true) + res = gw.publishDiscovery(data.device, data.nodeId, { + deleteDevice: true, + forceUpdate: true + }) break case 'discover': - res = gw.publishDiscovery(data.device, data.nodeId, false, true) + res = gw.publishDiscovery(data.device, data.nodeId, { + deleteDevice: false, + forceUpdate: true + }) break case 'rediscoverNode': res = gw.rediscoverNode(data.nodeId) diff --git a/docs/usage/setup.md b/docs/usage/setup.md index b29c12db33e..0569453c678 100644 --- a/docs/usage/setup.md +++ b/docs/usage/setup.md @@ -198,6 +198,7 @@ Enable this to use Z2M only as a Control Panel - **MQTT discovery**: Enable this to use MQTT discovery. This is an alternative to Hass Zwave-js integration. (more about this [here](/guide/homeassistant)) - **Discovery Prefix**: The prefix to use to send MQTT discovery messages to HASS - **Retain Discovery**: Set retain flag to true in discovery messages +- **Manual Discovery**: Don't automatically send the discovery payloads when a device is discovered - **Entity name template**: Custom Entity name based on placeholders. Default is `%ln_%o` - `%ln`: Node location with name `` - `%n`: Node Name diff --git a/lib/Gateway.js b/lib/Gateway.js index 3c4d8959ff5..063f659827a 100755 --- a/lib/Gateway.js +++ b/lib/Gateway.js @@ -1175,52 +1175,53 @@ Gateway.prototype.disableDiscovery = function (nodeID) { * * @param {HassDevice} hassDevice The hass device configuration to use for the discovery * @param {number} nodeId The node id - * @param {boolean} deleteDevice Enable this to remove the selected device from hass discovery - * @param {boolean} update Update an hass device of a specific node in zwaveClient and send the event to socket + * @param {{deleteDevice: boolean, forceUpdate: boolean}} options options */ Gateway.prototype.publishDiscovery = function ( hassDevice, nodeId, - deleteDevice, - update + options = {} ) { try { logger.log( 'debug', - `${deleteDevice ? 'Removing' : 'Publishing'} discovery: %o`, + `${options.deleteDevice ? 'Removing' : 'Publishing'} discovery: %o`, hassDevice ) - this.setDiscovery(nodeId, hassDevice, deleteDevice) - - // don't discovery this device when ignore is true - if (!hassDevice.ignoreDiscovery) { - if (this.config.payloadType === 2) { - // Payload is set to "Just Value" - const p = hassDevice.discovery_payload - const template = - 'value' + - (p.hasOwnProperty('payload_on') && p.hasOwnProperty('payload_off') - ? " == 'true'" - : '') - - for (const k in p) { - if (typeof p[k] === 'string') { - p[k] = p[k].replace(/value_json\.value/g, template) - } + this.setDiscovery(nodeId, hassDevice, options.deleteDevice) + + if (this.config.payloadType === 2) { + // Payload is set to "Just Value" + const p = hassDevice.discovery_payload + const template = + 'value' + + (p.hasOwnProperty('payload_on') && p.hasOwnProperty('payload_off') + ? " == 'true'" + : '') + + for (const k in p) { + if (typeof p[k] === 'string') { + p[k] = p[k].replace(/value_json\.value/g, template) } } + } + + const skipDiscovery = + hassDevice.ignoreDiscovery || + (this.config.manualDiscovery && !options.forceUpdate) + if (!skipDiscovery) { this.mqtt.publish( hassDevice.discoveryTopic, - deleteDevice ? '' : hassDevice.discovery_payload, + options.deleteDevice ? '' : hassDevice.discovery_payload, { qos: 0, retain: this.config.retainedDiscovery || false }, this.config.discoveryPrefix ) } - if (update) { - this.zwave.updateDevice(hassDevice, nodeId, deleteDevice) + if (options.forceUpdate) { + this.zwave.updateDevice(hassDevice, nodeId, options.deleteDevice) } } catch (error) { logger.log( diff --git a/src/components/Settings.vue b/src/components/Settings.vue index c555c435d1f..05bc2dd7f2b 100644 --- a/src/components/Settings.vue +++ b/src/components/Settings.vue @@ -501,7 +501,7 @@ hint="The prefix to use for Hass MQTT discovery. Leave empty to use the mqtt prefix" > - + + + + - Store - Remove Store - Rediscover Node - Disable Discovery + + + Store all discovered devices in nodes.json in store directory. + Prevents re-discovery on startup + + + + + Remove devices from nodes.json in store directory + + + + + Rediscover all node entities. Useful when changing node + name/location and need to recalculate topics + + + + + Set the ignoreDiscovery flag to true on all entities of this node + to skip the discovery of them + - Add - Update - Rediscover - Delete + + + Add this device to discovered entities + + + + + Update the in-memory discover template. You have to press + Rediscover in order to send this to HA + + + + + Send this payload to HA + + + + + Delete this entity + +