diff --git a/docs/node/sentence.md b/docs/node/sentence.md index f090dac55f..73cebd6504 100644 --- a/docs/node/sentence.md +++ b/docs/node/sentence.md @@ -17,6 +17,12 @@ in Home Assistant for this node to function_ A list of sentences to match. Sentences are allowed to use some basic template syntax. Check Home Assistant [documentation](https://www.home-assistant.io/docs/automation/trigger/#sentence-trigger) for more information. +### Expose as + +- Type: `string` + +When an entity is selected a switch entity will be created in Home Assistant. Turning on and off this switch will disable/enable the nodes in Node-RED. + ## Outputs Value types: diff --git a/src/nodes/sentence/SentenceController.ts b/src/nodes/sentence/SentenceController.ts index 886d9c54b9..30c5db38fc 100644 --- a/src/nodes/sentence/SentenceController.ts +++ b/src/nodes/sentence/SentenceController.ts @@ -1,4 +1,4 @@ -import OutputController from '../../common/controllers/OutputController'; +import ExposeAsController from '../../common/controllers/EposeAsController'; import { NodeMessage } from '../../types/nodes'; import { SentenceNode } from '.'; @@ -7,8 +7,10 @@ interface SentenceResponse { result: Record; } -export default class SentenseController extends OutputController { +export default class SentenseController extends ExposeAsController { public onReceivedMessage(data: SentenceResponse) { + if (!this.isEnabled) return; + const message: NodeMessage = {}; this.setCustomOutputs(this.node.config.outputProperties, message, { config: this.node.config, diff --git a/src/nodes/sentence/editor.html b/src/nodes/sentence/editor.html index 7e1904b341..65eb657a81 100644 --- a/src/nodes/sentence/editor.html +++ b/src/nodes/sentence/editor.html @@ -25,3 +25,11 @@
    + +
    + + +
    diff --git a/src/nodes/sentence/editor.ts b/src/nodes/sentence/editor.ts index 4af09ac5b0..46214216d4 100644 --- a/src/nodes/sentence/editor.ts +++ b/src/nodes/sentence/editor.ts @@ -1,11 +1,12 @@ import { EditorNodeDef, EditorNodeProperties, EditorRED } from 'node-red'; -import { NodeType, TypedInputTypes } from '../../const'; +import { EntityType, NodeType, TypedInputTypes } from '../../const'; import * as haOutputs from '../../editor/components/output-properties'; import * as exposeNode from '../../editor/exposenode'; import ha, { NodeCategory, NodeColor } from '../../editor/ha'; import * as haServer from '../../editor/haserver'; import { OutputProperty } from '../../editor/types'; +import { saveEntityType } from '../entity-config/editor/helpers'; declare const RED: EditorRED; @@ -15,6 +16,7 @@ interface SentenceEditorNodeProperties extends EditorNodeProperties { sentences: string[]; response: string; outputProperties: OutputProperty[]; + exposeAsEntityConfig: string; } const SentenceEditor: EditorNodeDef = { @@ -33,6 +35,13 @@ const SentenceEditor: EditorNodeDef = { server: { value: '', type: NodeType.Server, required: true }, version: { value: RED.settings.get('haSentenceVersion', 0) }, outputs: { value: 1 }, + exposeAsEntityConfig: { + value: '', + type: NodeType.EntityConfig, + // @ts-ignore - DefinitelyTyped is missing this property + filter: (config) => config.entityType === EntityType.Switch, + required: false, + }, sentences: { value: [], validate: (s) => s.length > 0 }, response: { value: '' }, outputProperties: { @@ -57,6 +66,7 @@ const SentenceEditor: EditorNodeDef = { ha.setup(this); haServer.init(this, '#node-input-server'); exposeNode.init(this); + saveEntityType(EntityType.Switch, 'exposeAsEntityConfig'); $('#dialog-form').prepend(ha.betaWarning(981)); diff --git a/src/nodes/sentence/index.ts b/src/nodes/sentence/index.ts index 1be3a1eb17..e87f12ad6a 100644 --- a/src/nodes/sentence/index.ts +++ b/src/nodes/sentence/index.ts @@ -6,7 +6,7 @@ import State from '../../common/State'; import Status from '../../common/status/Status'; import { RED } from '../../globals'; import { migrate } from '../../helpers/migrate'; -import { getServerConfigNode } from '../../helpers/node'; +import { getExposeAsConfigNode, getServerConfigNode } from '../../helpers/node'; import { getHomeAssistant } from '../../homeAssistant/index'; import { BaseNode, @@ -20,6 +20,7 @@ export interface SentenceNodeProperties extends BaseNodeProperties { sentences: string[]; response: string; outputProperties: OutputProperty[]; + exposeAsEntityConfig: string; } export interface SentenceNode extends BaseNode { @@ -35,6 +36,9 @@ export default function sentenceNode( const serverConfigNode = getServerConfigNode(this.config.server); const homeAssistant = getHomeAssistant(serverConfigNode); + const exposeAsConfigNode = getExposeAsConfigNode( + this.config.exposeAsEntityConfig + ); const clientEvents = new ClientEvents({ node: this, emitter: homeAssistant.eventBus, @@ -42,6 +46,7 @@ export default function sentenceNode( const nodeEvents = new Events({ node: this, emitter: this }); const state = new State(this); const status = new Status({ + exposeAsEntityConfigNode: exposeAsConfigNode, config: serverConfigNode.config, node: this, }); @@ -56,6 +61,8 @@ export default function sentenceNode( integration.setStatus(status); const controller = new SentenceController({ + exposeAsConfigNode, + homeAssistant, node: this, status, ...controllerDeps, diff --git a/src/nodes/sentence/migrations.ts b/src/nodes/sentence/migrations.ts index 03a63d9b41..be8f833aa0 100644 --- a/src/nodes/sentence/migrations.ts +++ b/src/nodes/sentence/migrations.ts @@ -15,6 +15,7 @@ export default [ const newSchema = { ...schema, version: 1, + exposeAsEntityConfig: '', response: '', }; return newSchema; diff --git a/test/migrations/sentence.test.js b/test/migrations/sentence.test.js index 75ff641f09..b3c896d8de 100644 --- a/test/migrations/sentence.test.js +++ b/test/migrations/sentence.test.js @@ -44,6 +44,7 @@ const VERSION_1 = { ...VERSION_0, version: 1, response: '', + exposeAsEntityConfig: '', }; describe('Migrations - Sentense Node', function () {