Skip to content

Commit

Permalink
fix: Fix TS types
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Jan 9, 2024
1 parent ac04436 commit b805cf8
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 53 deletions.
6 changes: 4 additions & 2 deletions src/editor/editors/entity-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const filterIds = (
filter: string,
filterType: 'regex' | 'substring',
) => {
let results: HassEntity[];
let results: HassEntity[] = [];
if (filterType === 'regex') {
try {
const regex = new RegExp(filter);
Expand Down Expand Up @@ -98,7 +98,8 @@ export const entityFilter = {
.trigger('input');
},
};
RED.tray.show(trayOptions);
// TODO: Fix types find a way to get the type of the trayOptions object
RED.tray.show(trayOptions as any);
},
};

Expand All @@ -114,6 +115,7 @@ export const openEntityFilter = ({
complete: (filter: string) => void;
}) => {
RED.editor.showTypeEditor('ha_entity_filter', {
// @ts-expect-error - filter is an acceptable argument, DefinitelyTyped is wrong
filter,
filterType,
entities,
Expand Down
10 changes: 6 additions & 4 deletions src/editor/exposenode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function isEntityNode() {
NodeType.Text,
NodeType.TimeEntity,
];
return node?.type && nodes.includes(node.type);
return (
'type' in node && node?.type && nodes.includes(node.type as NodeType)
);
}

function getServerId(): string | undefined {
Expand Down Expand Up @@ -65,7 +67,7 @@ function isAddNodeSelected(selector: 'entityConfig' | 'server') {

export function init(n: HassNodeProperties) {
node = n;
const type = node.type as unknown as NodeType;
const type = 'type' in node && (node.type as unknown as NodeType);
render();

$('#node-input-server, #node-input-entityConfig').on('change', () => {
Expand Down Expand Up @@ -106,7 +108,7 @@ export function init(n: HassNodeProperties) {
}

function render() {
const type = node.type as unknown as NodeType;
const type = 'type' in node && (node.type as unknown as NodeType);

switch (type) {
case NodeType.BinarySensor:
Expand Down Expand Up @@ -146,7 +148,7 @@ function renderEventNode() {
const $row = $('<div />', {
id: 'exposeToHa',
class: `form-row checkbox-option${
node.type === 'trigger-state' ? '-left' : ''
'type' in node && node.type === 'trigger-state' ? '-left' : ''
}`,
});
$('<input />', {
Expand Down
2 changes: 1 addition & 1 deletion src/editor/sidebar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function initSidebar() {
id: 'ha_sidebar',
label: 'Home Assistant',
name: 'Home Assistant',
content,
content: content.get(0),
closeable: true,
disableOnEdit: true,
iconClass: 'fa fa-home',
Expand Down
6 changes: 3 additions & 3 deletions src/editor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ export interface EditorWidgetEditableListOptions<T>
buttons: EditorWidgetEditableListButton[];
}

export interface HassNodeProperties
extends Omit<EditorNodeProperties, 'outputs' | 'inputs'> {
export interface HassNodeProperties extends EditorNodeProperties {
version: number;
debugenabled?: boolean;
server?: string;
entityConfigNode?: string;
exposeAsEntityConfig?: string;
outputs?: number | undefined;
haConfig?: HassExposedConfig[];

// TODO: remove after controllers are converted to TypeScript
exposeToHomeAssistant?: boolean;
haConfig?: HassExposedConfig[];
}

export interface HassTargetDomains {
Expand Down
4 changes: 2 additions & 2 deletions src/editor/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function migrateNode(node: EditorNodeInstance<HassNodeProperties>) {
const migratedData: HassNodeProperties = migrate(data);

// TODO: Remove for version 1.0
if (migratedData.type === NodeType.Entity) {
if ('type' in migratedData && migratedData.type === NodeType.Entity) {
convertEntityNode(migratedData as unknown as EntityProperties);
} else if (haConfig) {
convertEventNode(migratedData as unknown as EntityProperties, haConfig);
Expand Down Expand Up @@ -187,7 +187,7 @@ function isHomeAssistantConfigNode(
NodeType.Server,
NodeType.EntityConfig,
NodeType.DeviceConfig,
].includes(node.type)
].includes(node.type as unknown as NodeType)
);
}

Expand Down
9 changes: 3 additions & 6 deletions src/nodes/number/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EditorNodeDef, EditorNodeProperties, EditorRED } from 'node-red';
import { EditorNodeDef, EditorRED } from 'node-red';

import {
EntityType,
Expand All @@ -9,14 +9,12 @@ import {
import * as haOutputs from '../../editor/components/output-properties';
import * as exposeNode from '../../editor/exposenode';
import ha, { NodeCategory, NodeColor } from '../../editor/ha';
import { OutputProperty } from '../../editor/types';
import { HassNodeProperties, OutputProperty } from '../../editor/types';
import { saveEntityType } from '../entity-config/editor/helpers';

declare const RED: EditorRED;

interface NumberEditorNodeProperties extends EditorNodeProperties {
version: number;
debugenabled: boolean;
interface NumberEditorNodeProperties extends HassNodeProperties {
entityConfig: any;
mode: ValueIntegrationMode;
outputProperties: OutputProperty[];
Expand All @@ -38,7 +36,6 @@ const NumberEditor: EditorNodeDef<NumberEditorNodeProperties> = {
name: { value: '' },
version: { value: RED.settings.get('haNumberVersion', 0) },
debugenabled: { value: false },
// @ts-expect-error - DefinitelyTyped is wrong inputs can be changed
inputs: { value: 0 },
outputs: { value: 1 },
entityConfig: {
Expand Down
9 changes: 3 additions & 6 deletions src/nodes/select/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EditorNodeDef, EditorNodeProperties, EditorRED } from 'node-red';
import { EditorNodeDef, EditorRED } from 'node-red';

import {
EntityType,
Expand All @@ -9,14 +9,12 @@ import {
import * as haOutputs from '../../editor/components/output-properties';
import * as exposeNode from '../../editor/exposenode';
import ha, { NodeCategory, NodeColor } from '../../editor/ha';
import { OutputProperty } from '../../editor/types';
import { HassNodeProperties, OutputProperty } from '../../editor/types';
import { saveEntityType } from '../entity-config/editor/helpers';

declare const RED: EditorRED;

interface SelectEditorNodeProperties extends EditorNodeProperties {
version: number;
debugenabled: boolean;
interface SelectEditorNodeProperties extends HassNodeProperties {
entityConfig: any;
mode: ValueIntegrationMode;
outputProperties: OutputProperty[];
Expand All @@ -38,7 +36,6 @@ const SelectEditor: EditorNodeDef<SelectEditorNodeProperties> = {
name: { value: '' },
version: { value: RED.settings.get('haTextVersion', 0) },
debugenabled: { value: false },
// @ts-expect-error - DefinitelyTyped is wrong inputs can be changed
inputs: { value: 0 },
outputs: { value: 1 },
entityConfig: {
Expand Down
9 changes: 3 additions & 6 deletions src/nodes/switch/editor.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { EditorNodeDef, EditorNodeProperties, EditorRED } from 'node-red';
import { EditorNodeDef, EditorRED } from 'node-red';

import { EntityType, NodeType } from '../../const';
import * as haOutputs from '../../editor/components/output-properties';
import * as exposeNode from '../../editor/exposenode';
import ha, { NodeCategory, NodeColor } from '../../editor/ha';
import { OutputProperty } from '../../editor/types';
import { HassNodeProperties, OutputProperty } from '../../editor/types';
import { saveEntityType } from '../entity-config/editor/helpers';

declare const RED: EditorRED;

interface SwitchEditorNodeProperties extends EditorNodeProperties {
version: number;
debugenabled: boolean;
interface SwitchEditorNodeProperties extends HassNodeProperties {
enableInput: boolean;
entityConfig: any;
outputOnStateChange: boolean;
Expand All @@ -34,7 +32,6 @@ const SwitchEditor: EditorNodeDef<SwitchEditorNodeProperties> = {
name: { value: '' },
version: { value: RED.settings.get('haSwitchVersion', 0) },
debugenabled: { value: false },
// @ts-expect-error - DefinitelyTyped is wrong inputs can be changed
inputs: { value: 0 },
outputs: { value: 2 },
entityConfig: {
Expand Down
9 changes: 3 additions & 6 deletions src/nodes/text/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EditorNodeDef, EditorNodeProperties, EditorRED } from 'node-red';
import { EditorNodeDef, EditorRED } from 'node-red';

import {
EntityType,
Expand All @@ -9,14 +9,12 @@ import {
import * as haOutputs from '../../editor/components/output-properties';
import * as exposeNode from '../../editor/exposenode';
import ha, { NodeCategory, NodeColor } from '../../editor/ha';
import { OutputProperty } from '../../editor/types';
import { HassNodeProperties, OutputProperty } from '../../editor/types';
import { saveEntityType } from '../entity-config/editor/helpers';

declare const RED: EditorRED;

interface TextEditorNodeProperties extends EditorNodeProperties {
version: number;
debugenabled: boolean;
interface TextEditorNodeProperties extends HassNodeProperties {
entityConfig: any;
mode: ValueIntegrationMode;
outputProperties: OutputProperty[];
Expand All @@ -38,7 +36,6 @@ const TextEditor: EditorNodeDef<TextEditorNodeProperties> = {
name: { value: '' },
version: { value: RED.settings.get('haTextVersion', 0) },
debugenabled: { value: false },
// @ts-expect-error - DefinitelyTyped is wrong inputs can be changed
inputs: { value: 0 },
outputs: { value: 1 },
entityConfig: {
Expand Down
9 changes: 3 additions & 6 deletions src/nodes/time-entity/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EditorNodeDef, EditorNodeProperties, EditorRED } from 'node-red';
import { EditorNodeDef, EditorRED } from 'node-red';

import {
EntityType,
Expand All @@ -9,14 +9,12 @@ import {
import * as haOutputs from '../../editor/components/output-properties';
import * as exposeNode from '../../editor/exposenode';
import ha, { NodeCategory, NodeColor } from '../../editor/ha';
import { OutputProperty } from '../../editor/types';
import { HassNodeProperties, OutputProperty } from '../../editor/types';
import { saveEntityType } from '../entity-config/editor/helpers';

declare const RED: EditorRED;

interface TimeEntityEditorNodeProperties extends EditorNodeProperties {
version: number;
debugenabled: boolean;
interface TimeEntityEditorNodeProperties extends HassNodeProperties {
entityConfig: any;
mode: ValueIntegrationMode;
value: string;
Expand All @@ -39,7 +37,6 @@ const TimeEntityEditor: EditorNodeDef<TimeEntityEditorNodeProperties> = {
name: { value: '' },
version: { value: RED.settings.get('haTimeEntityVersion', 0) },
debugenabled: { value: false },
// @ts-expect-error - DefinitelyTyped is wrong inputs can be changed
inputs: { value: 0 },
outputs: { value: 1 },
entityConfig: {
Expand Down
9 changes: 3 additions & 6 deletions src/nodes/trigger-state/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EditorNodeDef, EditorNodeProperties, EditorRED } from 'node-red';
import { EditorNodeDef, EditorRED } from 'node-red';

import { TransformType } from '../../common/TransformState';
import {
Expand All @@ -12,6 +12,7 @@ import * as exposeNode from '../../editor/exposenode';
import ha, { NodeCategory, NodeColor } from '../../editor/ha';
import * as haServer from '../../editor/haserver';
import { i18n } from '../../editor/i18n';
import { HassNodeProperties } from '../../editor/types';
import { saveEntityType } from '../entity-config/editor/helpers';
import {
ComparatorPropertyType,
Expand All @@ -24,12 +25,9 @@ import {

declare const RED: EditorRED;

interface TriggerStateEditorNodeProperties extends EditorNodeProperties {
server: string;
version: number;
interface TriggerStateEditorNodeProperties extends HassNodeProperties {
entityId: string | string[];
entityIdType: string;
debugEnabled: boolean;
constraints: Constraint[];
customOutputs: CustomOutput[];
outputInitially: boolean;
Expand Down Expand Up @@ -81,7 +79,6 @@ const TriggerStateEditor: EditorNodeDef<TriggerStateEditorNodeProperties> = {
name: { value: '' },
server: { value: '', type: NodeType.Server, required: true },
version: { value: RED.settings.get('triggerStateVersion', 0) },
// @ts-ignore - DefinitelyTyped is wrong inputs can be changed
inputs: { value: 0 },
outputs: { value: 2 },
exposeAsEntityConfig: {
Expand Down
12 changes: 7 additions & 5 deletions src/nodes/wait-until/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EditorNodeDef, EditorNodeProperties, EditorRED } from 'node-red';
import { EditorNodeDef, EditorRED } from 'node-red';

import {
ComparatorType,
Expand All @@ -10,13 +10,15 @@ import EntitySelector from '../../editor/components/EntitySelector';
import * as haOutputs from '../../editor/components/output-properties';
import ha, { NodeCategory, NodeColor } from '../../editor/ha';
import * as haServer from '../../editor/haserver';
import { HATypedInputTypeOptions, OutputProperty } from '../../editor/types';
import {
HassNodeProperties,
HATypedInputTypeOptions,
OutputProperty,
} from '../../editor/types';

declare const RED: EditorRED;

interface WaitUntilEditorNodeProperties extends EditorNodeProperties {
server: string;
version: number;
interface WaitUntilEditorNodeProperties extends HassNodeProperties {
entityId: string | string[];
entityIdFilterType: string;
property: string;
Expand Down

0 comments on commit b805cf8

Please sign in to comment.