Skip to content

Commit

Permalink
feat(hass): entities name template configuration (#100)
Browse files Browse the repository at this point in the history
* [WIP] [feat] Add ability to post more friendly names in HASS

Hass names always have hyphens and underscored. Allow users to choose
between the default and more friendly ones, which replaces symbols with
space.

* add more options for Friendly name

* fix also payload during device discovery

* add fibaro motion sensor

* Remove motion sensor, hass devices

* fix parameter on function

* change the approach of defining this value (remove repetitions)

* Update src/components/Settings.vue

Co-authored-by: Daniel Lando <[email protected]>

* Update src/components/Settings.vue

Co-authored-by: Daniel Lando <[email protected]>

* Update src/components/Settings.vue

Co-authored-by: Daniel Lando <[email protected]>

* add Default value

* modify list of options

* Modify list of options (once more)

* add an extra option

* fix missing comma on Vue

* replace property key or name with label! as it resolves everything we look for

* add underscore on default option

* change customDevices payload.name, as has no valueId exposed

* move valueId last makes the implementation clear. Also fix the line 552 in Gateway, which was by accident quoted

* change id to labelId, as conflict with id

* add new method for entity name

* first changes to use entityTemplate

* fix conflicts and make function cover all cases

* add tooltip, even though is not working! just prepare work

* add tooltip

* fix linting issues

* fix: template hint

* fix: lint issues

* change the commenf of function

* fix comments of function

* Update lib/Gateway.js

* add autogen node name and also cover empty name

* remove autogen name

* add NodeID and rename NoneName placeholder

* shorten placeholders

* fix: fallback to node props in entity name

* fix: use device class specific

* revert changes and use new cfg.type and cfg.object_id values for customDevices

* apply forgoten changes on Gateway

* update UI comments

* remove nodeName value from function getEntityName

* add documentation

* lint-fix

Co-authored-by: V. Aretakis <[email protected]>
Co-authored-by: Daniel Lando <[email protected]>
  • Loading branch information
3 people authored Dec 30, 2020
1 parent 6408d32 commit 3a63e2e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,13 @@ Gateway settings:
- **Use nodes name instead of numeric nodeIDs**: When gateway type is `ValueId` use this flag to force to use node names instead of node ids in topic.
- :star:**Hass discovery**:star:: Enable this to automatically create entities on Hass using MQTT autodiscovery (more about this [here](#robot-home-assistant-integration-beta))
- **Discovery Prefix**: The prefix to use to send MQTT discovery messages to HASS
- **Entity name template**: Custom Entity name based on placeholders. Default is `%loc-%n_%o`
- `%n`: Node Name
- `%loc`: Node Location
- `%pk`: valueId property key (fallback to device type)
- `%pn`: valueId property name (fallback to device type)
- `%o`: HASS object_id
- `%l`: valueId label (fallback to object_id)
Once finished press `SAVE` and gateway will start Zwave Network Scan, than go to 'Control Panel' section and wait until the scan is completed to check discovered devices and manage them.
Expand Down
39 changes: 37 additions & 2 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,36 @@ function setDiscoveryValue (payload, prop, node) {
}
}

/**
*
* @param {any} node node object
* @param {any} valueId valueId object
* @param {any} cfg configuration object
* @param {string} entityTemplate the entity template from configuration
*/
function getEntityName (node, valueId, cfg, entityTemplate) {
entityTemplate = entityTemplate || '%loc-%n_%o'
// when getting the entity name of a device use node props
let propertyKey = cfg.type
let propertyName = cfg.type
let label = cfg.object_id

if (valueId) {
propertyKey = valueId.propertyKey || valueId.propertyName
propertyName = valueId.propertyName
label = valueId.label
}

return entityTemplate
.replace(/%nid/g, NODE_PREFIX + node.id)
.replace(/%loc/g, node.loc || '')
.replace(/%pk/g, propertyKey)
.replace(/%pn/g, propertyName)
.replace(/%o/g, cfg.object_id)
.replace(/%n/g, node.name || NODE_PREFIX + node.id)
.replace(/%l/g, label)
}

/**
* Parse the value of the payload received from mqtt
* based on the type of the payload and the gateway config
Expand Down Expand Up @@ -1155,7 +1185,12 @@ Gateway.prototype.discoverDevice = function (node, hassDevice) {
hassDevice.object_id = sanitizeId(hassDevice.object_id)

// Set a friendly name for this component
payload.name = `${nodeName}_${hassDevice.object_id}`
payload.name = getEntityName(
node,
undefined,
hassDevice,
this.config.entityTemplate
)

// set a unique id for the component
payload.unique_id =
Expand Down Expand Up @@ -1677,7 +1712,7 @@ Gateway.prototype.discoverValue = function (node, vId) {
cfg.object_id = sanitizeId(cfg.object_id)

// Set a friendly name for this component
payload.name = `${nodeName}_${cfg.object_id}`
payload.name = getEntityName(node, valueId, cfg, this.config.entityTemplate)

// Set a unique id for the component
payload.unique_id = 'zwavejs2mqtt_' + this.zwave.homeHex + '_' + valueId.id
Expand Down
21 changes: 21 additions & 0 deletions src/components/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,27 @@
persistent-hint
></v-switch>
</v-flex>
<v-flex xs6 v-if="gateway.hassDiscovery">
<v-text-field
v-on="on"
v-model="gateway.entityTemplate"
label="Entity name template"
persistent-hint
hint="Template which generates entity names"
></v-text-field>
</v-flex>
<v-flex xs6 v-if="gateway.hassDiscovery">
<div>
Default: <code>%loc-%n_%o</code> <br />-
<code>%nid</code>: Node ID <br />- <code>%n</code>:
Node Name <br />- <code>%loc</code>: Node Location
<br />- <code>%pk</code>: valueId property key
(fallback to device type) <br />- <code>%pn</code>:
valueId property name (fallback to device type)
<br />- <code>%o</code>: HASS object_id <br />-
<code>%l</code>: valueId label (fallback to object_id)
</div>
</v-flex>
<v-flex xs12 sm6>
<v-switch
hint="Enable gateway logging"
Expand Down

0 comments on commit 3a63e2e

Please sign in to comment.