Skip to content

Commit

Permalink
fix: Fix duplication of nodes when converting expose as
Browse files Browse the repository at this point in the history
fix: Default isEnable to true

refactor: Add JSONataService to Output Controller
  • Loading branch information
zachowj committed Sep 24, 2023
1 parent 827b926 commit 39c8b03
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/common/controllers/EposeAsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default abstract class ExposeAsController<
}

get isEnabled(): boolean {
return this.exposeAsConfigNode?.state?.isEnabled() ?? false;
return this.exposeAsConfigNode?.state?.isEnabled() ?? true;
}

protected async validateTriggerMessage(
Expand Down
5 changes: 5 additions & 0 deletions src/common/controllers/OutputController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
OutputProperty,
} from '../../types/nodes';
import { NodeEvent } from '../events/Events';
import JSONataService from '../services/JSONataService';
import NodeRedContextService from '../services/NodeRedContextService';
import TypedInputService from '../services/TypedInputService';
import Status from '../status/Status';
Expand All @@ -19,22 +20,26 @@ export interface OutputControllerConstructor<T extends BaseNode> {
node: T;
status: Status;
typedInputService: TypedInputService;
jsonataService: JSONataService;
}

// export default abstract class OutputController<T extends BaseNode> {
export default abstract class OutputController<T extends BaseNode = BaseNode> {
protected readonly contextService: NodeRedContextService;
protected readonly jsonataService: JSONataService;
protected readonly node: T;
protected readonly status: Status;
protected readonly typedInputService: TypedInputService;

constructor({
nodeRedContextService,
node,
jsonataService,
status,
typedInputService,
}: OutputControllerConstructor<T>) {
this.contextService = nodeRedContextService;
this.jsonataService = jsonataService;
this.node = node;
this.status = status;
this.typedInputService = typedInputService;
Expand Down
47 changes: 28 additions & 19 deletions src/editor/convert-entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// TODO: Remove for version 1.0
import { EditorRED } from 'node-red';
import { EditorNodeInstance, EditorRED } from 'node-red';

import { EntityType, NodeType } from '../const';
import { HassExposedConfig } from './types';
import { HassExposedConfig, HassNodeProperties } from './types';

declare const RED: EditorRED;

Expand Down Expand Up @@ -184,7 +184,7 @@ export const convertEntityNode = (node: EntityProperties) => {
RED.view.redraw(true);
};

export const convertEventNode = (node: any) => {
export const convertEventNode = (node: any, haConfig: HassExposedConfig[]) => {
// Save the wires so we can add them to the new node
const wires = {
// @ts-expect-error - function is not defined in types
Expand All @@ -193,29 +193,40 @@ export const convertEventNode = (node: any) => {
target: RED.nodes.getNodeLinks(node.id, 1),
};

const newId = generateId();
// If the node is in a group remove it so NR doesn't think the new config node is in the group
if (node.g) {
const oldEntityNode = RED.nodes.node(node.id);
if (oldEntityNode) {
RED.group.removeFromGroup(RED.nodes.group(node.g), oldEntityNode);
}
}
RED.nodes.remove(node.id);
RED.nodes.import({
type: NodeType.EntityConfig,
id: node.id,
server: node.server,
deviceConfig: '',
name: `exposed as for ${node.name || node.id}`,
version: RED.settings.get('haEntityConfigVersion', 0),
entityType: EntityType.Switch,
haConfig: node.haConfig ?? [],
resend: false,
});
RED.nodes.import(
[
{
type: NodeType.EntityConfig,
id: node.id,
server: node.server,
deviceConfig: '',
name: `exposed as for ${node.name || node.id}`,
version: RED.settings.get('haEntityConfigVersion', 0),
entityType: EntityType.Switch,
haConfig: haConfig ?? [],
resend: false,
},
],
// @ts-expect-error - options are not defined in types
{
importMap: {
[node.id]: 'replace',
},
}
);
const newId = generateId();
RED.nodes.import({ ...node, id: newId, exposeAsEntityConfig: node.id });
addLinks(newId, wires);
const entityNode = RED.nodes.node(newId);
const entityNode = RED.nodes.node(
newId
) as EditorNodeInstance<HassNodeProperties>;
if (entityNode) {
RED.nodes.moveNodeToTab(entityNode, node.z);
if (node.g) {
Expand All @@ -225,6 +236,4 @@ export const convertEventNode = (node: any) => {
entityNode.changed = true;
}
RED.view.redraw(true);

return newId;
};
13 changes: 6 additions & 7 deletions src/editor/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,22 @@ export function versionCheckOnEditPrepare(
const exposedEventNodes: NodeType[] = [NodeType.Tag];

function migrateNode(node: EditorNodeInstance<HassNodeProperties>) {
let data = RED.nodes.convertNode(node, false);
const data = RED.nodes.convertNode(node, false) as HassNodeProperties;

// TODO: Remove for version 1.0
if (
const haConfig =
exposedEventNodes.includes(node.type as unknown as NodeType) &&
node.exposeToHomeAssistant === true
) {
const newId = convertEventNode(data as unknown as EntityProperties);
node = RED.nodes.node(newId) as EditorNodeInstance<HassNodeProperties>;
data = RED.nodes.convertNode(node, false);
}
? data.haConfig
: undefined;

const migratedData: HassNodeProperties = migrate(data);

// TODO: Remove for version 1.0
if (migratedData.type === NodeType.Entity) {
convertEntityNode(migratedData as unknown as EntityProperties);
} else if (haConfig) {
convertEventNode(migratedData as unknown as EntityProperties, haConfig);
}

let key: keyof HassNodeProperties;
Expand Down

0 comments on commit 39c8b03

Please sign in to comment.