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 { export enum ActionExecutorEnum {
DEVICE_ISSUE = 'device_issue', DEVICE_ISSUE = 'device_issue',
DELAY = 'delay', DELAY = 'delay',
RULE_TRIGGER = 'rule_trigger',
} }
export enum EntityTypeEnum { export enum EntityTypeEnum {
@ -18,3 +19,8 @@ export const AUTOMATION_CONFIG = {
}; };
export const AUTOMATION_TYPE = 'automation'; export const AUTOMATION_TYPE = 'automation';
export const AUTO_PREFIX = 'Auto_'; 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 { export interface ConvertedExecutorProperty {
function_code?: string; function_code?: string;
function_value?: any; function_value?: any;
@ -7,5 +9,6 @@ export interface ConvertedExecutorProperty {
export interface ConvertedAction { export interface ConvertedAction {
entity_id: string; entity_id: string;
action_executor: string; action_executor: string;
action_type?: ActionTypeEnum;
executor_property?: ConvertedExecutorProperty; executor_property?: ConvertedExecutorProperty;
} }

View File

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

View File

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

View File

@ -26,6 +26,7 @@ import {
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter'; import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { import {
ActionExecutorEnum, ActionExecutorEnum,
ActionTypeEnum,
AUTO_PREFIX, AUTO_PREFIX,
AUTOMATION_TYPE, AUTOMATION_TYPE,
EntityTypeEnum, EntityTypeEnum,
@ -247,20 +248,19 @@ export class AutomationService {
action.actionExecutor !== ActionExecutorEnum.DEVICE_ISSUE && action.actionExecutor !== ActionExecutorEnum.DEVICE_ISSUE &&
action.actionExecutor !== ActionExecutorEnum.DELAY action.actionExecutor !== ActionExecutorEnum.DELAY
) { ) {
const scene = await this.sceneRepository.findOne({ const sceneDetails = await this.getTapToRunSceneDetailsTuya(
where: { action.entityId,
uuid: action.entityId, );
},
});
if (scene.uuid) {
const sceneDetails = await this.getTapToRunSceneDetailsTuya(
scene.sceneTuyaUuid,
);
if (sceneDetails.id) { if (sceneDetails.id) {
action.name = sceneDetails.name; if (sceneDetails.type === ActionTypeEnum.SCENE) {
action.type = sceneDetails.type; 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) { if (device) {
action.entity_id = device.deviceTuyaUuid; 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;
}
}
} }
}), }),
); );