From 5a698f80878d30b72d50bf12d50f3aec821b3d21 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 3 Dec 2024 03:10:32 -0600 Subject: [PATCH] Add updateAutomation method and update external service handling --- .../tuya/services/tuya.service.ts | 36 ++++++++++ src/automation/services/automation.service.ts | 71 +++++++++++++++---- 2 files changed, 93 insertions(+), 14 deletions(-) diff --git a/libs/common/src/integrations/tuya/services/tuya.service.ts b/libs/common/src/integrations/tuya/services/tuya.service.ts index 76c6582..e9c937f 100644 --- a/libs/common/src/integrations/tuya/services/tuya.service.ts +++ b/libs/common/src/integrations/tuya/services/tuya.service.ts @@ -218,6 +218,42 @@ export class TuyaService { return response; } + async updateAutomation( + automationUuid: string, + spaceId: string, + automationName: string, + actions: ConvertedAction[], + conditions: any[], + decisionExpr: string, + effectiveTime: any, + ) { + const path = `/v2.0/cloud/scene/rule/${automationUuid}`; + const response = await this.tuya.request({ + method: 'PUT', + path, + body: { + space_id: spaceId, + name: automationName, + effective_time: { + ...effectiveTime, + timezone_id: 'Asia/Dubai', + }, + type: 'automation', + decision_expr: decisionExpr, + conditions: conditions, + actions: actions, + }, + }); + + if (response.success) { + return response; + } else { + throw new HttpException( + `Error fetching automation rule: ${response.msg}`, + HttpStatus.BAD_REQUEST, + ); + } + } async updateAutomationState( spaceId: string, diff --git a/src/automation/services/automation.service.ts b/src/automation/services/automation.service.ts index 5e2caec..d28b944 100644 --- a/src/automation/services/automation.service.ts +++ b/src/automation/services/automation.service.ts @@ -256,8 +256,11 @@ export class AutomationService { if (sceneDetails.type === ActionTypeEnum.SCENE) { const scene = await this.sceneRepository.findOne({ where: { sceneTuyaUuid: action.entityId }, + relations: ['sceneIcon'], }); action.entityId = scene.uuid; + action.iconUuid = scene.sceneIcon.uuid; + action.icon = scene.sceneIcon.icon; } action.name = sceneDetails.name; action.type = sceneDetails.type; @@ -377,13 +380,54 @@ export class AutomationService { } } } + async updateAutomationExternalService( + spaceTuyaUuid: string, + automationUuid: string, + updateAutomationDto: UpdateAutomationDto, + ) { + const { automationName, decisionExpr, actions, conditions, effectiveTime } = + updateAutomationDto; + try { + const formattedActions = await this.prepareActions(actions); + const formattedCondition = await this.prepareConditions(conditions); + const response = await this.tuyaService.updateAutomation( + automationUuid, + spaceTuyaUuid, + automationName, + formattedActions, + formattedCondition, + decisionExpr, + effectiveTime, + ); + if (!response.success) { + throw new HttpException( + 'Failed to update automation in Tuya', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + + return response; + } catch (err) { + if (err instanceof HttpException) { + throw err; + } else if (err.message?.includes('tuya')) { + throw new HttpException( + 'API error: Failed to update automation', + HttpStatus.BAD_GATEWAY, + ); + } else { + throw new HttpException( + `An Internal error has been occured ${err}`, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + } async updateAutomation( updateAutomationDto: UpdateAutomationDto, automationUuid: string, ) { - const { actions, conditions, automationName, effectiveTime, decisionExpr } = - updateAutomationDto; try { const automation = await this.getAutomationDetails(automationUuid, true); if (!automation.spaceId) { @@ -392,19 +436,18 @@ export class AutomationService { HttpStatus.NOT_FOUND, ); } + const updateTuyaAutomationResponse = + await this.updateAutomationExternalService( + automation.spaceId, + automation.id, + updateAutomationDto, + ); - const newAutomation = await this.add({ - actions, - conditions, - automationName, - effectiveTime, - decisionExpr, - spaceTuyaId: automation.spaceId, - }); - - if (newAutomation.id) { - await this.delete(automation.spaceId, automationUuid); - return newAutomation; + if (!updateTuyaAutomationResponse.success) { + throw new HttpException( + `Failed to update a external automation`, + HttpStatus.BAD_GATEWAY, + ); } } catch (err) { if (err instanceof BadRequestException) {