-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Able to access flow global and states in templates
You can use templates in the Entity Id and data fields. When using templates the top level is a property of the message object: msg.payload would be {{payload}}. You can also access the flow, global and states contexts {{flow.foobar}} {{global.something}}. For the states context you can use the {{states.domain.entity_id}} to just get the state or drill further down like {{states.light.kitchen.attributes.friendly_name}}. {{states.light.kitchen}} and {{states.light.kitchen.state}} are equivalent.
- Loading branch information
Showing
5 changed files
with
129 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Modified from https://github.com/node-red/node-red/blob/master/nodes/core/core/80-template.js | ||
*/ | ||
|
||
const mustache = require('mustache'); | ||
const selectn = require('selectn'); | ||
|
||
function parseContext(key) { | ||
var match = /^(flow|global)(\[(\w+)\])?\.(.+)/.exec(key); | ||
if (match) { | ||
var parts = {}; | ||
parts.type = match[1]; | ||
parts.store = match[3] === '' ? 'default' : match[3]; | ||
parts.field = match[4]; | ||
return parts; | ||
} | ||
return undefined; | ||
} | ||
|
||
/** | ||
* Custom Mustache Context capable to collect message property and node | ||
* flow and global context | ||
*/ | ||
|
||
function NodeContext(msg, parent, nodeContext, serverName) { | ||
this.msgContext = new mustache.Context(msg, parent); | ||
this.nodeContext = nodeContext; | ||
this.serverName = serverName; | ||
} | ||
|
||
NodeContext.prototype = new mustache.Context(); | ||
|
||
NodeContext.prototype.lookup = function(name) { | ||
// try message first: | ||
try { | ||
const value = this.msgContext.lookup(name); | ||
if (value !== undefined) { | ||
return value; | ||
} | ||
|
||
// try flow/global context: | ||
const context = parseContext(name); | ||
if (context) { | ||
const type = context.type; | ||
const store = context.store; | ||
const field = context.field; | ||
const target = this.nodeContext[type]; | ||
if (target) { | ||
return target.get(field, store); | ||
} | ||
} | ||
|
||
// try state entities | ||
const match = /^states\.(\w+\.\w+)(?:\.(.+))?/.exec(name); | ||
if (match) { | ||
const gHomeAssistant = this.nodeContext.global.get('homeassistant'); | ||
const states = gHomeAssistant[this.serverName].states; | ||
const entityId = match[1]; | ||
const path = match[2] || 'state'; | ||
|
||
return selectn(path, states[entityId]) || ''; | ||
} | ||
|
||
return ''; | ||
} catch (err) { | ||
throw err; | ||
} | ||
}; | ||
|
||
NodeContext.prototype.push = function push(view) { | ||
return new NodeContext( | ||
view, | ||
this.nodeContext, | ||
this.msgContext, | ||
this.serverName | ||
); | ||
}; | ||
|
||
module.exports = NodeContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters