Add RULE_TRIGGER to ActionExecutorEnum and introduce ActionTypeEnum

This commit is contained in:
faris Aljohari
2024-12-02 05:25:11 -06:00
parent 7dca9ea5ff
commit b507bcbdf1
5 changed files with 45 additions and 12 deletions

View File

@ -1,6 +1,7 @@
export enum ActionExecutorEnum {
DEVICE_ISSUE = 'device_issue',
DELAY = 'delay',
RULE_TRIGGER = 'rule_trigger',
}
export enum EntityTypeEnum {
@ -18,3 +19,8 @@ export const AUTOMATION_CONFIG = {
};
export const AUTOMATION_TYPE = 'automation';
export const AUTO_PREFIX = 'Auto_';
export enum ActionTypeEnum {
SCENE = 'scene',
AUTOMATION = 'automation',
}

View File

@ -1,3 +1,5 @@
import { ActionTypeEnum } from '@app/common/constants/automation.enum';
export interface ConvertedExecutorProperty {
function_code?: string;
function_value?: any;
@ -7,5 +9,6 @@ export interface ConvertedExecutorProperty {
export interface ConvertedAction {
entity_id: string;
action_executor: string;
action_type?: ActionTypeEnum;
executor_property?: ConvertedExecutorProperty;
}

View File

@ -7,8 +7,10 @@ import {
IsOptional,
IsNumber,
IsBoolean,
IsEnum,
} from 'class-validator';
import { Type } from 'class-transformer';
import { ActionTypeEnum } from '@app/common/constants/automation.enum';
export class EffectiveTime {
@ApiProperty({ description: 'Start time', required: true })
@ -102,6 +104,11 @@ class Action {
@IsNotEmpty()
public actionExecutor: string;
@ApiProperty({ description: 'Action type', required: false })
@IsOptional()
@IsEnum(ActionTypeEnum)
public actionType?: ActionTypeEnum;
@ApiProperty({
description: 'Executor property',
required: false,

View File

@ -1,3 +1,4 @@
import { ActionTypeEnum } from '@app/common/constants/automation.enum';
import { EffectiveTime } from '../dtos';
export interface AddAutomationInterface {
@ -26,6 +27,7 @@ export interface DeleteAutomationInterface {
export interface Action {
actionExecutor: string;
entityId: string;
actionType?: ActionTypeEnum;
[key: string]: any; // Allow additional properties
}

View File

@ -26,6 +26,7 @@ import {
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import {
ActionExecutorEnum,
ActionTypeEnum,
AUTO_PREFIX,
AUTOMATION_TYPE,
EntityTypeEnum,
@ -247,20 +248,19 @@ export class AutomationService {
action.actionExecutor !== ActionExecutorEnum.DEVICE_ISSUE &&
action.actionExecutor !== ActionExecutorEnum.DELAY
) {
const scene = await this.sceneRepository.findOne({
where: {
uuid: action.entityId,
},
});
if (scene.uuid) {
const sceneDetails = await this.getTapToRunSceneDetailsTuya(
scene.sceneTuyaUuid,
);
const sceneDetails = await this.getTapToRunSceneDetailsTuya(
action.entityId,
);
if (sceneDetails.id) {
action.name = sceneDetails.name;
action.type = sceneDetails.type;
if (sceneDetails.id) {
if (sceneDetails.type === ActionTypeEnum.SCENE) {
const scene = await this.sceneRepository.findOne({
where: { sceneTuyaUuid: action.entityId },
});
action.entityId = scene.uuid;
}
action.name = sceneDetails.name;
action.type = sceneDetails.type;
}
}
}
@ -463,6 +463,21 @@ export class AutomationService {
if (device) {
action.entity_id = device.deviceTuyaUuid;
}
} else if (action.action_executor === ActionExecutorEnum.RULE_TRIGGER) {
// Check if action_type is missing
if (!action.action_type) {
throw new Error(`actionType is required for rule_trigger actions`);
}
if (action.action_type === ActionTypeEnum.SCENE) {
const scene = await this.sceneRepository.findOne({
where: { uuid: action.entity_id },
});
if (scene) {
action.entity_id = scene.sceneTuyaUuid;
}
}
}
}),
);