From 7bcd56277c3ce6cd3cf01dfa50d077a7a3ea86c7 Mon Sep 17 00:00:00 2001 From: Sadie Hain Date: Tue, 3 Jan 2023 21:28:29 -0500 Subject: [PATCH] Add Tuya Plugin --- src/app/config/config.default.ts | 4 +++ src/app/config/config.model.ts | 5 ++++ src/app/config/config.service.ts | 8 ++++++ .../setup/plugins/plugins.component.html | 8 ++++++ .../config/setup/plugins/plugins.component.ts | 2 ++ src/app/config/setup/setup.component.html | 1 + src/app/model/octoprint/index.ts | 1 + src/app/model/octoprint/plugins/tuya.model.ts | 4 +++ .../enclosure/enclosure.octoprint.service.ts | 25 +++++++++++++++++++ src/app/settings/settings.component.html | 23 +++++++++++++++++ 10 files changed, 81 insertions(+) create mode 100644 src/app/model/octoprint/plugins/tuya.model.ts diff --git a/src/app/config/config.default.ts b/src/app/config/config.default.ts index 9b497443f..062dab5bf 100644 --- a/src/app/config/config.default.ts +++ b/src/app/config/config.default.ts @@ -70,6 +70,10 @@ export const defaultConfig: Config = { topic: 'topic', relayNumber: null, }, + tuya: { + enabled: false, + label: 'label', + }, }, octodash: { customActions: [ diff --git a/src/app/config/config.model.ts b/src/app/config/config.model.ts index 636acfd4c..3010f94b5 100644 --- a/src/app/config/config.model.ts +++ b/src/app/config/config.model.ts @@ -60,6 +60,7 @@ interface Plugins { tpLinkSmartPlug: TPLinkSmartPlugPlugin; tasmota: TasmotaPlugin; tasmotaMqtt: TasmotaMqttPlugin; + tuya: TuyaPlugin; } interface Plugin { @@ -92,6 +93,10 @@ interface TasmotaMqttPlugin extends Plugin { relayNumber: number; } +interface TuyaPlugin extends Plugin { + label: string; +} + interface OctoDash { customActions: CustomAction[]; fileSorting: FileSorting; diff --git a/src/app/config/config.service.ts b/src/app/config/config.service.ts index d83757d03..3beeddfad 100644 --- a/src/app/config/config.service.ts +++ b/src/app/config/config.service.ts @@ -185,6 +185,14 @@ export class ConfigService { return this.config.plugins.ophom.enabled; } + public useTuya(): boolean { + return this.config.plugins.tuya.enabled; + } + + public getTuyaLabel(): string { + return this.config.plugins.tuya.label; + } + public useTpLinkSmartPlug(): boolean { return this.config.plugins.tpLinkSmartPlug.enabled; } diff --git a/src/app/config/setup/plugins/plugins.component.html b/src/app/config/setup/plugins/plugins.component.html index 5f7823a8d..cbf4ef80d 100644 --- a/src/app/config/setup/plugins/plugins.component.html +++ b/src/app/config/setup/plugins/plugins.component.html @@ -69,6 +69,14 @@ TPLink SmartPlug + + + Tuya SmartPlug + + diff --git a/src/app/config/setup/plugins/plugins.component.ts b/src/app/config/setup/plugins/plugins.component.ts index 471e64499..6ea120d29 100644 --- a/src/app/config/setup/plugins/plugins.component.ts +++ b/src/app/config/setup/plugins/plugins.component.ts @@ -15,6 +15,7 @@ export class PluginsComponent { @Input() psuControlPlugin: boolean; @Input() ophomPlugin: boolean; @Input() tpLinkSmartPlugPlugin: boolean; + @Input() tuyaPlugin: boolean; @Input() tasmotaPlugin: boolean; @Input() tasmotaMqttPlugin: boolean; @@ -27,6 +28,7 @@ export class PluginsComponent { @Output() psuControlPluginChange = new EventEmitter(); @Output() ophomPluginChange = new EventEmitter(); @Output() tpLinkSmartPlugPluginChange = new EventEmitter(); + @Output() tuyaPluginChange = new EventEmitter(); @Output() tasmotaPluginChange = new EventEmitter(); @Output() tasmotaMqttPluginChange = new EventEmitter(); } diff --git a/src/app/config/setup/setup.component.html b/src/app/config/setup/setup.component.html index a7a57395c..75eca6bdd 100644 --- a/src/app/config/setup/setup.component.html +++ b/src/app/config/setup/setup.component.html @@ -59,6 +59,7 @@ [(psuControlPlugin)]="config.plugins.psuControl.enabled" [(ophomPlugin)]="config.plugins.ophom.enabled" [(tpLinkSmartPlugPlugin)]="config.plugins.tpLinkSmartPlug.enabled" + [(tyuaPlugin)]="config.plugins.tuya.enabled" [(tasmotaPlugin)]="config.plugins.tasmota.enabled" [(tasmotaMqttPlugin)]="config.plugins.tasmotaMqtt.enabled"> diff --git a/src/app/model/octoprint/index.ts b/src/app/model/octoprint/index.ts index 24edd4f29..c4d69c80b 100644 --- a/src/app/model/octoprint/index.ts +++ b/src/app/model/octoprint/index.ts @@ -15,3 +15,4 @@ export * from './plugins/ophomplugstatus.model'; export * from './plugins/tp-link.model'; export * from './plugins/tasmota.model'; export * from './plugins/tasmota-mqtt.model'; +export * from './plugins/tuya.model'; diff --git a/src/app/model/octoprint/plugins/tuya.model.ts b/src/app/model/octoprint/plugins/tuya.model.ts new file mode 100644 index 000000000..057e65e19 --- /dev/null +++ b/src/app/model/octoprint/plugins/tuya.model.ts @@ -0,0 +1,4 @@ +export interface TuyaCommand { + command: 'turnOn' | 'turnOff'; + label: string; +} diff --git a/src/app/services/enclosure/enclosure.octoprint.service.ts b/src/app/services/enclosure/enclosure.octoprint.service.ts index 68398cbd7..cb4ad6fa5 100644 --- a/src/app/services/enclosure/enclosure.octoprint.service.ts +++ b/src/app/services/enclosure/enclosure.octoprint.service.ts @@ -15,6 +15,7 @@ import { TasmotaCommand, TasmotaMqttCommand, TPLinkCommand, + TuyaCommand, } from '../../model/octoprint'; import { NotificationService } from '../../notification/notification.service'; import { EnclosureService } from './enclosure.service'; @@ -158,6 +159,8 @@ export class EnclosureOctoprintService implements EnclosureService { this.setPSUStateTasmota(state); } else if (this.configService.useTasmotaMqtt()) { this.setPSUStateTasmotaMqtt(state); + } else if (this.configService.useTuya()) { + this.setPSUStateTuya(state); } else { this.notificationService.setNotification({ heading: $localize`:@@error-psu-state:Can't change PSU State!`, @@ -307,6 +310,28 @@ export class EnclosureOctoprintService implements EnclosureService { .subscribe(); } + private setPSUStateTuya(state: PSUState) { + const tuyaPayload: TuyaCommand = { + command: state === PSUState.ON ? 'turnOn' : 'turnOff', + label: this.configService.getTuyaLabel(), + }; + + this.http + .post(this.configService.getApiURL('plugin/tuyasmartplug'), tuyaPayload, this.configService.getHTTPHeaders()) + .pipe( + catchError(error => { + this.notificationService.setNotification({ + heading: $localize`:@@error-send-psu-gcode:Can't send GCode!`, + text: error.message, + type: NotificationType.ERROR, + time: new Date(), + }); + return of(null); + }), + ) + .subscribe(); + } + togglePSU(): void { this.currentPSUState === PSUState.ON ? this.setPSUState(PSUState.OFF) : this.setPSUState(PSUState.ON); } diff --git a/src/app/settings/settings.component.html b/src/app/settings/settings.component.html index d42bc3ff2..9b3126f0f 100644 --- a/src/app/settings/settings.component.html +++ b/src/app/settings/settings.component.html @@ -317,6 +317,7 @@ config.plugins.psuControl.enabled || config.plugins.ophom.enabled || config.plugins.tpLinkSmartPlug.enabled || + config.plugins.tuya.enabled || config.plugins.tasmota.enabled || config.plugins.tasmotaMqtt.enabled ) @@ -330,6 +331,7 @@ config.plugins.psuControl.enabled || config.plugins.ophom.enabled || config.plugins.tpLinkSmartPlug.enabled || + config.plugins.tuya.enabled || config.plugins.tasmota.enabled || config.plugins.tasmotaMqtt.enabled ) @@ -579,6 +581,27 @@ [(ngModel)]="config.plugins.tpLinkSmartPlug.smartPlugIP" required [disabled]="!config.plugins.tpLinkSmartPlug.enabled" /> + Tuya Smart Plug +
+ + + + enabled +
+ + Tasmota