Skip to content

Commit

Permalink
feat: Add expose as
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Sep 24, 2023
1 parent e34b1cb commit 8c7991c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/node/sentence.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions src/nodes/sentence/SentenceController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import OutputController from '../../common/controllers/OutputController';
import ExposeAsController from '../../common/controllers/EposeAsController';
import { NodeMessage } from '../../types/nodes';
import { SentenceNode } from '.';

Expand All @@ -7,8 +7,10 @@ interface SentenceResponse {
result: Record<string, unknown>;
}

export default class SentenseController extends OutputController<SentenceNode> {
export default class SentenseController extends ExposeAsController<SentenceNode> {
public onReceivedMessage(data: SentenceResponse) {
if (!this.isEnabled) return;

const message: NodeMessage = {};
this.setCustomOutputs(this.node.config.outputProperties, message, {
config: this.node.config,
Expand Down
8 changes: 8 additions & 0 deletions src/nodes/sentence/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@
<div class="form-row">
<ol id="custom-outputs"></ol>
</div>

<div class="form-row" id="exposed-as-row">
<label
for="node-input-exposeAsEntityConfig"
data-i18n="ha-tag.label.expose_as"
></label>
<input type="text" id="node-input-exposeAsEntityConfig" />
</div>
12 changes: 11 additions & 1 deletion src/nodes/sentence/editor.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -15,6 +16,7 @@ interface SentenceEditorNodeProperties extends EditorNodeProperties {
sentences: string[];
response: string;
outputProperties: OutputProperty[];
exposeAsEntityConfig: string;
}

const SentenceEditor: EditorNodeDef<SentenceEditorNodeProperties> = {
Expand All @@ -33,6 +35,13 @@ const SentenceEditor: EditorNodeDef<SentenceEditorNodeProperties> = {
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: {
Expand All @@ -57,6 +66,7 @@ const SentenceEditor: EditorNodeDef<SentenceEditorNodeProperties> = {
ha.setup(this);
haServer.init(this, '#node-input-server');
exposeNode.init(this);
saveEntityType(EntityType.Switch, 'exposeAsEntityConfig');

$('#dialog-form').prepend(ha.betaWarning(981));

Expand Down
9 changes: 8 additions & 1 deletion src/nodes/sentence/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,6 +20,7 @@ export interface SentenceNodeProperties extends BaseNodeProperties {
sentences: string[];
response: string;
outputProperties: OutputProperty[];
exposeAsEntityConfig: string;
}

export interface SentenceNode extends BaseNode {
Expand All @@ -35,13 +36,17 @@ 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,
});
const nodeEvents = new Events({ node: this, emitter: this });
const state = new State(this);
const status = new Status({
exposeAsEntityConfigNode: exposeAsConfigNode,
config: serverConfigNode.config,
node: this,
});
Expand All @@ -56,6 +61,8 @@ export default function sentenceNode(
integration.setStatus(status);

const controller = new SentenceController({
exposeAsConfigNode,
homeAssistant,
node: this,
status,
...controllerDeps,
Expand Down
1 change: 1 addition & 0 deletions src/nodes/sentence/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default [
const newSchema = {
...schema,
version: 1,
exposeAsEntityConfig: '',
response: '',
};
return newSchema;
Expand Down
1 change: 1 addition & 0 deletions test/migrations/sentence.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const VERSION_1 = {
...VERSION_0,
version: 1,
response: '',
exposeAsEntityConfig: '',
};

describe('Migrations - Sentense Node', function () {
Expand Down

0 comments on commit 8c7991c

Please sign in to comment.